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

标签 c# entity-framework sqlite visual-studio-2015

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

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

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>

当我第一次运行该程序时,出现以下错误:

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" ,然后我得到这个错误:

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

要使这个简单的示例正常运行,需要更改什么?

最佳答案

这里问了一个类似的问题: Entity Framework 6 with SQLite 3 Code First - Won't create tables

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

关于c# - 使用 System.Data.SQLite 和 Entity Framework 6 的简单示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38557170/

相关文章:

c# - "The model backing the ' DataContext ' context has changed"但我正在进行相同的迁移

C# 声称我的 SQLite 表不存在,但它确实存在

android - SQLiteDatabase 插入或替换(如果更改)

c# - 在运行时更改云服务 web.config

c# - 需要 C# 函数将灰度 TIFF 转换为黑白(单色/1BPP)TIFF

c# - EF Core 3.0 SumAsync 触发聚合函数异常

java - LIKE 正在处理 BLOB 类型,但 = 不工作

c# - 如何动态创建DataTemplate并为其分配StackPanel?

entity-framework - Entity Framework 、SQL Server CE 4.0 和 Code First 的启动性能问题

c# - 无法读取配置部分 'entityFramework',因为它缺少部分声明