2017-05-28 79 views
0

使用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?我似乎無法在這些地方找到任何文檔。

我是從合併的例子:

編輯:如果我更換db.Database.Migrate();db.Database.EnsureCreated();它的工作原理。從文檔中,Migrate()是相同的,但可以讓您創建表結構的更新,其中EnsureCreated()不更新。我很困惑。

回答

0

所以,

微軟有一個嚴重的問題,使體面的文件,但我沒有發現有有些過時文檔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是它爲您創建的類的名稱,並粘貼在項目底部的名爲Migrations的文件夾中。

現在當:

context.Database.Migrate(); 

運行時,一切都很好!

+0

下面是一些非常難以找到文件的微軟EF核心遷移:https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/migrations – bricelam

+0

@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中填充所有帖子。 –

+0

沒有諷刺。嚴重缺乏文檔,無法找到和瀏覽哪些文檔。 – bricelam

0

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

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的使用只要表格ID的默認值,因此在執行CRUD操作之後,它會失敗,因爲它無法比較/轉換/將LONG(64)轉換爲INT(32)。

+0

沒有幫助,感謝您的建議。 –