Use NHibernate 2.1 to adapt to a SQLite database

Source: Internet
Author: User
Tags sqlite sqlite database

Configure NHibernate to connect SQLite database files, take hsqlite.cfg as an example:

<?XML version= "1.0" encoding= "Utf-8"?><hibernate-configurationxmlns= "urn:nhibernate-configuration-2.2" >  <session-factoryname= "Nhibernate.test">    < Propertyname= "Current_session_context_class">Thread_static</ Property>    < Propertyname= "Connection.driver_class">NHibernate.Driver.SQLite20Driver</ Property>    < Propertyname= "Connection.connection_string"></ Property>    < Propertyname= "Adonet.batch_size">10</ Property>    < Propertyname= "Show_sql">True</ Property>    < Propertyname= "dialect">NHibernate.Dialect.SQLiteDialect</ Property>    < Propertyname= "Use_outer_join">True</ Property>    < Propertyname= "Command_timeout">60</ Property>    < Propertyname= "Hbm2ddl.auto">Update</ Property>    < Propertyname= "Query.substitutions">True=1;false=0</ Property>    < Propertyname= "Proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</ Property>    <MappingAssembly= "Inspectclient.domain"/>  </session-factory></hibernate-configuration>


Where Current_session_context_class is primarily used to configure the session type, the WinForm type uses the thread_static parameter, and the Web type uses Web parameters.

Sessionhelper

  

 Public  Static classSessionbuilder {Private Static ReadOnlyisessionfactory _sessionfactory; Private Static ReadOnly Object_lock =New Object(); StaticSessionbuilder () {Lock(_lock) {_sessionfactory=NewConfiguration (). Configure ("HSqlite.cfg.xml")                        . SetProperty ("connection.connection_string", Sqliteconfig.datasource).            Buildsessionfactory (); }        }        #regionSession operation in the current contextPrivate Static voidBindcontext () {Lock(_lock) {if(!Currentsessioncontext.hasbind (_sessionfactory))                {Currentsessioncontext.bind (_sessionfactory.opensession ()); }            }        }        Private Static voidUnbindcontext () {Lock(_lock) {if(Currentsessioncontext.hasbind (_sessionfactory)) {Currentsessioncontext.unbind (_se                Ssionfactory); }            }        }         Public Static voidclosecurrentsession () {unbindcontext (); }         Public StaticISession getcurrentsession () {bindcontext (); return_sessionfactory.getcurrentsession (); }        #endregion        #regionClose sessionfactory (typically at end of application) Public Static voidclosesessionfactory () {if(!_sessionfactory.isclosed)            {_sessionfactory.close (); }        }        #endregion        #regionOpen a new session Public StaticISession opensession () {Lock(_lock) {return_sessionfactory.opensession (); }        }        #endregion    }

Use NHibernate 2.1 to adapt to a SQLite database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.