【发布时间】:2016-05-20 04:43:13
【问题描述】:
我正在尝试让 System.Data.SQLite 与 Entity Framework 6 一起使用,在我的 C#.NET (4.5.2) WPF 项目中使用 Code First 方法。
我已经查看并尝试按照以下位置(以及其他地方)中的说明进行操作,但它们似乎已经过时,对于不同的 dbms,而不是 Code First,或者上述的某种组合。
https://msdn.microsoft.com/en-us/data/jj592674
https://msdn.microsoft.com/en-us/data/jj592674.aspx
http://nullskull.com/a/10476742/sqlite-in-wpf-with-entity-framework-6.aspx
我想我终于正确设置了我的 App.config 文件,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<entityFramework>
<!-- from: https://stackoverflow.com/questions/14510096/entity-framework-6-sqlite -->
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
<provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
</providers>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
<connectionStrings>
<add name="MyDatabase" connectionString="Data Source=.\DataFile\myDatabase.sqlite" providerName="System.Data.SQLite" />
</connectionStrings>
<system.data>
<!-- from: https://stackoverflow.com/questions/14510096/entity-framework-6-sqlite -->
<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>
</configuration>
我的 DbContext 定义如下:
public class MyDataContext : DbContext
{
public MyDataContext() : base("name=MyDatabase")
{
}
public DbSet<DataItem> DataItems { get; set; }
}
我的DataItem类如下:
public class DataItem
{
[Key]
public int MyInt { get; set; }
public string MyString { get; set; }
}
我正在尝试这样调用上下文:
using (var db = new MyDataContext())
{
DataItem item = new DataItem { MyInt = 19, MyString = "nineteen" };
db.DataItems.Add(item);
try
{
db.SaveChanges();
}
catch (Exception ex)
{
var x = ex.InnerException;
Debug.WriteLine("Inner Exception: {0}", x);
throw;
}
}
正如我所料,当我实例化MyDataContext 时,myDatabase.sqlite 文件会在我的bin\debug\DataFile 目录中创建。但是,当我尝试调用db.SaveChanges() 时,会捕获到异常。
这个异常的InnerException是System.Data.Entity.Core.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SQLite.SQLiteException: SQL logic error or missing database
no such table: DataItems
当我查看 DataFile 目录时,myDatabase.sqlite 文件是零字节。这告诉我,尽管 DbContext 从 app.config 文件中找到了新数据库文件的正确文件名,但它并没有按照应有的方式在数据库中执行代码优先创建表。
我在这里有什么明显的遗漏吗?这些示例都假设这个(极其关键的)步骤有效,并且 EF6 中的所有内容都来自一个正常工作的设置数据库。
提前感谢您为此提供的任何帮助。
【问题讨论】:
-
我删除了我的答案,因为它不正确,但发现了这个问题:stackoverflow.com/questions/22174212/… 显然 EF6 SQLite 已损坏。
-
另外,您使用的是 SQLite 还是 MySQL?您似乎在不同的地方引用了两者。
-
sqlite... 抱歉,我有几次错误地将其输入为 MySQL。已编辑!
-
谢谢你的链接,我去看看!
标签: c# wpf sqlite ef-code-first entity-framework-6