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

No database providers are configured EF7

我似乎在使用 Entity Framework 7 和 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.

我相信我已经完成了我应该做的一切,所以这可能是一个错误。我正在使用 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,它正在返回一个有效的连接。我还检查了我的服务,该对象正在被注入,并且它不为空,所以应该一切正常。

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

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

我的 DbContext 没有覆盖 OnConfiguring 方法,因为我认为它不需要,因为我正在按照上面的方式进行所有操作?我对吗?我错过了什么?看了很多不同的网站,我猜有些使用的是旧代码,因为有些方法不存在,而其他网站和我的一样。

设置如下所示的 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 时,我遇到了同样的问题。

几周前,我在 Visual Studio 2015 年遇到了这个问题。

我必须将上下文添加到 startup.cs 中的依赖项注入集合中。

查看此处了解更多详情 - http://nodogmablog.bryanhogan.net/2016/01/how-to-fix-no-database-providers-are-configured-when-scaffolding-a-controller-in-asp-net-5/

我相信您在尝试从数据库访问某些数据的那一行遇到了这个错误。您可以通过配置上下文来解决此问题。只需覆盖 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)
    {
        ...
    }
}

尝试以下操作:

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