SQLite.CodeFirst“提供程序未返回ProviderManifest实例。”

问题描述 投票:0回答:2

我正在尝试使用SQLite.CodeFirst(1.5.1.25,从NuGet包安装)以及System.Data.SQLite(1.0.108,也是NuGet)来创建最简单的项目。 但是,添加一个人将导致

提供程序未返回ProviderManifest实例。

例外。可悲的是,没有官方的示例项目可以从中汲取灵感,所以以下是几十个SO答案的汇总让我遵循最小的例子。 context

class Person
{
   [Key]
   public int ID { get; set; }
   public string Name { get; set; }
}

class MyDBContentext : DbContext
{
    public MyDBContentext(string connectionString) 
           : base(new SQLiteConnection() { ConnectionString = connectionString }, true)
    { }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        var sqliteConnectionInitializer = new SqliteCreateDatabaseIfNotExists<MyDBContentext>(modelBuilder);
        System.Data.Entity.Database.SetInitializer(sqliteConnectionInitializer);
    }

    public DbSet<Person> People { get; set; }
}

//main
var conStr = $"Data Source=eftest.sqlite;Version=3;Password=123;";
var ctx = new MyDBContentext(conStr);
ctx.People.Add(new Person() { Name = "foo" });//<--- exception throw here
var res = ctx.SaveChanges();

App.config

  <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>
  <entityFramework>
    <providers>
      <provider invariantName="System.Data.SQLite.EF6"
          type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
      <provider invariantName="System.Data.SqlClient"
          type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="System.Data.SQLite"
          type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
    </providers>
  </entityFramework>

如果它具有任何相关性,还有一个内部异常(已翻译,因为它忽略了线程文化):

方法System.Data.SQLite.EF6.SQLiteProviderManifest.GetProviderManifestToken(System.String)尝试访问方法System.Data.SQLite.UnsafeNativeMethods.GetSettingValue(System.String,System.String)失败。

c# sqlite entity-framework-6 ef-code-first system.data.sqlite
2个回答
0
投票

重新安装所有NuGet包(SQLite.CodeFirst,System.Data.SQLite和Entity框架)并清理解决方案解决了该问题。


0
投票

对于将来遇到此问题的用户,请从GAC中删除所有SQLite程序集。就我而言,我必须使用管理员权限从命令提示符运行以下命令:

gacutil –u System.Data.SQLite
gacutil –u System.Data.SQLite.Linq
© www.soinside.com 2019 - 2024. All rights reserved.