簡體   English   中英

創建表實體框架核心和SQLite

[英]Creating table Entity Framework Core and SQLite

我正在嘗試使用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()不能。 我糊塗了。

試試這個(幾個月前在一個項目中為我工作,我不記得為什么了):

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

我也必須對類ID使用LONG而不是INT,因為sqlite使用LONG作為表ID的默認值,因此在執行CRUD操作后,它失敗了,因為它無法將LONG(64)比較/轉換/廣播為INT(32) )。

所以,

Microsoft在制作像樣的文檔方面遇到了一個嚴重問題,但是我確實找到了一個站點,該站點的文檔有些過時,有關Learning Entity Framework Core ,尤其是鏈接中的遷移。

在最上方,它提到

如果您擁有Visual Studio,則可以使用程序包管理器控制台(PMC)來管理遷移。

這導致進入Package Manager Console頁面,該頁面頂部顯示,您需要:

如果要使用程序包管理器控制台執行遷移命令,則需要確保將最新版本的Microsoft.EntityFrameworkCore.Tools添加到project.json文件中。

問題是,我的項目(或解決方案)中的任何地方都沒有project.json文件。 經過一些搜索,我發現通過NuGet可以添加Microsoft.EntityFrameworkCore.Tools

然后,通過Tools > NuGet Package Manager > Package Manager Console我能夠運行add-migration InitialDatabases命令。 最后一部分InitialDatabases是它為您創建的類的名稱,並將其InitialDatabases在項目基礎的一個名為Migrations的文件夾中。

現在,當:

context.Database.Migrate();

運行,一切都很好!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM