Xce*_*led 44 .net c# sqlite entity-framework entity-framework-6
我正在尝试使用SQLite实体框架.我将问题集成到我的主应用程序中时遇到了问题,因此我从头开始进行了一些测试,完全按照http://brice-lambson.blogspot.com/2012/10/entity-framework-on-sqlite.html上的说明进行操作.
毕竟说完了,运行项目时出现以下错误:
没有为ADO.NET提供程序找到具有不变名称"System.Data.SQLite"的实体框架提供程序.确保提供程序已在应用程序配置文件的"entityFramework"部分中注册.有关详细信息,请参阅http://go.microsoft.com/fwlink/?LinkId=260882.
我的app.config看起来像这样:
<?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>
Run Code Online (Sandbox Code Playgroud)
然后我看到他关于实体框架6的帖子.虽然这不是我得到的确切错误,但我尝试通过NuGet安装他更新的提供程序.错误消失了,但取而代之的是:
无法加载文件或程序集'System.Data.SQLite.Linq,Version = 2.0.88.0,Culture = neutral,PublicKeyToken = db937bc2d44ff139'或其依赖项之一.定位的程序集的清单定义与程序集引用不匹配.(HRESULT异常:0x80131040)
另外,我的app.config已经(略)改为:
<?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>
Run Code Online (Sandbox Code Playgroud)
我已经尝试了一切我能想到的来解决这些错误,没有任何效果.我尝试过使用其他SQLite二进制文件; 我尝试手动编辑SQLite项目以使用EF版本6; 我已经改变了架构,我一遍又一遍地添加和删除了nuget包等.
我不知道从哪里开始.
kjb*_*tel 57
我以为我会分享另一种方法,在不使用app.config
/的情况下使用SQLite配置EF6 web.config
.EF6现在支持基于代码的配置,如msdn所述.我使用了以下代码(由于Sly更新了删除反射):
public class SQLiteConfiguration : DbConfiguration
{
public SQLiteConfiguration()
{
SetProviderFactory("System.Data.SQLite", SQLiteFactory.Instance);
SetProviderFactory("System.Data.SQLite.EF6", SQLiteProviderFactory.Instance);
SetProviderServices("System.Data.SQLite", (DbProviderServices)SQLiteProviderFactory.Instance.GetService(typeof(DbProviderServices)));
}
}
Run Code Online (Sandbox Code Playgroud)
我使用这个,所以我可以注入正确的DbContext
,因此DbProvider
在运行时,不需要在主程序集中配置的所有内容.
编辑:
正如Reyn所说IDbConnectionFactory
,如果你希望你的web.config
/ app.config
file中有你的连接字符串,你还需要为SQLite 添加一个.另一种方法是调用一个不同的基本构造函数,DbContext
它传递一个新的SQLiteConnection
而不是连接字符串,如本答案所示.
Cle*_*man 30
我知道这是一个老问题,但似乎没有人提供实际使用.config文件的答案.这是我的web.config的system.data和entityframework部分,它们允许连接到SQLServer和Sqlite数据库:
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SQLite.EF6" />
<add name="SQLite Data Provider (Entity Framework 6)"
invariant="System.Data.SQLite.EF6"
description=".NET Framework Data Provider for SQLite (Entity Framework 6)"
type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
<remove invariant="System.Data.SQLite" />
<add name="SQLite Data Provider" invariant="System.Data.SQLite"
description=".Net Framework Data Provider for SQLite"
type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
</DbProviderFactories>
</system.data>
<entityFramework>
<providers>
<provider invariantName="System.Data.SQLite.EF6"
type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
<provider invariantName="System.Data.SqlClient"
type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="System.Data.SQLite"
type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
</providers>
</entityFramework>
Run Code Online (Sandbox Code Playgroud)
Xce*_*led 23
根据magicandre1981的评论,我开始更仔细地研究提供者节点的语法.我发现我的程序集与type属性中指定的版本不同,但我没有插入或触摸该特定行.通过删除强命名,我得到.Net来加载库.作为参考,这是新行:
<provider invariantName="System.Data.SQLite" type="System.Data.SQLite.SQLiteProviderServices, System.Data.SQLite.Linq" />
Run Code Online (Sandbox Code Playgroud)
这让我重回正轨,我能够将我的结果与博客上的结果相匹配.
但是,我不得不注意到,我已经确定SQLite不适合实体框架,因为缺少太多关键功能.我切换到我通过NuGet安装的SQL Server Compact Edition.对我的连接字符串的一个简单调整,我正在运行Entity Framework的全部功能.与SQLite的多小时口号相比,花了不到一分钟.我建议尽可能切换数据库,System.Data.SQLite还没有为Entity Framework做好准备.
Rey*_*eyn 15
对于任何使用基于代码的设置并仍然得到相同异常的人:我需要在kjbartel的答案中另外设置连接工厂.
public class SQLiteConnectionFactory : IDbConnectionFactory
{
public DbConnection CreateConnection(string nameOrConnectionString)
{
return new SQLiteConnection(nameOrConnectionString);
}
}
Run Code Online (Sandbox Code Playgroud)
然后在SQLiteConfiguration中设置默认连接工厂
public class SQLiteConfiguration : DbConfiguration
{
public SQLiteConfiguration()
{
SetDefaultConnectionFactory(new SQLiteConnectionFactory());
SetProviderFactory("System.Data.SQLite", SQLiteFactory.Instance);
SetProviderFactory("System.Data.SQLite.EF6", SQLiteProviderFactory.Instance);
Type t = Type.GetType( "System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6");
FieldInfo fi = t.GetField("Instance", BindingFlags.NonPublic | BindingFlags.Static);
SetProviderServices("System.Data.SQLite", (DbProviderServices)fi.GetValue(null));
}
}
Run Code Online (Sandbox Code Playgroud)