【发布时间】:2018-09-19 12:02:43
【问题描述】:
初始化dbModel上下文时出现如下错误:
这是我的dbContext 班级:
public class DbModel : DbContext
{
public DbModel()
{
}
public DbModel(DbContextOptions<DbModel> options)
: base(options)
{ }
public DbSet<UserModel> User {get;set;}
}
根据stackoverflow中的一些答案,我在Startup.cs的ConfigureServices方法中添加了以下代码,但仍然出现错误。
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
这是我的ConfigureServices 方法:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
var connection = @"Server=(localdb)\mssqllocaldb;Database=EFGetStarted.AspNetCore.Restaurant;Trusted_Connection=True;ConnectRetryCount=0";
services.AddDbContext<DbModel>(options => options.UseSqlServer(connection));
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "Restaurant APIs", Description = "Swagger Core API" });
});
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
}
可能是什么原因?
【问题讨论】:
标签: c# entity-framework asp.net-core asp.net-core-2.0