使用System.Data.SQLite实体框架6简单的例子 [英] Simple example using System.Data.SQLite with Entity Framework 6

查看:1895
本文介绍了使用System.Data.SQLite实体框架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:\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:

未处理的异常:System.InvalidOperationException:没有实体框架提供发现与不变名称'System.Data.SQLite'ADO.NET提供程序确保供应商在的的的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."

所以我换<供应商invariantName =System.Data.SQLite.EF6<供应商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?

推荐答案

一个类似的问题,被要求在这里:
实体框架6使用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实体框架6简单的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆