我正在尝试让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中的所有内容都来自正常设置的数据库。
提前感谢您提供的任何帮助。
答案 0 :(得分:5)
根据Entity Framework 6 with SQLite 3 Code First - Won't create tables,EF6在与SQLite一起使用时不会创建表格,所以我必须自己这样做。
我能够使用DB Browser for SQLite创建一个SQLite数据库,并自己创建表格。我必须小心确保我创建的表的结构与我的Model类中的属性匹配,并使用Model类上的[Key]注释来指示哪个字段是主键字段。
重要的是,我必须在Visual Studio中添加现有项才能获取该文件,并确保Visual Studio知道该文件。
另外,重要的是,我必须转到该文件的属性并将其复制到输出目录&#39;属性为&#39;复制如果更新&#39;,以便数据库在我运行应用程序时进入bin / debug或bin / release目录。如果我没有这样做,数据库将不会在运行时存在,这将导致运行时崩溃。