'No database provider has been configured for this DbContext' using SQLite

asp.net-core c# entity-framework-core entity-framework-core-2.1 sqlite

Question

I'm just experimenting with EF Core and SQLite and have been slightly stumped by the issue.

If I attempt to configure the database like this:

services.AddDbContext<MyDbContext>(options => options.UseSqlite("Filename=./App_Data/dashboard.db") );

And then execute:

dotnet ef migrations add InitialCreate -c MyDbContext

I get:

'No database provider has been configured for this DbContext'

But if I do this:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    optionsBuilder.UseSqlite("Filename=./App_Data/dashboard.db");
}

All works as expected.

My context is setup to work with the fluent interface:

public MyDbContext(DbContextOptions options) 
    : base(options)
{
}

public MyDbContext(DbContextOptions<MyDbContext> options)
    : base(options)
{
}

It seems from various articles that others have the fluent approach working fine.

Have anyone else had this issue and solved in using Core 2.1.0?

1
1
6/2/2018 2:33:47 PM

Popular Answer

I would try adding a design time db context factory to your project like this:

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;

namespace yournamespace
{
    public class YourDbContextDesignTimeFactory : IDesignTimeDbContextFactory<YourDbContext>
    {
        public YourDbContext CreateDbContext(string[] args)
        {
            var builder = new DbContextOptionsBuilder<YourDbContext>();
            builder.UseSqlite("Data Source=somename.db");

            return new YourDbContext(builder.Options);
        }
    }
}

I don't think the connection string has to be correct, EF will use this to create the context when generating the migration

0
6/2/2018 5:53:31 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