Entity Framework 6 with SQLite 3 Code First - 不会创建表 [英] Entity Framework 6 with SQLite 3 Code First - Won't create tables

查看:31
本文介绍了Entity Framework 6 with SQLite 3 Code First - 不会创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用来自 NuGet 的最新版本的 EF6 和 SQLite.在 Stackoverflow 上发表了一些有用的帖子后,我终于让 app.config 文件工作了.现在的问题是,尽管数据库已创建,但并未创建表.

Using latest versions of EF6 and SQLite from NuGet. I finally got the app.config file to work after some useful posts on Stackoverflow. Now the problem is that the tables are not being created although the database is.

我的 app.config:

My 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>

我的简单测试程序:

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

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

If I create the table manually the program works fine and the table is updated. If I delete the database, EF creates it but doesn't create the table and the program fails when it attempts to read back the data with an error message that the table does not exist.

有没有人设法让 Code First 与 EF6 一起工作?非常感谢这方面的帮助/指导,因为我现在完全卡住了!!!

Has anyone managed to get Code First working with EF6 yet? Would appreciate help/guidance on this as I'm now completely stuck!!!

谢谢大家.

推荐答案

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

Unfortunately, the EF6 provider implementation in System.Data.SQLite.EF6 doesn't support creating tables. I downloaded the SQLite source code to have a look but couldn't find anything for creating tables and for migrations. The EF6 provider is basically the same as their Linq implementation so it's all aimed at querying the database rather than modifying it.

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

I currently do all of my work with SQL Server and generate sql scripts for SQLite using the SQL Server Compact & SQLite Toolbox. The scripts can then be run using an SQLiteCommand to simulate migrations.

更新

EF7 中,已取消对 SQL server compact 的支持,EF 团队正在开发新的 SQLite 提供程序.提供程序将使用 Microsoft 的托管 SQLite 包装器项目 Microsoft.Data.SQLite 而不是System.Data.SQLite 项目.这也将允许在 iOS、Android、Windows Phone/Mobile、Linux、Mac 等上使用 EF7,因为 Microsoft 的包装器正在开发为便携式库.

In EF7 support for SQL server compact has been dropped and a new provider for SQLite is being developed by the EF team. The provider will use Microsoft's managed SQLite wrapper project, Microsoft.Data.SQLite rather than the System.Data.SQLite project. This will also allow for using EF7 on iOS, Android, Windows Phone / Mobile, Linux, Mac etc. as Microsoft's wrapper is being developed as a portable library.

一切仍处于测试阶段,但您可以从 MyGet (dev,大师发布) 如果你想看看.查找 EntityFramework.SQLite 包.

It's all still in beta but you can get nuget packages from the ASP.Net development feeds at MyGet (dev, master, release) if you wish to have a look. Look for the EntityFramework.SQLite package.

这篇关于Entity Framework 6 with SQLite 3 Code First - 不会创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆