亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

值不能為空。ASP.NET Core 3..0 中的(參數“key”)

值不能為空。ASP.NET Core 3..0 中的(參數“key”)

PHP
函數式編程 2024-01-20 16:08:54
我需要在 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,我收到此錯誤:值不能為空。(參數“鍵”)有什么問題?我該如何解決這個問題?
查看完整描述

2 回答

?
料青山看我應如是

TA貢獻1772條經驗 獲得超8個贊

這是因為您的實體之一(繼承自接口:)IEntity沒有標識列。


請檢查您的所有實體。確保他們都有一個 ID 或標記為 的屬性[Key]。


public class MyEntity : IEntity

{

    // make sure:

    public int Id { get; set; }

    // or:

    [Key]

    public int SomeProperty { get; set; }

}


查看完整回答
反對 回復 2024-01-20
?
米脂

TA貢獻1836條經驗 獲得超3個贊

僅對于注冊而言,當從現有表創建 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");

  }

}


查看完整回答
反對 回復 2024-01-20
  • 2 回答
  • 0 關注
  • 188 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號