没有配置数据库提供程序 EF7 [英] No database providers are configured EF7

查看:19
本文介绍了没有配置数据库提供程序 EF7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎在使用 Entity Framework 7 和 MVC6 时收到此错误消息

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

System.InvalidOperationException 未配置任何数据库提供程序.通过覆盖 OnConfiguring 来配置数据库提供程序DbContext 类或在 AddDbContext 方法中设置时服务.

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.

我相信我已经完成了我应该做的一切,所以可能是一个错误.我使用的是 Entity Framework 7.0.0-beta7 版.

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,一个接口,所以我可以模拟 DbContext(在 EntityFramework 6 中需要用于单元测试).我的服务将接口作为构造函数,并且我在 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() 调用中定义的 options 参数.

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"]));

当我不得不将我的解决方案拆分为单独的项目 Web、BL 和 DAL 时,我遇到了同样的问题.

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

这篇关于没有配置数据库提供程序 EF7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆