【发布时间】:2014-03-12 13:03:27
【问题描述】:
有没有人获得新的 System.Data.SQLite 1.0.91.0 以在 Visual Studio 201# 中与实体框架 6 一起使用?如果有,你是怎么做到的?
更新 - 2014 年 3 月 20 日:System.Data.SQLite 1.0.92.0 已发布,但我没有运气在 VS2013 中创建 EDMX :( 我最终使用了包管理器(因为 EF6.#.# 是新的 SQLite NuGet 包):
uninstall-package entityframework -force
重启 VS2013 并安装旧版 EF5 以让 VS2013 从现有数据库生成 EDMX:
install-package entityframework -version 5.0.0
注意:这不是一个复杂的多表 SQLite 关系数据库测试,所以我不确定如果我确实使用具有多个导航 (FK) 关系的任何东西会出现什么其他问题:/
EDMX/Model First 的回答:(更新 - 2014 年 3 月 2 日)我找到了一种解决方法,但它不够一致,需要太多步骤才能将其视为有效的解决方案。基本上:
你制作了所有的 Class(es) 文件,
使用表连接到现有 SQLite 数据库,
-
将 web/app.config 修改为 misachkin 在仍然未完成的 SQLite 故障单 (http://system.data.sqlite.org/index.html/tktview?name=7b8fd3ef77) 中所描述的内容,包括伪造 cdsl/.ssdl/.msl 片段,以及
李> 然后在重新构建项目之前,在 Designer 中的 Empty EDMX 中手动创建所有实体模型,然后...
右键单击实体并选择“从数据库更新”...
有时 EF/VS2013 会添加 .tt/DbContesxt,有时不会。太“命中或错过”:(
有和没有现有 SQLite 数据库的“代码优先”的答案(基于 PMCB、Drexter 和 Jimi 的建议)。但是请注意,您无法在 Visual Studio 2013 中自动生成任何 EF 模型或 EDMX 文件 [原文 - 概念架构定义语言 (.CSDL)、存储架构定义语言 (.SSDL) 和映射规范语言 (.MSL)]。我做到了不要尝试手动创建 EDMX 文件以查看它们是否适合 EF,即使我这样做了,在我看来,进行所有手动创建/映射/更改/XML 编辑都会破坏基于实体的框架的整个目的/概念...
<connectionStrings>
<add name="DogsContext" connectionString="Data Source=|DataDirectory|\dogs.s3db;" providerName="System.Data.SQLite" />
</connectionStrings>
<entityFramework>
<providers>
<provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6, Version=1.0.91.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<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, Version=1.0.91.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
<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, Version=1.0.91.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
</DbProviderFactories>
</system.data>
这是测试类(请注意,我必须将 DogID 更改为 Int64 才能使其与 SQLite 一起使用...):
using System.Data.Entity;
namespace WebApplication1.Models
{
public class Dog
{
public Dog() { }
public Int64 DogID { get; set; }
public string DogName { get; set; }
}
}
这里是测试 DbContext:
using System.Data.Entity;
namespace WebApplication1.Models
{
public class DogsContext : DbContext
{
public DogsContext() : base() { }
public DbSet<Dog> DogNames { get; set; }
}
}
=============问题的原始细节===================
SQLite 1.0.91.0 昨天发布 (http://system.data.sqlite.org/index.html/doc/trunk/www/news.wiki),带有“添加对实体框架 6 的支持”。包含的“自述文件”中有一个警告,您需要将以下内容添加到 web.config/app.config 中:
<configuration>
<system.data>
<DbProviderFactories>
<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, Version=1.0.91.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
</DbProviderFactories>
</system.data>
</configuration>
如果您在 VS2013 中使用 NuGet 添加“System.Data.SQLite (x86/x64)”,您现在会将此行添加到 web.config/app.config:
<entityFramework>
...
<providers>
<provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
</providers>
...
</entityFramework>
作为测试,我复制了一个有效的 NET4.5.1/MVC4/EF5/System.Data.SQlite 1.0.90 应用程序并在其上运行 PM: update-package。它成功地将上述行添加到我的 web.config 中,并且我添加了所需的“DbProviderFactories”部分。重建并运行...失败并显示“未找到提供程序”。我在没有“DbProviderFactories”的情况下尝试它......失败并出现“未找到提供者”。我删除了新的“提供者”部分......失败并显示“未找到提供者”。所以我尝试了一个新项目,但有无身份(OWIN),以防万一出现问题。两者都不起作用......几乎没有想法,所以在 Google/StackOverflow 搜索后在这里询问。
============ 2014 年 2 月 14 日 ================
不幸的是,更改后的条目和许多变体在我的电脑上不起作用... 我在 Win8.1 Pro x64 上使用 VS 2013 Pro。 我重新安装了 System.Data.SQLite。 至少我在现有 SQLite 数据库和 VS2013 创建的新数据库(我可以访问并看到它具有零表的正确结构)都遇到了一个新错误:
"An error occurred connecting to the database. The database might be unavailable. An
exception of type 'System.Data.Entity.Core.ProviderIncompatibleException' occurred. The
error message is: Schema specified is not valid. Errors: StoreSchemaDefinition(2,64) :
Error 0175: The ADO.NET provider with invariant name 'System.Data.SQLite.EF6' is either
not registered in the machine or application config file, or could not be loaded. See the
inner exception for details.'"
EF6 和 System.Data.SQLite (x86/x64) 通过 NuGet 引入全新的 MVC Web 应用程序和 Windows 控制台应用程序。两者都产生了上述错误......我开始怀疑我的 VS3013 可能有问题,但是当我可以使用 MVC4/SQLite/EF5 时,我不想再花 6 个小时下载和修补它......
===== 2014 年 2 月 17 日 ========
@Jimi - 运气不好。我尝试使用带有新 MVC 和 Windows 应用程序项目的 NET 4.5 和 4.5.1。我尝试使用 EF6.0.0 和 EF6.0.2。从 NuGet 添加 "System.Data.SQLite (x86/x64)" 后,我尝试用安装 System.Data.SQLite 的本地副本替换 3x SQLite.Data.xxx.dll 引用。我还尝试了 NuGet “System.Data.SQLite.MSIL” 包,而不是将 DBFactories 添加到我尝试过的各种版本的 Web.Config 和 App.Config。
我还尝试创建一个 NET 4.0 MVC4 Web 应用程序,但现在 NuGet 的“System.Data.SQLite (x86/x64)”似乎包含对 EF6 的要求。怀着希望,我同意了它并尝试了各种“保存/创建新连接/编辑 web.config/etc”,但它不起作用。我现在已经放弃并使用 Datasets 回到 SQLite,因此我可以通过将 DataTables 设置为“AsEnumerable”(或 Java (jdbc))来将 Linq 与 System.Data.DataSetExtensions 一起用于想要在 Android 上离线复制数据库的人具有最小 App 界面的操作系统设备)。
========= 2014 年 2 月 19 日 =======
@Christian Sauer - 我没有让 EF6 和 System.Data.SQLite(或 System.Data.SQLite.EF6)一起工作,但是......但是
我刚刚在 http://system.data.sqlite.org/index.html/tktview?name=7b8fd3ef77 上发现了一个更新,它说有一个更新的 SQLite NuGet 包:
mistachkin added on 2014-02-19 03:39:35:
All NuGet packages have been updated to 1.0.91.3. Several fixes are included
that pertain to supporting Entity Framework 6.
我现在正在使用一个新项目(VS2013 中的 MVC Web 项目,使用面向 .NET 4.5.1 的 EF6.0.2 和 SQLite 1.0.91.3)对其进行测试...
新的 SQLite 1.0.91.3 又不走运了。 web.config 已更改为:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<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" />
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<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" />
<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" />
</DbProviderFactories>
</system.data>
我会在下班后尝试弄乱配置(如 Jimi 和 PCMB 的建议)并发布任何发现。
我刚刚尝试重新安装 System.Data.SQLite 的 32 位和 64 位版本(添加和不添加到 GAC),但 EF6 不适用于 SQLite。如果我尝试手动构建模型和映射,任何时候我引用/尝试使用 EF6 都会失败。至于创建实体数据模型(无论是从现有 SQLite 还是新 SQLite 数据库生成),无论我对配置、连接或提供程序做什么,它都会失败/出错。
=========== 2014 年 2 月 22 日 =============
他们将 System.Data.SQLite 1.0.91.3 更新拉/回滚到 1.0.91.0。仍然有一张未解决的票 (http://system.data.sqlite.org/index.html/tktview?name=7b8fd3ef77) 打开,其中包括 misachkin 的一些建议。这是 app.config misachkin 推荐尝试的(这个配置在我之前或之前对我都不起作用......):
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<system.data>
<DbProviderFactories>
<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, Version=1.0.91.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
<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, Version=1.0.91.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
</DbProviderFactories>
</system.data>
<connectionStrings>
<add name="northwindEFEntities" connectionString="metadata=res://*/NorthwindModel.EF6.2013.csdl|res://*/NorthwindModel.EF6.2013.ssdl|res://*/NorthwindModel.EF6.2013.msl;provider=System.Data.SQLite.EF6;provider connection string="data source=.\northwindEF.db"" providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
<providers>
<provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6, Version=1.0.91.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
</providers>
</entityFramework>
</configuration>
在我尝试了 misachkin 将 dll 添加到 GAC 的建议之后(使用 .NET "gacutil.exe" 完成)...
【问题讨论】:
-
你有进步吗?我也被这个问题困住了......
-
嘿,你终于明白了吗?请说出你的进展...
-
'EF6.anything' 还没有运气。对于 CRUD 应用程序,我一直在使用 EF5,对于查询,我要么使用基于 JDBC/ODBC SQL 的 DAL 并带有原始返回,要么构建适合 Linq 和 Razor 视图的对象。
-
如果这仍然是一个问题,也许我发布的这个答案对你有用(另见视频):stackoverflow.com/questions/25089346/…
-
22 Feb 解决方案终于对我有用了。竖起大拇指
标签: c# entity-framework sqlite