【问题标题】:Creating table Entity Framework Core and SQLite创建表 Entity Framework Core 和 SQLite
【发布时间】:2017-10-28 16:18:47
【问题描述】:

使用Microsoft.EntityFrameworkCore.SQLite,我正在尝试创建数据库的代码级创建,并向表中添加一个简单的行。我收到错误消息,SQLite error: no such table Jumplists

从最后到第一,这里是班级

using JumpList_To_Clipboard.Data.Tables;
using Microsoft.EntityFrameworkCore;

namespace JumpList_To_Clipboard.Data
{
    public class DataSQLite : IData
    {
        public const string DATABASE = "data.sqlite";

        public DataSQLite()
        {
            using (var db = new SQLiteDbContext(DATABASE))
            {
                // Ensure database is created with all changes to tables applied
                db.Database.Migrate();

                db.JumpLists.Add(new JumpList { Name = "Default" });
                db.SaveChanges(); // Exception thrown here
            }
        }
    }
}

DbContext 类

using JumpList_To_Clipboard.Data.Tables;
using Microsoft.EntityFrameworkCore;

namespace JumpList_To_Clipboard.Data
{
    class SQLiteDbContext : DbContext
    {
        readonly string db_path;

        public DbSet<JumpList> JumpLists { get; set; }
        public DbSet<Group> Groups { get; set; }
        public DbSet<Item> Items { get; set; }

        public SQLiteDbContext(string database) : base()
        {
            db_path = database;
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite(string.Format("Data Source={0}", db_path));
        }
    }
}

JumpList 类

using System.Collections.Generic;

namespace JumpList_To_Clipboard.Data.Tables
{
    public class JumpList
    {
        public int JumpListId { get; set; }
        public string Name { get; set; }

        public List<Group> Groups { get; set; }
        public List<Item> Items { get; set; }
    }
}

另外两个类不值得在这里重复,也不要报错。

当我使用 firefox sqlite 扩展查看data.sqlite 文件时,我的三个表都没有列出。

命令db.DataBase.Migrate 这么说

将上下文的所有挂起迁移应用到数据库。

pending migrations 是什么?我似乎在这些的任何地方都找不到任何文档。

我正在结合以下示例:

编辑:如果我将 db.Database.Migrate(); 替换为 db.Database.EnsureCreated(); 就可以了。从文档中,Migrate() 是相同的,但允许您创建表结构的更新,而 EnsureCreated() 没有。我很困惑。

【问题讨论】:

    标签: sqlite entity-framework-core


    【解决方案1】:

    试试这个(几个月前在一个项目中为我工作过,我不记得为什么了):

     public virtual DbSet<JumpList> JumpLists { get; set; }
     public virtual DbSet<Group> Groups { get; set; }
     public virtual DbSet<Item> Items { get; set; }
    

    我还必须使用 LONG 而不是 INT 作为类 ID,因为 sqlite 使用 LONG 作为表 ID 的默认值,所以在执行 CRUD 操作后它会失败,因为它无法比较/转换/转换 LONG(64) 到INT(32)。

    【讨论】:

      【解决方案2】:

      所以,

      Microsoft 在制作体面的文档方面存在严重问题,但我确实找到了一个网站,其中包含 Learning Entity Framework Core 的有些过时的文档,特别是链接中的迁移。

      在顶部,它提到,

      如果您有 Visual Studio,则可以使用包管理器控制台 (PMC) 来管理迁移。

      这导致了Package Manager Console 页面,该页面位于顶部,您需要:

      如果您想使用包管理器控制台执行迁移命令,您需要确保将最新版本的Microsoft.EntityFrameworkCore.Tools 添加到您的project.json 文件中。

      问题是,我的项目(或解决方案)中的任何地方都没有 project.json 文件。经过一番搜索,我发现通过NuGet,添加Microsoft.EntityFrameworkCore.Tools

      然后通过Tools &gt; NuGet Package Manager &gt; Package Manager Console 我能够运行add-migration InitialDatabases 命令。最后一部分 InitialDatabases 是它为您创建的类的名称,并位于项目基础的名为 Migrations 的文件夹中。

      现在什么时候:

      context.Database.Migrate();
      

      运行,一切顺利!

      【讨论】:

      • 以下是 Microsoft 极难找到的关于 EF Core 迁移的文档:docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/migrations
      • @bricelam 我查看了其中的“入门”页面,因为这不是我当时想要的。绑定 WCF 和 EFC,我继续前进。很高兴知道。我认为您是在讽刺,但请查看官方文档 Getting StartedEF Core with WPF and .Net,您就会明白我的意思。它们甚至不包含未引用的博客示例。如果你修复它,添加绑定帖子,在组合框中选择博客,并在代码和 xaml 中填充所有帖子。
      • 没有讽刺意味。文档严重缺乏,哪些文档无法找到和导航。
      猜你喜欢
      • 2020-07-10
      • 1970-01-01
      • 1970-01-01
      • 2016-10-25
      • 2017-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多