繁体   English   中英

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

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

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

尝试运行一个简单的程序:

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看起来像这样:

  <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>

当我第一次运行此程序时,我收到以下错误:

未处理的异常:System.InvalidOperationException:找不到具有不变名称“System.Data.SQLite”的ADO.NET提供程序的实体框架提供程序。 确保提供程序已在应用程序配置文件的“entityFramework”部分中注册。“

所以我将<provider invariantName="System.Data.SQLite.EF6"更改为<provider invariantName="System.Data.SQLite" ,然后我收到此错误:

未处理的异常:System.Data.Entity.Infrastructure.DbUpdateException:更新条目时发生错误。 有关详细信息,请参阅内部异常 System.Data.Entity.Core.UpdateException:更新条目时发生错误。 有关详细信息,请参阅内部异常 System.Data.SQLite.SQLiteException:SQL逻辑错误或缺少数据库没有这样的表:People

需要改变什么来使这个简单的例子有效?

这里提出了一个类似的问题: 实体框架6与SQLite 3代码优先 - 不会创建表

kjbartel提供了非常有用的解释,即EF SQLite驱动程序不支持表创建。

另请参阅https://github.com/msallin/SQLiteCodeFirst ,它提供了出色的解决方案。 我安装了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; }
    }

您需要使用模型的表初始化数据库。 注意错误“SQL逻辑错误或缺少数据库没有这样的表:People”。

这意味着你需要运行SQL来在数据库中创建相应的表,谢天谢地,如果在模型编辑器(* .edmx文件)中使用VS,在上下文菜单下,有一个选项让它自动生成SQL并执行它以根据模型为您在数据库中创建表条目。 注意:有时为非MS-SQL自动生成的问题可能需要在编译/运行之前手动修复。

暂无
暂无

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

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