【发布时间】:2020-05-27 11:19:06
【问题描述】:
我需要在 ASP.NET Core 3.0 中使用代码优先创建数据库
这是DbContext:
public class TrelloContext : DbContext
{
public TrelloContext(DbContextOptions options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.AddDbSet<IEntity>(typeof(IEntity).Assembly);
modelBuilder.ApplyConfigurationsFromAssembly(typeof(IType).Assembly);
}
}
这是启动:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers().AddControllersAsServices();
services.AddDbContext<TrelloContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SqlServer")));
services.Injection();
}
这是我的连接字符串:
"ConnectionStrings": {
"SqlServer": "Data Source=.;Initial Catalog=TrelloDB;Trusted_Connection=True;Trusted_Connection=True;"
}
当我使用这个add-migration initial 时,我得到这个错误:
值不能为空。 (参数'key')
有什么问题?我该如何解决这个问题?
【问题讨论】:
-
也可以添加堆栈跟踪。
-
什么是`modelBuilder.AddDbSet
(typeof(IEntity).Assembly);`?你用vs 2019 16.3吗?您是否尝试过重建解决方案?
标签: c# asp.net asp.net-core