No database providers are configured EF7

asp.net-core asp.net-core-mvc c# entity-framework entity-framework-core

Question

I seem to be getting this error message when using Entity Framework 7 and MVC6

System.InvalidOperationException No database providers are configured. Configure a database provider by overriding OnConfiguring in your DbContext class or in the AddDbContext method when setting up services.

I believe i have done everything i am supposed to be doing, so maybe its a bug. I am using version 7.0.0-beta7 of Entity Framework.

I have setup my DbContext, an interface so i can mock DbContext (was needed in EntityFramework 6 for unit testing). My services take the interface as a constructor and i have setup DI in MVC 6.

In my Startup.cs file i have the following

public void ConfigureServices(IServiceCollection services)
{
    // entity framework
    services.AddEntityFramework()
        .AddSqlServer()
        .AddDbContext<MyDbContext>(options =>
            options.UseSqlServer(Configuration["Data:MyConnection:ConnectionString"])
        );

    // Add MVC services to the services container.
    services.AddMvc();

    // new context on each request
    services.AddScoped<IMyDbContext, MyDbContext>();
}

I have checked my connectionString and that is returning a valid connection. I have also checked in my service that the object is being injected, and it is not null, so that should all work.

My config.json file looks like this

{
    "Data": {
        "MyConnection": {
            "ConnectionString": "Server=(local);Database=XXXX;Trusted_Connection=True;"
        }
    }
}

My DbContext does not override the OnConfiguring method, because i believe it is not needed as i am doing it all as above? Am i right? What am i missing? Looked at lots of different websites, i guess some are using old code, because some methods dont exist and other websites have the same as what i have.

Accepted Answer

Setup your MyDbContext shown below to inject the options parameter defined in Startup.cs AddDbContext() call.

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

This will allow you to pass your connection string from the Configuration (config.json) into the method call options.UseSqlServer()

services.AddEntityFramework()
    .AddSqlServer()
    .AddDbContext<MyDbContext>(options => options.UseSqlServer(Configuration["Data:MyConnection:ConnectionString"]));

I encountered the same problem when I had to split my solution into separate projects Web, BL, and DAL.


Popular Answer

I belive that you hit this error on the line where you trying to access some data from database. You can resolve this problem by confiuguring your context. Just override OnConfiguring method.

public class MyDbContext : DbContext
{
    public DbSet<Employee> Employees { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder options)
    {
        options.UseSqlServer("<your connection string>");
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        ...
    }
}


Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why