gpt4 book ai didi

c# - 使用 Entity Framework 6 和 SQLite 的问题

转载 作者:IT王子 更新时间:2023-10-29 03:55:03 26 4
gpt4 key购买 nike

我正在尝试将 Entity Framework 与 SQLite 结合使用。我在将它集成到我的主应用程序中时遇到了问题,所以我完全按照 http://brice-lambson.blogspot.com/2012/10/entity-framework-on-sqlite.html 上的说明从头开始了一些测试。

总而言之,运行项目时出现以下错误:

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 看起来像这样:

<?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 安装他更新的提供程序。错误消失了,但取而代之的是:

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 已(略微)更改为:

<?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 包,等等。

我不知道从这里去哪里。

最佳答案

只是想分享另一种使用 SQLite 配置 EF6 的方法,而无需使用 app.config/web.config。 EF6 现在支持基于代码的配置,如此处所述on 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)));
}
}

我使用它以便在运行时注入(inject)正确的 DbContextDbProvider,并且不需要在主程序集中配置所有内容。

编辑:

作为Reyn said如果您希望在您的 web.config/app.config 文件中包含您的连接字符串,您还需要为 SQLite 添加一个 IDbConnectionFactory。另一种方法是从您的 DbContext 调用不同的基本构造函数,它传入一个新的 SQLiteConnection 而不是连接字符串,如 in this answer 所示。 .

关于c# - 使用 Entity Framework 6 和 SQLite 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20460357/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com