SQLite + EF6 "DeleteDatabase is not supported by the provider."

ef-code-first entity-framework entity-framework-6 sqlite

Question

Before I begin, existing questions here and here do not solve my problem.

I am using SQLite with EF6, with package SQLite.CodeFirst used to generate my db schema as required. I am running unit tests on my repository class which uses the DBContext on the backend, but the database needs to be cleaned up before running each test such that subsequent tests are not corrupted.

My problem is that when I call dbContext.Database.Delete() I get the following exception:

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

I want to avoid trying to having to discover the location of the database and deleting it from disk as the location varies depending on application. Has anybody got any suggestions?

Here are excerpts of relevant parts of my code:

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>

Data context

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);
    }
}

Repository

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
5/23/2017 11:45:43 AM

Popular Answer

It means that the System.data.Sqlite EF provider does not implement the Deletedatabase method (part of Migrations, which is not implemented either). The Devart provider (not free) adds proper support: https://www.devart.com/dotconnect/sqlite/features.html#ef

0
12/9/2016 2:09:37 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow