使用 Entity Framework 6 和 SQLite 的问题 [英] Problems using Entity Framework 6 and SQLite

查看:32
本文介绍了使用 Entity Framework 6 和 SQLite 的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将实体框架与 SQLite 结合使用.我在将它集成到我的主应用程序中遇到了问题,所以我从头开始了一个小测试,完全按照 http://brice-lambson.blogspot.com/2012/10/entity-framework-on-sqlite.html

I'm trying to use Entity Framework with SQLite. I had issues integrating it into my main application, so I started a little test from scratch, exactly following the directions on http://brice-lambson.blogspot.com/2012/10/entity-framework-on-sqlite.html

说到底,我在运行项目时出现如下错误:

After all is said and done, I get the following error when running the project:

未找到 ADO.NET 提供程序的实体框架提供程序不变名称System.Data.SQLite".确保提供者是在应用程序配置的entityFramework"部分注册文件.请参阅 http://go.microsoft.com/fwlink/?LinkId=260882 了解更多的信息.

No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SQLite'. Make sure the provider is registered in the 'entityFramework' section of the application config file. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.

我的 app.config 如下所示:

My app.config looks like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <add name="SQLite Data Provider"
            invariant="System.Data.SQLite"
            description="Data Provider for SQLite"
            type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
    </DbProviderFactories>
  </system.data>
  <connectionStrings>
    <add name="ChinookContext"
          connectionString=
"Data Source=|DataDirectory|Chinook_Sqlite_AutoIncrementPKs.sqlite"
          providerName="System.Data.SQLite" />
  </connectionStrings>
</configuration>

然后我看到了他关于 Entity Framework 6 的帖子.虽然这不是我得到的确切错误,但我尝试通过 NuGet 安装他更新的提供程序.错误消失了,但取而代之的是这个:

Then I saw his post about Entity Framework 6. While it wasn't the exact error I was getting, I tried installing his updated provider via NuGet. The error was gone, but replaced with this one:

无法加载文件或程序集System.Data.SQLite.Linq,版本=2.0.88.0,文化=中性,PublicKeyToken=db937bc2d44ff139' 或它的依赖项之一.定位的程序集的清单定义与程序集引用不匹配.(HRESULT 的例外:0x80131040)

Could not load file or assembly 'System.Data.SQLite.Linq, Version=2.0.88.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

此外,我的 app.config 已更改(略微)为:

Additionally, my app.config got changed (slightly) to this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.SQLiteProviderServices, System.Data.SQLite.Linq, Version=2.0.88.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <add name="SQLite Data Provider" invariant="System.Data.SQLite" description="Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
      <remove invariant="System.Data.SQLite" />
    </DbProviderFactories>
  </system.data>
  <connectionStrings>
    <add name="ChinookContext" connectionString="Data Source=|DataDirectory|Chinook_Sqlite_AutoIncrementPKs.sqlite" providerName="System.Data.SQLite" />
  </connectionStrings>
</configuration>

我已经尝试了所有我能想到的方法来解决这些错误,但没有任何效果.我试过使用其他 SQLite 二进制文件;我尝试手动编辑 SQLite 项目以使用 EF 版本 6;我改变了架构,我一遍又一遍地添加和删除了 nuget 包,等等.

I've tried everything I can think of to address these errors, nothing has worked. I've tried using the other SQLite binaries; I've tried manually editing the SQLite project to use the EF version 6; I've changed the architectures, I've added and removed the nuget packages over and over, etc.

我不知道从哪里开始.

推荐答案

根据magicandre1981 的评论,我开始更仔细地研究提供者节点的语法.我发现我的程序集与 type 属性中指定的版本不同,尽管我没有插入或触及该特定行.通过删除强命名,我得到了 .Net 来加载库.作为参考,这是新行:

Based on magicandre1981's comment, I began to look more closely at the syntax of the provider node. I found that my assembly was a different version than what was specified in the type attribute, though I had not inserted or touched that particular line. By deleting the strong naming, I got .Net to load the library. For reference, here's the new line:

<provider invariantName="System.Data.SQLite" type="System.Data.SQLite.SQLiteProviderServices, System.Data.SQLite.Linq" />

这让我回到了正轨,我能够将我的结果与博客上的结果相匹配.

That put me back on track and I was able to match my results with the ones on the blog.

然而,我不得不指出,我认为 SQLite 不适合实体框架,因为缺少太多关键功能.我切换到通过 NuGet 安装的 SQL Server Compact Edition.对我的连接字符串进行一个简单的调整,我就可以使用 Entity Framework 的全部功能运行.与 SQLite 需要花费数小时的时间相比,它只用了不到一分钟的时间.如果可能,我建议切换数据库,System.Data.SQLite 还没有为实体框架做好准备.

I feel compelled to note, however, that I have decided that SQLite is not a good fit for the Entity Framework, as too many critical functions are missing. I switched over to SQL Server Compact Edition, which I installed via NuGet. A simple tweak to my Connection String and I was running with the full power of Entity Framework. It took less than a minute, compared to the multi-hour slog that was SQLite. I'd recommend switching databases if possible, System.Data.SQLite just isn't ready for the Entity Framework.

这篇关于使用 Entity Framework 6 和 SQLite 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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