实体框架6不在SQLite数据库中创建表

Kyl*_*eld 7 c# sqlite wpf ef-code-first entity-framework-6

我正在尝试让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

实体框架6 + SQLite

我想我终于正确设置了我的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>
Run Code Online (Sandbox Code Playgroud)

我的DbContext定义如下:

public class MyDataContext : DbContext
{
    public MyDataContext() :  base("name=MyDatabase")
    {
    }
    public DbSet<DataItem> DataItems { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我的DataItem类如下:

public class DataItem
{
    [Key]
    public int MyInt { get; set; }
    public string MyString { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

而我正试图像这样调用上下文:

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;
    }

}
Run Code Online (Sandbox Code Playgroud)

正如我所料,当我实例化时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文件中找到了新数据库文件的正确文件名,但它没有按照预期在数据库中执行其Code First创建表.

有什么明显的东西我在这里不见了吗?这些示例都假设这个(非常关键)步骤有效,EF6中的所有内容都来自正常设置的数据库.

在此先感谢您提供的任何帮助.

Kyl*_*eld 5

根据实体框架6与SQLite 3 Code First - 不会创建表,EF6在与SQLite一起使用时不会创建表,所以我必须自己这样做.

我能够使用DB Browser for SQLite创建一个SQLite数据库,并自己创建表格.我必须小心确保我创建的表的结构与我的Model类中的属性匹配,并使用Model类上的[Key]注释来指示哪个字段是主键字段.

重要的是,我必须在Visual Studio中添加现有项以获取该文件并确保Visual Studio知道该文件.

此外,重要的是,我必须转到该文件的属性并将其"复制到输出目录"属性设置为"复制如果更新",以便数据库进入bin/debug或bin/release目录时运行应用程序.如果我不这样做,数据库将不会在运行时存在,这将导致运行时崩溃.

  • 你需要这个:https://github.com/msallin/SQLiteCodeFirst - 它是关于链接的问题:/sf/answers/2062247911/ (3认同)