使用 System.Data.SQLite 和 Entity Framework 6 的简单示例 [英] Simple example using System.Data.SQLite with Entity Framework 6
问题描述
我正在尝试使用 SQLite 和 EF6 在控制台应用程序中获取一个简单的代码第一个示例,但是我遇到了多个错误:我在 VS 2015 中创建了一个新的控制台项目.然后通过 NuGet 安装 EF (6.1.3) 和 System.Data.SQLite (1.0.102).
I am trying to get a simple code first example to work in a console app using SQLite and EF6, however I am running into multiple errors: I created a new console project in VS 2015. Then install EF (6.1.3) and System.Data.SQLite (1.0.102) via NuGet.
尝试运行一个简单的程序:
Try to run a simple program:
namespace SQLiteConsole1
{
class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
class MyContext : DbContext
{
public DbSet<Person> Persons { get; set; }
}
class Program
{
static void Main(string[] args)
{
using (var db = new MyContext())
{
var person = new Person() { Name = "John" };
db.Persons.Add(person);
db.SaveChanges();
}
}
}
}
这是我的 App.Config 的样子:
This is what my App.Config looks like this:
<connectionStrings>
<add name="MyContext" connectionString="Data Source=C:TempTest.sqlite" providerName="System.Data.SQLite" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<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" />
</providers>
</entityFramework>
<system.data>
<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>
当我第一次运行这个程序时,我收到以下错误:
When I first run this program I get the following error:
未处理的异常:System.InvalidOperationException:无实体框架为具有不变名称的 ADO.NET 提供程序找到的提供程序'System.Data.SQLite'.确保提供程序已在应用程序配置文件的entityFramework"部分."
Unhandled Exception: System.InvalidOperationException: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SQLite'. Make sure the provider is registered in the 'entityFramework' section of the application config file."
所以我将 <provider invariantName="System.Data.SQLite.EF6"
改为 <provider invariantName="System.Data.SQLite"
,然后我得到这个错误:
So I change <provider invariantName="System.Data.SQLite.EF6"
to <provider invariantName="System.Data.SQLite"
, then I get this error:
未处理的异常:System.Data.Entity.Infrastructure.DbUpdateException:发生错误在更新条目时.有关详细信息,请参阅内部异常.System.Data.Entity.Core.UpdateException:发生错误更新条目.有关详细信息,请参阅内部异常.System.Data.SQLite.SQLiteException:SQL 逻辑错误或缺失数据库没有这样的表:人
Unhandled Exception: System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. 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: People
需要更改什么才能使这个简单的示例工作?
What needs to be changed to get this simple example working?
推荐答案
这里问了一个类似的问题:Entity Framework 6 with SQLite 3 Code First - 获胜不创建表
A similar question is asked over here: Entity Framework 6 with SQLite 3 Code First - Won't create tables
kjbartel 给出了非常有用的解释,即 EF SQLite 驱动程序不支持表创建.
kjbartel gives very useful explanation that table creation is not supported by the EF SQLite Driver.
另见 https://github.com/msallin/SQLiteCodeFirst,它提供了一个很好的解决方案.我安装了 SQLite.CodeFirst NuGet 包,并添加了以下代码,然后应用程序运行良好:
Also see https://github.com/msallin/SQLiteCodeFirst, which provides an excellent solution. I installed the SQLite.CodeFirst NuGet package, and added the below code, then the app works fine:
class MyContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var sqliteConnectionInitializer = new SqliteCreateDatabaseIfNotExists<MyContext>(modelBuilder);
Database.SetInitializer(sqliteConnectionInitializer);
}
public DbSet<Person> Persons { get; set; }
}
这篇关于使用 System.Data.SQLite 和 Entity Framework 6 的简单示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!