简体   繁体   English

使用System.Data.SQLite和Entity Framework 6的简单示例

[英]Simple example using System.Data.SQLite with Entity Framework 6

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. 我正在尝试使用SQLite和EF6在控制台应用程序中使用简单的代码第一个示例,但是我遇到了多个错误:我在VS 2015中创建了一个新的控制台项目。然后安装EF(6.1.3)和System。 Data.SQLite(1.0.102)通过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();
            }
        }
    }
}

This is what my App.Config looks like this: 这就是我的App.Config看起来像这样:

  <connectionStrings>
    <add name="MyContext" connectionString="Data Source=C:\Temp\Test.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: 当我第一次运行此程序时,我收到以下错误:

Unhandled Exception: System.InvalidOperationException: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SQLite'. 未处理的异常:System.InvalidOperationException:找不到具有不变名称“System.Data.SQLite”的ADO.NET提供程序的实体框架提供程序。 Make sure the provider is registered in the 'entityFramework' section of the application config file." 确保提供程序已在应用程序配置文件的“entityFramework”部分中注册。“

So I change <provider invariantName="System.Data.SQLite.EF6" to <provider invariantName="System.Data.SQLite" , then I get this error: 所以我将<provider invariantName="System.Data.SQLite.EF6"更改为<provider invariantName="System.Data.SQLite" ,然后我收到此错误:

Unhandled Exception: System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while updating the entries. 未处理的异常:System.Data.Entity.Infrastructure.DbUpdateException:更新条目时发生错误。 See the inner exception for details. 有关详细信息,请参阅内部异常 System.Data.Entity.Core.UpdateException: An error occurred while updating the entries. System.Data.Entity.Core.UpdateException:更新条目时发生错误。 See the inner exception for details. 有关详细信息,请参阅内部异常 System.Data.SQLite.SQLiteException: SQL logic error or missing database no such table: People System.Data.SQLite.SQLiteException:SQL逻辑错误或缺少数据库没有这样的表:People

What needs to be changed to get this simple example working? 需要改变什么来使这个简单的例子有效?

A similar question is asked over here: Entity Framework 6 with SQLite 3 Code First - Won't create tables 这里提出了一个类似的问题: 实体框架6与SQLite 3代码优先 - 不会创建表

kjbartel gives very useful explanation that table creation is not supported by the EF SQLite Driver. kjbartel提供了非常有用的解释,即EF SQLite驱动程序不支持表创建。

Also see https://github.com/msallin/SQLiteCodeFirst , which provides an excellent solution. 另请参阅https://github.com/msallin/SQLiteCodeFirst ,它提供了出色的解决方案。 I installed the SQLite.CodeFirst NuGet package, and added the below code, then the app works fine: 我安装了SQLite.CodeFirst NuGet包,并添加了以下代码,然后该应用程序正常工作:

    class MyContext : DbContext
    {
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            var sqliteConnectionInitializer = new SqliteCreateDatabaseIfNotExists<MyContext>(modelBuilder);
            Database.SetInitializer(sqliteConnectionInitializer);
        }
        public DbSet<Person> Persons { get; set; }
    }

You need to initialize your database with the tables for your models. 您需要使用模型的表初始化数据库。 Notice the error "SQL logic error or missing database no such table: People". 注意错误“SQL逻辑错误或缺少数据库没有这样的表:People”。

That means you need to run SQL to create the corresponding tables in the database, thankfully, if using VS, under the context menu while in the model editor (*.edmx files), there is an option to have it auto-generate the SQL and execute it to create the table entries in the database for you based on the model. 这意味着你需要运行SQL来在数据库中创建相应的表,谢天谢地,如果在模型编辑器(* .edmx文件)中使用VS,在上下文菜单下,有一个选项让它自动生成SQL并执行它以根据模型为您在数据库中创建表条目。 Note: sometimes the auto-generated for non MS-SQL can have issues that need to be manually fixed before it will compile/run. 注意:有时为非MS-SQL自动生成的问题可能需要在编译/运行之前手动修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM