No database providers are configured EF7
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.Configure<Content>(Configuration.GetSection("ContentList"));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddDbContext<MovieDBContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DbLearning")));
}
up vote 13 down vote favorite
2
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.
c# entity-framework asp.net-core asp.net-core-mvc entity-framework-core
2,77922139
asked Oct 11 '15 at 21:01
3,681114599
-
How are you creating your
MyDbContext
in code? – DavidG Oct 11 '15 at 21:55
add a comment
4 Answers
up vote 26 down vote accepted
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.
answered Oct 11 '15 at 22:55
2,77922139
-
Perfect! Thank you – Gillardo Oct 12 '15 at 3:36
-
If you want to mock the DbContext you do not need an interface. Just make the DbSet<T> virtual so you can mock those. I assume you do not want to mock context.Database or context.Configuration and test the entity framework ;-) – Pascal Nov 26 '15 at 20:42
-
How would you instantiate MyDbContext in the Controller or wherever you need to use it? – Serj Sagan Jan 6 '16 at 5:36
-
Dependency Injection into the controller/service/repo where ever you need to use it. So basically just add the DbContext to the constructor of where you need it. How it works is: calling AddDbContext() calls AddScoped(TContext) which creates an instance of the DbContext once per request scope. Have a look here how AddDbContext() works github.com/aspnet/EntityFramework/blob/dev/src/… – Nick De Beer Jan 6 '16 at 7:19
add a comment
up vote 4 down vote
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)
{
...
}
}
answered Mar 28 '16 at 15:33
306211
add a comment
up vote 1 down vote
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/
answered Jan 28 '16 at 4:00
2,19132744
add a comment
up vote 1 down vote
Try the following:
public class DataContext<TEntity> : DbContext where TEntity : class
{
private TEntity _entity = null;
public DataContext()
: base()
{
}
public DataContext(TEntity entity)
{
this._entity = entity;
}
public DbSet<TEntity> Entity { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.HasDefaultSchema("dbo");
}
public IConfigurationRoot Configuration { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json");
Configuration = configuration.Build();
optionsBuilder.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]);
}
}
1,021719
answered Apr 20 '16 at 5:22
111
add a comment