使用System的简单示例.数据SQLite与实体框架6

本文关键字:SQLite 实体 框架 数据 System 简单 使用 | 更新日期: 2023-09-27 17:58:23

我正在尝试使用SQLite和EF6在控制台应用程序中获得一个简单的代码优先示例,但我遇到了多个错误:我在VS 2015中创建了一个新的控制台项目。然后安装EF(6.1.3)和系统。数据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();
            }
        }
    }
}

这就是我的应用程序。配置如下:

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

当我第一次运行这个程序时,我得到以下错误:

未处理的异常:系统。InvalidOperationException:无实体框架找到ADO的提供程序。具有固定名称的NET提供程序’系统。数据SQLite"。请确保提供程序已在应用程序配置文件的"entityFramework"部分。"

所以我把<provider invariantName="System.Data.SQLite.EF6"改为<provider invariantName="System.Data.SQLite",然后我得到这个错误:

未处理的异常:系统数据实体基础设施DbUpdateException:发生错误同时更新条目。有关详细信息,请参阅内部异常。系统数据实体果心UpdateException:在更新条目。有关详细信息,请参阅内部异常。系统数据SQLite。SQLiteException:SQL逻辑错误或丢失数据库没有这样的表:人员

为了使这个简单的示例起作用,需要更改什么?

使用System的简单示例.数据SQLite与实体框架6

这里也提出了类似的问题:实体框架6与SQLite 3代码第一-赢得';t创建表格

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逻辑错误或缺少数据库没有这样的表:人员"。

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