NHibernate with Windows Application
Looking through StackOverflow this question seems to be asked a lot, but I have tried everything listed and can't seem to get any of them to work.
I currently trying to use NHibernate in a Windows Application written in C# on .NET 4.0. My currently platform target is x86, and I have confirmed that I'm using the x86 System.Data.SQLite, and I have set it up to copy locally. My current operating system is Windows 7 x64.
I also have an Assembly that is the one that uses NHibernate and talks with the database, and it is when I try to call into it that I get the following error:
************** Exception Text **************
NHibernate.HibernateException: Could not create the driver from NHibernate.Driver.SQLite20Driver. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> NHibernate.HibernateException: The IDbCommand and IDbConnection implementation in the assembly System.Data.SQLite could not be found. Ensure that the assembly System.Data.SQLite is located in the application directory or in the Global Assembly Cache. If the assembly is in the GAC, use <qualifyAssembly/> element in the application configuration file to specify the full name of the assembly.
at NHibernate.Driver.ReflectionBasedDriver..ctor(String driverAssemblyName, String connectionTypeName, String commandTypeName)
at NHibernate.Driver.SQLite20Driver..ctor()
--- End of inner exception stack trace ---
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at NHibernate.Bytecode.ActivatorObjectsFactory.CreateInstance(Type type)
at NHibernate.Connection.ConnectionProvider.ConfigureDriver(IDictionary`2 settings)
--- End of inner exception stack trace ---
at NHibernate.Connection.ConnectionProvider.ConfigureDriver(IDictionary`2 settings)
at NHibernate.Connection.ConnectionProvider.Configure(IDictionary`2 settings)
at NHibernate.Connection.ConnectionProviderFactory.NewConnectionProvider(IDictionary`2 settings)
at NHibernate.Cfg.SettingsFactory.BuildSettings(IDictionary`2 properties)
at NHibernate.Cfg.Configuration.BuildSettings()
at NHibernate.Cfg.Configuration.BuildSessionFactory()
at Surrix.Cerberus.YouthData.Respositories.NHibernateHelper.get_SessionFactory() in D:\development\Cerberus\YouthData\Respositories\NHibernateHelper.cs:line 20
at Surrix.Cerberus.YouthData.Respos开发者_如何学Citories.NHibernateHelper.OpenSession() in D:\development\Cerberus\YouthData\Respositories\NHibernateHelper.cs:line 29
at Surrix.Cerberus.YouthData.Respositories.YouthRepository.GetYouthByLastName(String lastName) in D:\development\Cerberus\YouthData\Respositories\YouthRepository.cs:line 41
at Surrix.Cerberus.YouthCheckinUI.YouthCheckinMainForm.searchButton_Click(Object sender, EventArgs e) in D:\development\Cerberus\YouthCheckinUI\YouthCheckinMainForm.cs:line 31
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
I can't seem to figure out exactly what to do here. If anyone has experienced this before please assist.
Below is my Hibernate Config file. Again this works when unit testing, but not when using it under a Windows Application:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.driver_class">NHibernate.Driver.SQLite20Driver</property>
<property name="connection.connection_string">
Data Source=D:\\development\\cerberus\\test.s3db;Version=3
</property>
<property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
<property name="query.substitutions">true=1;false=0</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
Check this SO article, looks like it's a problem with the specific target of the assembly referenced.
SQL.Data.SqlLite version with NHibernate 2.1
I have no clue why this works in a NUnit test, but not in a WPF application, but adding:
useLegacyV2RuntimeActivationPolicy="true"
to your startup tag in the app.config fixed my problem. My app.config now looks like:
<?xml version="1.0"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
精彩评论