Dot-Net

Entity Framework 6 with SQLite 3 Code First - 不會創建表

  • March 4, 2014

使用來自 NuGet 的最新版本的 EF6 和 SQLite。在 Stackoverflow 上發表了一些有用的文章後,我終於讓 app.config 文件工作了。現在的問題是,雖然數據庫是,但表並沒有被創建。

我的 app.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <configSections>
   <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
   <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
 </configSections>
 <startup>
   <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
 </startup>
 <entityFramework>
   <providers>
     <provider invariantName="System.Data.SQLite"
               type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
   </providers>
 </entityFramework>
 <system.data>
   <DbProviderFactories>
     <remove invariant="System.Data.SQLite" />
     <add name="SQLite Data Provider"
          invariant="System.Data.SQLite"
          description=".Net Framework Data Provider for SQLite"
          type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
     <remove invariant="System.Data.SQLite.EF6" />
     <add name="SQLite Data Provider (Entity Framework 6)"
          invariant="System.Data.SQLite.EF6"
          description=".Net Framework Data Provider for SQLite (Entity Framework 6)"
          type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
   </DbProviderFactories>
 </system.data>
 <connectionStrings>
   <add name="MyDBContext"
         connectionString="Data Source=|DataDirectory|MyDB.sqlite"
         providerName="System.Data.SQLite" />
 </connectionStrings>
</configuration>

我的簡單測試程序:

class Program
{
   static void Main(string[] args)
   {
       using (var db = new MyDBContext())
       {
           db.Notes.Add(new Note { Text = "Hello, world" });
           db.Notes.Add(new Note { Text = "A second note" });
           db.Notes.Add(new Note { Text = "F Sharp" });
           db.SaveChanges();
       }

       using (var db = new MyDBContext())
       {
           foreach (var note in db.Notes)
           {
               Console.WriteLine("Note {0} = {1}", note.NoteId, note.Text);
           }
       }

       Console.Write("Press any key . . . ");
       Console.ReadKey();
   }

   public class Note
   {
       public long NoteId { get; set; }
       public string Text { get; set; }
   }

   public class MyDBContext : DbContext
   {
       // default constructor should do this automatically but fails in this case
       public MyDBContext()
           : base("MyDBContext")
       {

       }
       public DbSet<Note> Notes { get; set; }

       protected override void OnModelCreating(DbModelBuilder modelBuilder)
       {
           modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
       }
   }
}

如果我手動創建表,則程序可以正常工作並且表會更新。如果我刪除數據庫,EF 會創建它但不創建表,並且程序在嘗試讀回數據時失敗,並顯示表不存在的錯誤消息。

有沒有人設法讓 Code First 與 EF6 一起工作?非常感謝您的幫助/指導,因為我現在完全卡住了!!!

謝謝大家。

不幸的是,EF6 提供程序實​​現System.Data.SQLite.EF6不支持創建表。我下載了 SQLite 原始碼進行查看,但找不到用於創建表和遷移的任何內容。EF6 提供者與他們的 Linq 實現基本相同,所以它的目的都是查詢數據庫而不是修改它。

我目前使用 SQL Server 完成所有工作,並使用SQL Server Compact & SQLite Toolbox為 SQLite 生成 sql 腳本。然後可以使用 執行腳本SQLiteCommand來模擬遷移。

更新

EF7中,對 SQL server compact 的支持已被刪除,EF 團隊正在開發一個新的 SQLite 提供程序。提供程序將使用 Microsoft 的託管 SQLite 包裝器項目,Microsoft.Data.SQLite而不是該System.Data.SQLite項目。這也將允許在 iOS、Android、Windows Phone/Mobile、Linux、Mac 等上使用 EF7,因為 Microsoft 的包裝器正在開發為可移植庫。

它仍處於測試階段,但如果您想看一看,可以從 MyGet( devmasterrelease)的 ASP.Net 開發提要中獲取 nuget 包。尋找EntityFramework.SQLite包裹。

編輯 (12.08.2021)

這篇文章/庫是關於 EF 6 而不是 EF Core。如果您使用 EF Core,則支持 SQLite 程式碼優先。


初始 (05.04.2015)

我從 Fried 的程式碼開始,並創建了一個讓您使用 CodeFirst 的項目。該項目可在GitHub上開源或以NuGet 包的形式提供。

如果您遇到任何問題或錯過某個功能,請隨時在 GitHub 上打開一個新問題。當然,PR 是非常受歡迎的。

編輯(26.04.2016):

與此同時,我在這個項目中做了很多。

支持以下(預設 EF)功能:

  • 類中的表(支持的註釋:表)
  • 屬性中的列(支持的註釋:Column、Key、MaxLength、Required、NotMapped、DatabaseGenerated、Index)
  • PrimaryKey 約束(支持鍵註解、鍵組合)
  • ForeignKey 約束(1-n 關係,支持’Cascade on delete’)
  • 非空約束
  • 自動遞增(一個 int PrimaryKey 會自動遞增)
  • 索引(使用索引屬性裝飾列。預設情況下會自動為外鍵創建索引。為了防止這種情況,您可以刪除對等 ForeignKeyIndexConvention)

還有一些 SQLite 獨有的功能,預設不支持:

  • 唯一約束(使用 UniqueAttribute 裝飾列,這是該庫的一部分)
  • Collat​​e 約束(使用 Collat​​eAttribute 裝飾列,這是該庫的一部分)

有兩種方法可以使用這個庫的功能。

  1. 使用 DbInitializers:
  • SqliteCreateDatabaseIfNotExists
  • SqliteDropCreateDatabaseAlways
  • SqliteDropCreateDatabaseWhenModelChanges
  1. 通過使用以下兩個類之一獲得更多控制:
  • SqliteSqlGenerator(基於 EdmModel 創建 SQL)
  • SqliteDatabaseCreator(基於 Database 和 DbModel 創建一個新的 SQLite 數據庫)

編輯(30.05.2020 和 20.03.2021): 由於 EF 6 現在支持 .NET Core 3 及更高版本,我調整了庫以支持 .NET Standard 2.1。支持以下 .NET 框架版本:

  • .NET 4.0(使用 net40)
  • .NET 4.5-4.8(使用 net45)
  • .NET Core 3.0-3.1(使用 netstandard2.1)
  • .NET 5(使用 netstandard2.1)

引用自:https://stackoverflow.com/questions/22174212