实体框架6与SQLite 3代码优先 - 不会创建表

use*_*887 40 .net sqlite system.data.sqlite ef-code-first entity-framework-6

使用NuGet的EF6和SQLite的最新版本.我终于得到app.config文件在Stackoverflow上的一些有用的帖子后工作.现在问题是虽然数据库是,但是没有创建表.

我的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" />
  </startup>
  <entityFramework>
    <providers>
      <provider invariantName="System.Data.SQLite"
                type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
    </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>
  <connectionStrings>
    <add name="MyDBContext"
          connectionString="Data Source=|DataDirectory|MyDB.sqlite"
          providerName="System.Data.SQLite" />
  </connectionStrings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

我的简单测试程序:

class Program
{
    static void Main(string[] args)
    {
        using (var db = new MyDBContext())
        {
            db.Notes.Add(new Note { Text = "Hello, world" });
            db.Notes.Add(new Note { Text = "A second note" });
            db.Notes.Add(new Note { Text = "F Sharp" });
            db.SaveChanges();
        }

        using (var db = new MyDBContext())
        {
            foreach (var note in db.Notes)
            {
                Console.WriteLine("Note {0} = {1}", note.NoteId, note.Text);
            }
        }

        Console.Write("Press any key . . . ");
        Console.ReadKey();
    }

    public class Note
    {
        public long NoteId { get; set; }
        public string Text { get; set; }
    }

    public class MyDBContext : DbContext
    {
        // default constructor should do this automatically but fails in this case
        public MyDBContext()
            : base("MyDBContext")
        {

        }
        public DbSet<Note> Notes { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我手动创建表,程序工作正常,表更新.如果我删除数据库,EF会创建它,但不会创建表,并且程序在尝试使用表不存在的错误消息读回数据时会失败.

有没有人设法让Code First与EF6合作?非常感谢帮助/指导,因为我现在完全陷入困境!

谢谢大家.

msa*_*lin 52

我从Fried的代码开始,创建了一个允许您使用CodeFirst的项目.该项目在GitHubNuGet Package上提供开源.

如果您遇到任何问题或错过功能,请随时在GitHub上打开一个新问题.当然PR很受欢迎.

编辑(2016年4月26日):

与此同时,我在这个项目中做了很多.

支持以下(默认EF)功能:

  • 类中的表(支持的注释:表)
  • 属性中的列(支持的注释:Column,Key,MaxLength,Required,NotMapped,DatabaseGenerated,Index)
  • PrimaryKey约束(密钥注释,支持密钥复合)
  • ForeignKey约束(1-n关系,支持'删除时级联')
  • 不是Null约束
  • 自动递增(int PrimaryKey将自动递增)
  • 索引(使用Index属性装饰列.默认情况下会自动为外键创建索引.为了防止这种情况,您可以删除对话ForeignKeyIndexConvention)

SQLite还有一些独有的功能,默认情况下不支持:

  • 唯一约束(使用UniqueAttribute装饰列,这是此库的一部分)
  • 整理约束(使用Collat​​eAttribute装饰列,这是此库的一部分)

有两种方法可以使用此库的功能.

  1. 使用DbInitializers:

    • SqliteCreateDatabaseIfNotExists
    • SqliteDropCreateDatabaseAlways
    • SqliteDropCreateDatabaseWhenModelChanges
  2. 通过使用以下两个类之一获得更多控制:

    • SqliteSqlGenerator(基于EdmModel创建SQL)
    • SqliteDatabaseCreator(基于Database和DbModel创建一个新的SQLite数据库)

  • 楼主你真棒!我花了很长时间试图让 SQLite 和 EF6 工作,却发现它不会创建数据库。这让我很开心:) 也谢谢@fried! (2认同)

kjb*_*tel 40

不幸的是,EF6提供程序实现System.Data.SQLite.EF6不支持创建表.我下载了SQLite源代码以查看但无法找到任何用于创建表和迁移的内容.EF6提供程序与它们的Linq实现基本相同,因此它们都旨在查询数据库而不是修改它.

我目前正在使用SQL Server完成所有工作,并使用SQL Server Compact和SQLite工具箱为SQLite生成sql脚本.然后可以使用SQLiteCommand模拟迁移来运行脚本.

更新

EF7中,对SQL服务器的支持已经被删除,并且EF团队正在开发一个新的SQLite提供程序.提供程序将使用Microsoft的托管SQLite包装器项目,Microsoft.Data.SQLite而不是System.Data.SQLite项目.这也将允许在iOS,Android,Windows Phone/Mobile,Linux,Mac等上使用EF7,因为Microsoft的包装器正在开发为便携式库.

它仍然处于测试阶段,但是如果你想看一下,你可以从MyGet(dev,master,release)的ASP.Net开发源获得nuget包.寻找EntityFramework.SQLite包裹.

  • 是的,我走了同样的路线.我现在正在将应用程序移动到Windows 8.1商店应用程序,它根本不支持EF!唤醒MS,我们确实需要在Windows应用商店应用上提供适当的ORM支持,即使它仅适用于SQLite.要提供EF提供的所有变更跟踪和交易支持,真是太痛苦了. (7认同)

fri*_*ied 14

我决定编写自己的基础数据库初始化程序来解决这个问题.

你可以在这里查看:https: //gist.github.com/flaub/1968486e1b3f2b9fddaf

它支持:

  • 多对多关系
  • 代码优先数据注释如:
    • [键]
    • [需要]
    • [指数]
    • [ForeignKey的]