Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No database providers are configured EF7

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.

like image 381
Gillardo Avatar asked Oct 11 '15 21:10

Gillardo


3 Answers

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.

like image 190
Nick De Beer Avatar answered Oct 14 '22 02:10

Nick De Beer


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)
    {
        ...
    }
}
like image 27
Jaryn Avatar answered Oct 14 '22 03:10

Jaryn


I hit this problem in Visual Studio 2015 a couple of weeks ago.

I had to add the Context to the dependency injection collection in startup.cs.

See here for more detail - http://nodogmablog.bryanhogan.net/2016/01/how-to-fix-no-database-providers-are-configured-when-scaffolding-a-controller-in-asp-net-5/

like image 1
Bryan Avatar answered Oct 14 '22 02:10

Bryan