[英]Entity Framework 6 not creating tables in SQLite database
我正在嘗試讓System.Data.SQLite與Entity Framework 6一起使用,使用我的C#.NET(4.5.2)WPF項目中的Code First方法。
我已經查看並嘗試按照以下地方(以及其他地方)中的說明進行操作,但它們似乎已過時,對於不同的dbms,不是Code First,或上述的某些組合。
https://msdn.microsoft.com/en-us/data/jj592674
https://msdn.microsoft.com/en-us/data/jj592674.aspx
http://nullskull.com/a/10476742/sqlite-in-wpf-with-entity-framework-6.aspx
我想我終於正確設置了我的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.2" />
</startup>
<entityFramework>
<!-- from: https://stackoverflow.com/questions/14510096/entity-framework-6-sqlite -->
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
<provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
</providers>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
<connectionStrings>
<add name="MyDatabase" connectionString="Data Source=.\DataFile\myDatabase.sqlite" providerName="System.Data.SQLite" />
</connectionStrings>
<system.data>
<!-- from: https://stackoverflow.com/questions/14510096/entity-framework-6-sqlite -->
<DbProviderFactories>
<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" />
<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" />
</DbProviderFactories>
</system.data>
</configuration>
我的DbContext定義如下:
public class MyDataContext : DbContext
{
public MyDataContext() : base("name=MyDatabase")
{
}
public DbSet<DataItem> DataItems { get; set; }
}
我的DataItem類如下:
public class DataItem
{
[Key]
public int MyInt { get; set; }
public string MyString { get; set; }
}
而我正試圖像這樣調用上下文:
using (var db = new MyDataContext())
{
DataItem item = new DataItem { MyInt = 19, MyString = "nineteen" };
db.DataItems.Add(item);
try
{
db.SaveChanges();
}
catch (Exception ex)
{
var x = ex.InnerException;
Debug.WriteLine("Inner Exception: {0}", x);
throw;
}
}
正如我所料,當我實例化MyDataContext
, myDatabase.sqlite
文件在我的bin\\debug\\DataFile
目錄中創建。 但是,當我嘗試調用db.SaveChanges()
,會捕獲異常。
此異常的InnerException是System.Data.Entity.Core.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SQLite.SQLiteException: SQL logic error or missing database no such table: DataItems
System.Data.Entity.Core.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SQLite.SQLiteException: SQL logic error or missing database no such table: DataItems
當我查看DataFile目錄時,myDatabase.sqlite文件是零字節。 這告訴我,盡管DbContext從app.config文件中找到了新數據庫文件的正確文件名,但它沒有按照預期在數據庫中執行其Code First創建表。
有什么明顯的東西我在這里不見了嗎? 這些示例都假設這個(非常關鍵)步驟有效,EF6中的所有內容都來自正常設置的數據庫。
在此先感謝您提供的任何幫助。
根據實體框架6與SQLite 3 Code First - 不會創建表 ,EF6在與SQLite一起使用時不會創建表,所以我必須自己這樣做。
我能夠使用DB Browser for SQLite創建一個SQLite數據庫,並自己創建表格。 我必須小心確保我創建的表的結構與我的Model類中的屬性匹配,並使用Model類上的[Key]注釋來指示哪個字段是主鍵字段。
重要的是,我必須在Visual Studio中添加現有項以獲取該文件並確保Visual Studio知道該文件。
此外,重要的是,我必須轉到該文件的屬性並將其“復制到輸出目錄”屬性設置為“復制如果更新”,以便數據庫進入bin / debug或bin / release目錄時運行應用程序。 如果我不這樣做,數據庫將不會在運行時存在,這將導致運行時崩潰。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.