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

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

Question

I should note that the answers to the questions here and here do not resolve my issue.

I'm using the SQLite package and EF6 with SQLite. My database schema was generated by CodeFirst as needed. My repository class utilises the DBContext as the backend, and I'm running unit tests on it. However, the database has to be cleaned up before each test so that further tests won't be tainted.

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

Since the location fluctuates depending on the programme, I don't want to have to attempt to locate the database and delete it from disc. Anyone have any recommendations?

Here are some quotes from my code that are pertinent:

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 indicates that System.data. The Deletedatabase function is not provided by the Sqlite EF provider (part of Migrations, which is not implemented either). The Devart supplier offers appropriate support (not free): 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