用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"]); });
给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; } }
问题解决。
没有无参数的构造函数,那这个DbContext用起来会很不方便的。
有什么比较好的方法解决这个问题吗?
竟然有这种需要……
这种需求很正常吧