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