没有数据库提供商配置EF7 [英] No database providers are configured EF7
问题描述
我似乎得到该错误消息使用实体框架7和MVC6
I seem to be getting this error message when using Entity Framework 7 and MVC6
System.InvalidOperationException没有数据库提供商配置时。
通过在
的DbContext类或AddDbContext方法覆盖OnConfiguring设置
服务时,配置数据库提供商。
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.
我相信我所做的一切都是我应该做,所以也许它的一个bug。我使用实体框架的版本7.0.0-同β7。
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.
我安装我的DbContext,需要在6的EntityFramework的接口,所以我可以嘲笑的DbContext(单元测试)。我的服务把接口作为一个构造函数和我在MVC 6设置DI
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.
在我Startup.cs文件我有以下
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>();
}
我已经检查了我的connectionString并返回一个有效的连接。我还检查了我的服务的对象是被注入,它不为空,所以都应该的工作,。
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.
我的config.json文件看起来像这样
My config.json file looks like this
{
"Data": {
"MyConnection": {
"ConnectionString": "Server=(local);Database=XXXX;Trusted_Connection=True;"
}
}
}
我的DbContext不会覆盖OnConfiguring方法,因为我相信它不需要像我这样做以上这一切?我对吗?我在想什么?在许多不同的网站看了看,我猜一些正在使用的旧代码,因为一些方法不存在和其他网站有一样我有什么。
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.
推荐答案
设置如下图所示的MyDbContext注入在Startup.cs AddDbContext()调用定义的选项参数。
Setup your MyDbContext shown below to inject the options parameter defined in Startup.cs AddDbContext() call.
public MyDbContext(DbContextOptions options)
: base(options)
{ }
这将允许您从配置(config.json)连接字符串传递到方法调用options.UseSqlServer()
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"]));
我遇到同样的问题,当我有我的解决方案分成单独的项目网站,BL和DAL
I encountered the same problem when I had to split my solution into separate projects Web, BL, and DAL.
这篇关于没有数据库提供商配置EF7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!