没有配置EF7数据库提供程序

13

在使用Entity Framework 7和MVC6时,我似乎遇到了这个错误消息。

System.InvalidOperationException:未配置任何数据库提供程序。通过在您的DbContext类中重写OnConfiguring方法或在设置服务时的AddDbContext方法中重写该方法来配置数据库提供程序。

我相信我已经做了应该做的一切,所以可能是个bug。我正在使用Entity Framework的7.0.0-beta7版本。

我已经设置好了我的DbContext,还有一个接口,这样我就可以模拟DbContext(在EntityFramework 6中进行单元测试时需要)。我的服务使用接口作为构造函数,并在MVC 6中设置了DI。

在我的Startup.cs文件中,我有以下内容

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并且返回了有效的连接。我还在我的服务中检查了被注入的对象,它不是null,所以应该都可以工作。

我的config.json文件看起来像这样

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

我的DbContext没有覆盖OnConfiguring方法,因为我认为不需要,因为我已经按照上述方式完成了所有操作。 我是对的吗? 我错过了什么?查看了许多不同的网站,我猜有些网站使用的是旧代码,因为一些方法不存在,而其他网站与我拥有相同的方法。


你在代码中如何创建你的MyDbContext - DavidG
4个回答

26

将以下 MyDbContext 设置为注入 Startup.cs 中定义的 AddDbContext() 调用中定义的选项参数。

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

这将允许您从配置文件(config.json)中将连接字符串传递到方法调用options.UseSqlServer()中。

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

当我需要将我的解决方案拆分为独立的Web、BL和DAL项目时,我遇到了同样的问题。


如果您想模拟DbContext,您不需要一个接口。只需将DbSet<T>设置为虚拟的,这样您就可以模拟它们了。我假设您不想模拟context.Database或context.Configuration并测试实体框架;-) - Pascal
你会如何在控制器或需要使用它的任何地方实例化MyDbContext? - Serj Sagan
将依赖注入到控制器/服务/存储库中,无论何时需要使用它。因此,只需将DbContext添加到需要它的构造函数中即可。其工作原理是:调用AddDbContext()会调用AddScoped(TContext),它会在每个请求范围内创建一个DbContext实例。请在此处查看AddDbContext()的工作方式 https://github.com/aspnet/EntityFramework/blob/dev/src/EntityFramework.Core/Infrastructure/EntityFrameworkServicesBuilder.cs#L68-L77 - Nick De Beer

4

我相信您在试图从数据库中访问某些数据的行上遇到了此错误。您可以通过配置上下文来解决此问题。只需覆盖OnConfiguring方法即可。

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)
    {
        ...
    }
}

1

1
尝试以下内容:
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"]);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接