本文介绍了值不能为空.(参数'key')在ASP.NET Core 3..0中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在ASP.NET Core 3.0中以代码优先的方式创建数据库
I need to create a database with code-first in 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
时,出现此错误:
When I use this add-migration initial
, I get this error:
出什么问题了?我该如何解决?
What's the problem? How can I solve this?
推荐答案
这是因为您的一个实体(从接口: IEntity
继承)没有标识列.
It was because of one of your entities (inherited from interface: IEntity
) do not have an identity column.
请检查所有实体.确保它们都有一个ID或标记为 [Key]
的属性.
Please check all your entites. Make sure they all have an ID or a property marked as [Key]
.
public class MyEntity : IEntity
{
// make sure:
public int Id { get; set; }
// or:
[Key]
public int SomeProperty { get; set; }
}
这篇关于值不能为空.(参数'key')在ASP.NET Core 3..0中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!