SQLite + EF6“提供程序不支持DeleteDatabase。”

时间:2016-12-09 13:59:53

标签: entity-framework sqlite ef-code-first entity-framework-6

在开始之前,现有问题herehere无法解决我的问题。

我正在使用带有EF6的SQLite,使用包SQLite.CodeFirst来根据需要生成我的数据库模式。我在我的存储库类上运行单元测试,后者使用后端的DBContext,但是在运行每个测试之前需要清理数据库,以便后续测试不会被破坏。

我的问题是,当我调用dbContext.Database.Delete()时,我得到以下异常:

System.Data.Entity.Core.ProviderIncompatibleException was unhandled by user code
Message=DeleteDatabase is not supported by the provider.
Source=EntityFramework

我希望避免尝试必须发现数据库的位置并将其从磁盘中删除,因为位置因应用程序而异。有没有人有任何建议?

以下是我的代码相关部分的摘录:

的App.config

<entityFramework>
  <providers>
    <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6, Version=1.0.103.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
    <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>

<connectionStrings>
  <add name="EngineDataContext_LocalDb" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=Engine.DataModel.EngineDataContext;MultipleActiveResultSets=True;Integrated Security=True;App=EntityFramework" providerName="System.Data.SqlClient" />
  <add name="EngineDataContext_SQLServer" connectionString="Data Source=.;Initial Catalog=Engine.DataModel.EngineDataContext;MultipleActiveResultSets=True;Integrated Security=True;" providerName="System.Data.SqlClient" />
  <add name="EngineDataContext_SQLite" connectionString="Data Source=.\database.sqlite" providerName="System.Data.SQLite" />
</connectionStrings>

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

数据背景

public class EngineDataContext : DbContext
{
    public EngineDataContext()
        : base("name=EngineDataContext_SQLite")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        var sqliteConnectionInitializer = new SqliteDropCreateDatabaseAlways<EngineDataContext>(modelBuilder);
        Database.SetInitializer(sqliteConnectionInitializer);
    }
}

存储库

public class SqliteRepository
{
    private readonly EngineDataContext _dataContext;

    public SqliteRepository(EngineDataContext dataContext)
    {
        _dataContext = dataContext;
    }

    public void DropCreateDatabase()
    {
        _dataContext.Database.Delete(); // FAIL
        _dataContext.Database.Create();
    }
}

1 个答案:

答案 0 :(得分:0)

这意味着System.data.Sqlite EF提供程序未实现Deletedatabase方法(迁移的一部分,也未实现)。 Devart提供商(非免费)增加了适当的支持:https://www.devart.com/dotconnect/sqlite/features.html#ef