简体   繁体   English

使用FluentNHibernate + SQLite与.NET4的问题?

[英]Problems using FluentNHibernate + SQLite with .NET4?

I have a WPF application running with VS2010 .Net3.5 using Nhibernate with FluentNHibernate + SQLite, and all works fine. 我有一个WPF应用程序运行VS2010 .Net3.5使用Nhibernate与FluentNHibernate + SQLite,一切正常。

Now I want to change to use .Net4, but this has turned into a more painful experience then I expected.. When setting up the connection I do this: 现在我想改为使用.Net4,但这已经变成了一种比我想象的更痛苦的经历..在设置连接时我这样做:

var cfg = Fluently.Configure().
    Database(SQLiteConfiguration.Standard.ShowSql().UsingFile("MyDb.db")).
    Mappings(m => m.FluentMappings.AddFromAssemblyOf<MappingsPersistenceModel>());
_sessionFactory = cfg.BuildSessionFactory();                    

The BuildSessionFactory() call throws a FluentConfigurationException saying: BuildSessionFactory()调用抛出FluentConfigurationException:

An invalid or incomplete configuration was used while creating a SessionFactory. 创建SessionFactory时使用了无效或不完整的配置。 Check PotentialReasons collection, and InnerException for more details. 检查PotentialReasons集合和InnerException以获取更多详细信息。

The inner exception gives us more information: 内部异常为我们提供了更多信息:

Could not create the driver from NHibernate.Driver.SQLite20Driver, NHibernate, Version=2.1.2.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4. 无法从NHibernate.Driver.SQLite20Driver,NHibernate,Version = 2.1.2.4000,Culture = neutral,PublicKeyToken = aa95f207798dfdb4创建驱动程序。

And further InnerException: 而且还有InnerException:

The IDbCommand and IDbConnection implementation in the assembly System.Data.SQLite could not be found. 无法找到程序集System.Data.SQLite中的IDbCommand和IDbConnection实现。 Ensure that the assembly System.Data.SQLite is located in the application directory or in the Global Assembly Cache. 确保程序集System.Data.SQLite位于应用程序目录或全局程序集缓存中。 If the assembly is in the GAC, use element in the application configuration file to specify the full name of the assembly. 如果程序集位于GAC中,请使用应用程序配置文件中的元素指定程序集的全名。

Now - to me it sounds like it doesn't find System.Data.SQLite.dll , but I can't understand this. 现在 - 听起来它听起来没有找到System.Data.SQLite.dll ,但我无法理解这一点。 Everywhere this is referenced I have "Copy Local", and I have verified that it is in every build folder for projects using SQLite. 引用的所有地方我都有“复制本地”,我已经验证它是在使用SQLite的项目的每个构建文件夹中。 I have also copied it manually to every Debug folder of the solution - without luck. 我还手动将它复制到解决方案的每个Debug文件夹 - 没有运气。

Notes: 笔记:

  • This is exactly the same code that worked just fine before I upgraded to .Net4. 在升级到.Net4之前,这是完全相同的代码。
  • I did see some x64 x86 mismatch problems earlier, but I have switched to use x86 as the target platform and for all referenced dlls. 我之前确实看到了一些x64 x86不匹配问题,但我已经切换到使用x86作为目标平台和所有引用的dll。 I have verified that all files in the Debug-folder are x86. 我已经验证Debug-folder中的所有文件都是x86。
  • I have tried the precompiled Fluent dlls, I have tried compiling myself, and I have compiled my own version of Fluent using .Net4. 我已经尝试过预编译的Fluent dll,我已经尝试过编译自己,并且我已经使用.Net4编译了我自己的Fluent版本。
  • I see that there are also others that have seen this problem , but I haven't really seen any solution yet. 我看到还有其他人已经看到了这个问题 ,但我还没有真正看到任何解决方案。

After @devio's answer I tried adding a reference to the SQLite dll. 在@ devio的回答后,我尝试添加对SQLite dll的引用。 This didn't change anything, but I hope I made it right though.. This is what I added to the root node of the app.config file: 这没有改变任何东西,但我希望我做对了..这是我添加到app.config文件的根节点的内容:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <qualifyAssembly partialName="System.Data.SQLite" fullName="System.Data.SQLite, Version=1.0.60.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
    </assemblyBinding>
</runtime>

Anyone out there using Fluent with .Net4 and SQLite successfully? 有没有人使用Fluent成功使用.Net4和SQLite? Help! 救命! I'm lost... 我迷路了...

I also got the same error message when I tried Fluent with .Net4 and SQLite, but when I looked more closely, I found different error message. 当我尝试使用.Net4和SQLite时,我也得到了同样的错误信息,但是当我仔细观察时,我发现了不同的错误信息。

Could not load type System.Data.SQLite.SQLiteConnection, System.Data.SQLite. 无法加载类型System.Data.SQLite.SQLiteConnection,System.Data.SQLite。 System.IO.FileLoadException: Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information. System.IO.FileLoadException:混合模式程序集是针对运行时的版本“v2.0.50727”构建的,如果没有其他配置信息,则无法在4.0运行时中加载。

So what I did is to add useLegacyV2RuntimeActivationPolicy="true" to the "startup" tag like this. 所以我做的是将useLegacyV2RuntimeActivationPolicy =“true”添加到“startup”标签中。

<startup useLegacyV2RuntimeActivationPolicy="true">
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>

I don't need to add dependentAssembly or anything inside the "runtime" tag. 我不需要在“runtime”标记内添加dependentAssembly或任何内容。 According to this link text and this link text , it should be used for migration aid only. 根据此链接文本和此链接文本 ,它应仅用于迁移辅助。 So hopefully, SQLite will be updated soon. 所以希望SQLite很快就会更新。

Hope this helps! 希望这可以帮助! Karlkim Karlkim

Check the version of your System.Data reference. 检查System.Data参考的版本。 It sounds to me like System.Data.SqlLite can't find the version of IDbCommand and IDbConnection that it was built with which I suspect is version 2.0.0.0. 听起来像System.Data.SqlLite一样,找不到它所构建的IDbCommand和IDbConnection的版本,我怀疑它是版本2.0.0.0。 I suspect that now you've upgraded to .Net 4 you are referencing System.Data version 4.0.0.0. 我怀疑现在您已升级到.Net 4,您正在引用System.Data版本4.0.0.0。

If this is the case you should be able to add a binding redirect to resolve the problem: 如果是这种情况,您应该能够添加绑定重定向来解决问题:

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
        <assemblyIdentity name="System.Data" publicKeyToken="b77a5c561934e089"/>
        <bindingRedirect oldVersion="2.0.0.0" newVersion="4.0.0.0"/>
    </dependentAssembly>
</assemblyBinding>

I had a similar problem with NH, VS2010, .Net4, but with the Oracle ODP.Net drivers and 32bit. 我与NH,VS2010,.Net4有类似的问题,但是使用了Oracle ODP.Net驱动程序和32位。

The solution was to declare a "qualified assembly" in the web.config file with an explicit version number. 解决方案是在web.config文件中声明一个带有显式版本号的“限定程序集”。 See my summary . 看我的总结

Maybe this solution applies to your problem as well. 也许这个解决方案也适用于您的问题。

I managed to resolve it by changing the target platform to x86 in project debug setting. 我设法通过在项目调试设置中将目标平台更改为x86来解决它。 I am using vs2010. 我正在使用vs2010。

Yes sir: Got it to work with .NET 4 - Fluent NHibernate - NUnit 是的先生:使用.NET 4 - Fluent NHibernate - NUnit

By using the information from the above posts I managed to get it working under Visual Studio 2010 and in my NUnit library for testing, compiled for Any CPU in debug mode. 通过使用上述帖子中的信息,我设法让它在Visual Studio 2010和我的NUnit库中工作以进行测试,在调试模式下为任何CPU编译。

First I kept getting the same exception as some of you experience: 首先,我一直得到与你们一些经历相同的例外:

Could not create the driver from NHibernate.Driver.SQLite20Driver, NHibernate, Version=3.1.0.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4. 无法从NHibernate.Driver.SQLite20Driver,NHibernate,Version = 3.1.0.4000,Culture = neutral,PublicKeyToken = aa95f207798dfdb4创建驱动程序。

I've used NUGet to download the 1.0.76.0 version of SQLite for v4.0.30319 of .NET. 我已经使用NUGet下载.NET的v4.0.30319的SQLite的1.0.76.0版本。 Devios post above and his summary link got me into testing assemblyBinding in my App.config file - telling VS 2010 to use the correct version of System.Data.SQLite for NHibernate. 上面的Devios帖子和他的摘要链接让我在我的App.config文件中测试assemblyBinding - 告诉VS 2010为NHibernate使用正确版本的System.Data.SQLite。 Here's what I put in my App.Config: 这是我在App.Config中的内容:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <qualifyAssembly partialName="System.Data.SQLite" 
      fullName="System.Data.SQLite, Version=1.0.76.0, 
      Culture=neutral, 
      PublicKeyToken=db937bc2d44ff139"/>
    </assemblyBinding>
  </runtime>

How I knew which details to put in the App.config ? 我怎么知道在App.config中放入哪些细节?

I've also previously installed the statically linked library of SQLite's .NET 4 version from the SQLite website under their download section. 我之前也在SQLite网站的下载部分安装了静态链接的SQLite .NET 4版本库。 This installed the SQLite library to my computer and added it to the Global Assembly Cache - in turn giving me the information I needed to edit the App.config using the Visual Studio Command Prompt 2010: 这将SQLite库安装到我的计算机并将其添加到全局程序集缓存中 - 从而为我提供了使用Visual Studio命令提示符2010编辑App.config所需的信息:

C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC>gacutil -l System.Data.SQLite Microsoft (R) .NET Global Assembly Cache Utility. C:\\ Program Files(x86)\\ Microsoft Visual Studio 10.0 \\ VC> gacutil -l System.Data.SQLite Microsoft(R).NET全局程序集缓存实用程序。 Version 4.0.30319.1 Copyright (c) Microsoft Corporation. 版本4.0.30319.1版权所有(c)Microsoft Corporation。 All rights reserved. 版权所有。

The Global Assembly Cache contains the following assemblies: 全局程序集缓存包含以下程序集:
System.Data.SQLite, Version=1.0.76.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=AMD64 System.Data.SQLite,Version = 1.0.76.0,Culture = neutral,PublicKeyToken = db937bc2d44ff139,processorArchitecture = AMD64

Number of items = 1 物品数量= 1

Usually I would use .NET Reflector from RedGate for extracting the information of dll's of my previously NUget added System.Data.SQLite dll file, but the gacutil -l System.Data.SQLite is ok if you've already installed SQLite to your system, the correct version that is. 通常我会使用RedGate的.NET Reflector来提取我之前添加的NUget System.Data.SQLite dll文件的dll信息,但是如果你已经在系统中安装了SQLite,那么gacutil -l System.Data.SQLite就可以了。 ,正确的版本。

Previously I also got the mixed mode assembly exception: 以前我也有混合模式汇编异常:

Could not load type System.Data.SQLite.SQLiteConnection, System.Data.SQLite. 无法加载类型System.Data.SQLite.SQLiteConnection,System.Data.SQLite。 System.IO.FileLoadException: Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information. System.IO.FileLoadException:混合模式程序集是针对运行时的版本“v2.0.50727”构建的,如果没有其他配置信息,则无法在4.0运行时中加载。

Then if I wanted to use the .NET 2.0 version of SQLIte I'd insert the following to my App.config: 然后,如果我想使用.NET 2.0版本的SQLIte,我会将以下内容插入到我的App.config中:

 <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>

But now that I have a .NET 4 version of SQLite I do not need to run the old .NET 2 version and in legacy mode - avoiding potential conflicts further on using In-Process Side-by-Side Runtime Host Activation - Google MSDN: 但是现在我有一个.NET4版本的SQLite,我不需要运行旧的.NET 2版本和传统模式 - 避免使用进程内并行运行时主机激活的潜在冲突 - Google MSDN:

When a newer version of the .NET Framework CLR loaded an assembly that was built against a previous version of the CLR, compatibility issues could occur, and the application could stop working. 当较新版本的.NET Framework CLR加载了针对以前版本的CLR构建的程序集时,可能会出现兼容性问题,并且应用程序可能会停止工作。 This could happen for any managed assembly, both in full applications and in plug-ins (where the managed assemblies run in the context of a host application). 对于任何托管程序集,无论是在完整应用程序中还是在插件中(托管程序集在宿主应用程序的上下文中运行),都可能发生这种情况。 The only way to guarantee that a newer version of the .NET Framework did not affect existing applications was to have all existing managed applications run in their original target compiled .NET Framework version. 保证较新版本的.NET Framework不会影响现有应用程序的唯一方法是让所有现有托管应用程序在其原始目标编译.NET Framework版本中运行。

Thanks to all you guys for pointing me in the right direction ! 感谢你们所有人指出我正确的方向! Hopefully it will be easier for others after us to fix this issue from now on. 希望从现在开始,我们之后的其他人更容易解决这个问题。

I tried every solution on this page and nothing worked. 我在这个页面上尝试了所有解决方案,但没有任 Then I uninstalled my 64bit version and used the x86 version from Nuget and everything worked. 然后我卸载了我的64位版本并使用了Nuget的x86版本,一切正常。

PM> Install-Package System.Data.SQLite.x86 PM> Install-Package System.Data.SQLite.x86

Could not create the driver from NHibernate.Driver.SQLite20Driver, NHibernate, Version=2.1.2.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4 无法从NHibernate.Driver.SQLite20Driver,NHibernate,Version = 2.1.2.4000,Culture = neutral,PublicKeyToken = aa95f207798dfdb4创建驱动程序

Solution: 解:

Test settings: 测试设置:
choose hosts 选择主机
choose run tests in 64 bit process on 64 bit machine 在64位机器上选择64位进程运行测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM