简体   繁体   English

创建表实体框架核心和SQLite

[英]Creating table Entity Framework Core and SQLite

Using Microsoft.EntityFrameworkCore.SQLite , I'm attempting to create a code level creation of a database, and add a simple row to a table. 我正在尝试使用Microsoft.EntityFrameworkCore.SQLite创建数据库的代码级创建,并将简单的行添加到表中。 I get the error, SQLite error: no such table Jumplists . 我收到错误, SQLite error: no such table Jumplists

From last to first, here are the classes 从最后到第一,这是课程

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

The DbContext class 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));
        }
    }
}

The JumpList class 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; }
    }
}

The other two classes aren't worth repeating here, and don't give errors. 其他两个类在这里不值得重复,也不要给出错误。

When I use the firefox sqlite extension to look at the data.sqlite file, none of my three tables are listed. 当我使用firefox sqlite扩展名查看data.sqlite文件时,没有列出我的三个表。

The command db.DataBase.Migrate says it 命令db.DataBase.Migrate表示

Applies any pending migrations for the context to the database. 将上下文的所有未决迁移应用到数据库。

What are pending migrations ? 什么是pending migrations I can't seem to find any documentation anywhere on these. 我似乎找不到关于这些文件的任何文档。

I'm combining examples from: 我合并了以下示例:

Edit: If I replace db.Database.Migrate(); 编辑:如果我替换db.Database.Migrate(); with db.Database.EnsureCreated(); 使用db.Database.EnsureCreated(); it works. 有用。 From the documentation, Migrate() is the same, but lets you create updates to the table structures, where EnsureCreated() does not. 从文档来看, Migrate()是相同的,但是可以让您创建对表结构的更新,而EnsureCreated()不能。 I'm confused. 我糊涂了。

Try this (worked for me in a project a few months ago, i don't remember why): 试试这个(几个月前在一个项目中为我工作,我不记得为什么了):

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

Also i had to use LONG instead of INT for classes ID because sqlite uses LONG as default for table ID, so after when you do a CRUD operation it fails because it can't compare/convert/cast LONG(64) to INT(32). 我也必须对类ID使用LONG而不是INT,因为sqlite使用LONG作为表ID的默认值,因此在执行CRUD操作后,它失败了,因为它无法将LONG(64)比较/转换/广播为INT(32) )。

So, 所以,

Microsoft has a serious issue making decent documentation, but I did find a site that has somewhat dated documentation for Learning Entity Framework Core , specifically migrations which is in the link. Microsoft在制作像样的文档方面遇到了一个严重问题,但是我确实找到了一个站点,该站点的文档有些过时,有关Learning Entity Framework Core ,尤其是链接中的迁移。

At the top, it mentions, 在最上方,它提到

If you have Visual Studio, you can use the Package Manager Console (PMC) to manage migrations. 如果您拥有Visual Studio,则可以使用程序包管理器控制台(PMC)来管理迁移。

Which led to the Package Manager Console page which states right at the top, that you need to have: 这导致进入Package Manager Console页面,该页面顶部显示,您需要:

If you want to use the Package Manager Console to execute migrations command, you need to ensure that the latest version of Microsoft.EntityFrameworkCore.Tools is added to your project.json file. 如果要使用程序包管理器控制台执行迁移命令,则需要确保将最新版本的Microsoft.EntityFrameworkCore.Tools添加到project.json文件中。

The problem is, there is no project.json file anywhere in my project (or solution). 问题是,我的项目(或解决方案)中的任何地方都没有project.json文件。 After some searching, I found that via NuGet, to add Microsoft.EntityFrameworkCore.Tools 经过一些搜索,我发现通过NuGet可以添加Microsoft.EntityFrameworkCore.Tools

Then via Tools > NuGet Package Manager > Package Manager Console I was able to run the add-migration InitialDatabases command. 然后,通过Tools > NuGet Package Manager > Package Manager Console我能够运行add-migration InitialDatabases命令。 The last part InitialDatabases is the name of the class it creates for you, and sticks in a folder called Migrations at the base of the project. 最后一部分InitialDatabases是它为您创建的类的名称,并将其InitialDatabases在项目基础的一个名为Migrations的文件夹中。

Now when: 现在,当:

context.Database.Migrate();

is run, all is well! 运行,一切都很好!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM