使用SQLite测试EntityFramework

时间:2018-09-19 19:32:07

标签: c# entity-framework sqlite

我一直收到此错误,我不知道发生了什么事

System.NotSupportedException: 'Unable to determine the provider name for provider factory of type 'Microsoft.Data.Sqlite.SqliteFactory'. Make sure that the ADO.NET provider is installed or registered in the application config.'

这是我收到的代码:

        SqliteConnection connection = new SqliteConnection("DataSource=:memory:");
        connection.Open();

        using (MyDbContext context = new MyDbContext(connection))
        {
            var lst = context.Blogs.ToList();
        }

这是我的app.config

  <entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="mssqllocaldb" />
  </parameters>
</defaultConnectionFactory>
<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" />
  <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
</providers>
 </entityFramework>

  <system.data>
<DbProviderFactories>
  <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" />
  <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>

请帮助。这件事使我发疯。我需要在dbcontext中放入OnModelCreating的内容吗?

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。只需删除对“Microsoft.Data.Sqlite.SqliteFactory”的引用并添加“System.Data.Sqlite”。 Visual Studio 可以更改此引用并将其弄乱