C# 值不能为null。ASP.NET Core 3..0中的(参数';键';)

C# 值不能为null。ASP.NET Core 3..0中的(参数';键';),c#,asp.net,asp.net-core,C#,Asp.net,Asp.net Core,我需要先在ASP.NET Core 3.0中使用代码创建数据库 这是DbContext: public class TrelloContext : DbContext { public TrelloContext(DbContextOptions options) : base(options) { } protected override void OnModelCreating(ModelBuilder modelBuilder) {

我需要先在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);
    }
}
使用此
添加迁移初始值
时,出现以下错误:

值不能为null。(参数“key”)


有什么问题吗?如何解决此问题?

这是因为您的一个实体(从接口继承:
ienty
)没有标识列

请检查您所有的财产。确保它们都有一个ID或一个标记为
[Key]
的属性

公共类MyEntity:Entity
{
//确保:
公共int Id{get;set;}
//或:
[关键]
公共int SomeProperty{get;set;}
}

仅用于注册,当从现有表创建UniqueKey,并且UniqueKey字段之间已经存在重复记录时,DDD中也会发生这种情况

例如:

//SQL: [dbo].[User]:
Id | Name | Login
1  | Thi  | thi.xpto
2  | Thi  | thi.xpto


[Table("User")]
public class User
{
  [Key]
  public int Id { get; set; }
  public string Name { get; set; }
  public string Login { get; set; }
}

public class UserDbContext : DbContext
{
  ...
  protected override void OnModelCreating(ModelBuilder modelBuilder)
  {
    modelBuilder.Entity<User>()
      .HasAlternateKey(x => new { x.Name, x.Login })
      .HasName("UK_User_NameLogin");
  }
}
//SQL:[dbo].[User]:
Id |名称|登录
1 | Thi | Thi.xpto
2 | Thi | Thi.xpto
[表格(“用户”)]
公共类用户
{
[关键]
公共int Id{get;set;}
公共字符串名称{get;set;}
公共字符串登录{get;set;}
}
公共类UserDbContext:DbContext
{
...
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
modelBuilder.Entity()
.hasaAlternateKey(x=>new{x.Name,x.Login})
.HasName(“英国用户姓名登录”);
}
}

也可以添加stacktrace。什么是`modelBuilder.AddDbSet(typeof(ienty.Assembly);`?您是否使用vs 2019 16.3?您是否尝试重新构建解决方案?
"ConnectionStrings": {
    "SqlServer": "Data Source=.;Initial Catalog=TrelloDB;Trusted_Connection=True;Trusted_Connection=True;"
}
//SQL: [dbo].[User]:
Id | Name | Login
1  | Thi  | thi.xpto
2  | Thi  | thi.xpto


[Table("User")]
public class User
{
  [Key]
  public int Id { get; set; }
  public string Name { get; set; }
  public string Login { get; set; }
}

public class UserDbContext : DbContext
{
  ...
  protected override void OnModelCreating(ModelBuilder modelBuilder)
  {
    modelBuilder.Entity<User>()
      .HasAlternateKey(x => new { x.Name, x.Login })
      .HasName("UK_User_NameLogin");
  }
}