首页 新闻 赞助 找找看

ASP.NET Core: No database provider has been configured for this DbContext

0
悬赏园豆:10 [已解决问题] 解决于 2016-03-27 15:32

用dotnet run将asp.net core站点运行起来后,访问时却出现如下的错误:

InvalidOperationException: No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor.

dotnet cli的版本是:.NET Command Line Tools (1.0.0-beta-001793)

ef的版本是:Microsoft.EntityFrameworkCore 1.0.0-rc2-20334

Startup.cs中ConfigureServices()方法中的代码如下:

services.AddEntityFrameworkSqlServer()
    .AddDbContext<EfDbContext>(options =>
    {
        options.UseSqlServer(Configuration["data:ConnectionString"]);
    });
dudu的主页 dudu | 高人七级 | 园豆:31075
提问于:2016-03-27 14:49
< >
分享
最佳答案
2

给EfDbContext

public class EfDbContext : DbContext
{
    public DbSet<TabNav> TabNavs { get; set; }
}

加上构造函数

public class EfDbContext : DbContext
{
    public EfDbContext(DbContextOptions options) : base(options)
    {
    }

    public DbSet<TabNav> TabNavs { get; set; }
}

问题解决。

dudu | 高人七级 |园豆:31075 | 2016-03-27 15:32

没有无参数的构造函数,那这个DbContext用起来会很不方便的。

有什么比较好的方法解决这个问题吗?

任跃兵 | 园豆:260 (菜鸟二级) | 2016-06-13 12:37
其他回答(1)
0

竟然有这种需要……

零度的火 | 园豆:211 (菜鸟二级) | 2016-03-30 22:33

这种需求很正常吧

支持(0) 反对(0) 一两二三 | 园豆:200 (初学一级) | 2023-12-18 15:23
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册