使用 FluentNHibernate 的问题 + SQLite 与 .NET4?
我有一个使用 Nhibernate 和 FluentNHibernate + SQLite 与 VS2010 .Net3.5 一起运行的 WPF 应用程序,并且一切正常。
现在我想更改为使用.Net4,但这已经变成了比我预想的更痛苦的经历。在设置连接时,我这样做:
var cfg = Fluently.Configure().
Database(SQLiteConfiguration.Standard.ShowSql().UsingFile("MyDb.db")).
Mappings(m => m.FluentMappings.AddFromAssemblyOf<MappingsPersistenceModel>());
_sessionFactory = cfg.BuildSessionFactory();
BuildSessionFactory()调用抛出一个FluentConfigurationException,说:
创建 SessionFactory 时使用了无效或不完整的配置。检查 PotentialReasons 集合和 InnerException 了解更多详细信息。
内部异常为我们提供了更多信息:
无法从 NHibernate.Driver.SQLite20Driver、NHibernate、Version=2.1.2.4000、Culture=neutral、PublicKeyToken=aa95f207798dfdb4 创建驱动程序。
还有进一步的内部异常:
无法找到程序集 System.Data.SQLite 中的 IDbCommand 和 IDbConnection 实现。确保程序集 System.Data.SQLite 位于应用程序目录或全局程序集缓存中。如果程序集位于 GAC 中,请使用应用程序配置文件中的 element 指定程序集的全名。
现在 - 对我来说,听起来好像找不到 System.Data.SQLite.dll,但我无法理解这一点。在任何引用此内容的地方,我都有“复制本地”,并且我已经验证它位于使用 SQLite 的项目的每个构建文件夹中。我还手动将其复制到解决方案的每个调试文件夹中 - 没有运气。
注意:
- 这与我升级到 .Net4 之前运行良好的代码完全相同。
- 我之前确实看到了一些 x64 x86 不匹配问题,但我已转而使用 x86 作为目标平台和所有引用的 dll。我已验证 Debug 文件夹中的所有文件都是 x86。
- 我尝试过预编译的Fluent dll,我尝试过自己编译,并且我使用.Net4编译了我自己的Fluent版本。
- 我看到还有其他人也看到了这个问题 ,但我还没有真正看到任何解决方案。
在 @devio 的回答之后,我尝试添加对 SQLite dll 的引用。这并没有改变任何东西,但我希望我做对了。这是我添加到 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>
有人成功地使用 Fluent 与 .Net4 和 SQLite 吗?帮助!我迷路了...
I have a WPF application running with VS2010 .Net3.5 using Nhibernate with FluentNHibernate + SQLite, and all works fine.
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:
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:
An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more details.
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.
And further InnerException:
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 element in the application configuration file to specify the full name of the assembly.
Now - to me it sounds like it doesn't find System.Data.SQLite.dll
, but I can't understand this. Everywhere this is referenced I have "Copy Local", and I have verified that it is in every build folder for projects using SQLite. I have also copied it manually to every Debug folder of the solution - without luck.
Notes:
- This is exactly the same code that worked just fine before I upgraded to .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. I have verified that all files in the Debug-folder are x86.
- I have tried the precompiled Fluent dlls, I have tried compiling myself, and I have compiled my own version of Fluent using .Net4.
- 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. 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:
<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? Help! I'm lost...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
是的先生:让它与 .NET 4 - Fluent NHibernate - NUnit 一起使用
通过使用上面帖子中的信息,我设法让它在 Visual Studio 2010 下工作
并在我的 NUnit 库中进行测试,在调试模式下为任何 CPU 进行编译。
首先,我不断遇到与你们中的一些人经历的相同的异常:
我已使用 NUGet 下载 1.0.76.0 版本的 SQLite for v4.0.30319 of .NET。上面的 Devios 帖子和他的摘要链接让我开始在 App.config 文件中测试 assemblyBinding - 告诉 VS 2010 对 NHibernate 使用正确版本的 System.Data.SQLite。这是我在 App.Config 中放入的内容:
我如何知道要在 App.config 中放入哪些详细信息?
我之前还从 SQLite 网站的下载部分安装了 SQLite .NET 4 版本的静态链接库。这会将 SQLite 库安装到我的计算机上,并将其添加到全局程序集缓存中 - 进而为我提供使用 Visual Studio 命令提示符 2010 编辑 App.config 所需的信息:
通常我会使用 .NET Reflector from RedGate 来提取以下信息我之前的 NUget 添加了 System.Data.SQLite dll 文件,但是如果您已经在系统中安装了 SQLite(正确的版本),那么 gacutil -l System.Data.SQLite 就可以了。
之前我也遇到过混合模式装配异常:
然后,如果我想使用 .NET 2.0 版本的 SQLite,我会将以下内容插入到我的 App.config 中:
但现在我有 .NET 4 版本的 SQLite,我不需要运行旧的 .NET 2 版本,在传统模式下 - 使用进程内并行运行时主机激活进一步避免潜在冲突 - Google MSDN:
感谢大家为我指明了正确的方向!
希望从现在开始我们之后的其他人能够更容易地解决这个问题。
Yes sir: Got it to work with .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.
First I kept getting the same exception as some of you experience:
I've used NUGet to download the 1.0.76.0 version of SQLite for v4.0.30319 of .NET. 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. Here's what I put in my App.Config:
How I knew which details to put in the 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. 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:
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.Previously I also got the mixed mode assembly exception:
Then if I wanted to use the .NET 2.0 version of SQLIte I'd insert the following to my App.config:
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:
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.
解决方案:
测试设置:
选择主机
选择在 64 位机器上的 64 位进程中运行测试
Solution:
Test settings:
choose hosts
choose run tests in 64 bit process on 64 bit machine
我尝试了此页面上的所有解决方案,但没有任何效果。然后我卸载了 64 位版本并使用了 Nuget 的 x86 版本,一切正常。
下午>安装包System.Data.SQLite.x86
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.
PM> Install-Package System.Data.SQLite.x86
当我尝试 Fluent with .Net4 和 SQLite 时,我也得到了同样的错误消息,但是当我更仔细地观察时,我发现了不同的错误消息。
无法加载类型 System.Data.SQLite.SQLiteConnection、System.Data.SQLite。
System.IO.FileLoadException:混合模式程序集是针对运行时版本“v2.0.50727”构建的,如果没有附加配置信息,则无法在 4.0 运行时中加载。
所以我所做的是添加 useLegacyV2RuntimeActivationPolicy="true"像这样的“启动”标签。
我不需要在“运行时”标签内添加 dependentAssembly 或任何内容。根据此 链接文本 和此链接文本,应仅用于迁移帮助。所以希望 SQLite 能够尽快更新。
希望这有帮助!
卡尔金
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.
Could not load type 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.
So what I did is to add useLegacyV2RuntimeActivationPolicy="true" to the "startup" tag like this.
I don't need to add dependentAssembly or anything inside the "runtime" tag. According to this link text and this link text, it should be used for migration aid only. So hopefully, SQLite will be updated soon.
Hope this helps!
Karlkim
检查 System.Data 引用的版本。在我看来,System.Data.SqlLite 找不到构建它的 IDbCommand 和 IDbConnection 的版本,我怀疑是版本 2.0.0.0。我怀疑现在您已经升级到 .Net 4,您正在引用 System.Data 版本 4.0.0.0。
如果是这种情况,您应该能够添加绑定重定向来解决问题:
Check the version of your System.Data reference. 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. I suspect that now you've upgraded to .Net 4 you are referencing System.Data version 4.0.0.0.
If this is the case you should be able to add a binding redirect to resolve the problem:
我在 NH、VS2010、.Net4 上遇到了类似的问题,但是在 Oracle ODP.Net 驱动程序和 32 位上。
解决方案是在 web.config 文件中声明一个具有明确版本号的“合格程序集”。请参阅我的 摘要。
也许这个解决方案也适用于您的问题。
I had a similar problem with NH, VS2010, .Net4, but with the Oracle ODP.Net drivers and 32bit.
The solution was to declare a "qualified assembly" in the web.config file with an explicit version number. See my summary.
Maybe this solution applies to your problem as well.
我通过在项目调试设置中将目标平台更改为 x86 来解决它。我用的是vs2010。
I managed to resolve it by changing the target platform to x86 in project debug setting. I am using vs2010.