NHibernate与Windows应用程序 [英] NHibernate with Windows Application

查看:106
本文介绍了NHibernate与Windows应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过StackOverflow来看这个问题似乎有很多问题,但是我已经尝试了列出的所有内容,但似乎无法使它们中的任何一个起作用.

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.

我目前正在尝试在.NET 4.0上用C#编写的Windows应用程序中使用NHibernate.我当前的平台目标是x86,并且已经确认我正在使用x86 System.Data.SQLite,并且已将其设置为本地复制.我当前的操作系统是Windows 7 x64.

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.

我也有一个使用NHibernate并与数据库对话的程序集,当我尝试调用该程序集时,出现以下错误:

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.Respositories.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.

下面是我的Hibernate Config文件.再次适用于单元测试,但不适用于Windows应用程序:

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>

推荐答案

我不知道为什么它可以在NUnit测试中工作,但不能在WPF应用程序中工作,而是添加:

I have no clue why this works in a NUnit test, but not in a WPF application, but adding:

useLegacyV2RuntimeActivationPolicy="true"

在app.config中的启动标记中解决了我的问题.我的app.config现在看起来像:

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>

这篇关于NHibernate与Windows应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆