使用Microsoft.EntityFrameworkCore.SQLite
,我試圖創建一個數據庫的代碼級別創建,並添加一個簡單的行到表。我收到錯誤SQLite error: no such table Jumplists
。創建表實體框架核心和SQLite
從後到前,這裏是班
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));
}
}
}
跳轉表類
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
?我似乎無法在這些地方找到任何文檔。
我是從合併的例子:
- https://docs.microsoft.com/en-us/ef/core/get-started/netcore/new-db-sqlite
- https://blogs.msdn.microsoft.com/dotnet/2016/09/29/implementing-seeding-custom-conventions-and-interceptors-in-ef-core-1-0/
編輯:如果我更換db.Database.Migrate();
與db.Database.EnsureCreated();
它的工作原理。從文檔中,Migrate()
是相同的,但可以讓您創建表結構的更新,其中EnsureCreated()
不更新。我很困惑。
下面是一些非常難以找到文件的微軟EF核心遷移:https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/migrations – bricelam
@bricelam我看了「入門「這一頁,因爲這不是我當時想要的。綁定WCF和EFC,我繼續前進。很高興知道。我認爲你是諷刺的,但請查看官方文檔[入門](https://docs.microsoft.com/en-us/ef/core/get-started/)和[帶有WPF和.Net的EF核心](https://docs.microsoft.com/en-us/ef/core/get-started/full-dotnet/),你會明白我的意思。他們甚至不包含關閉的博客示例。如果您修復它,請添加綁定帖子,在組合框中選擇博客,並在代碼和xaml中填充所有帖子。 –
沒有諷刺。嚴重缺乏文檔,無法找到和瀏覽哪些文檔。 – bricelam