因此,在我們有兩個類的映射中遇到問題public class Customer{ public int Id; public CustomerAddress StatementAddress public bool AlwaysTrue => true; public string Code;}public class CustomerAddress{ public int Id; public bool ForStatement; public string _CustomerCode}我們有一個像這樣設置的映射:protected override void OnModelCreating(ModelBuilder modelBuilder){ modelBuilder.Entity<Customer>() .HasOne(s => s.StatementAddress) .WithMany() .HasPrincipalKey(x=> new {x._CustomerCode, x.ForStatement}) .HasForeignKey(s => new { s.Code, s.AlwaysTrue});}然而,這導致了一個問題,因為它似乎是在數據庫上尋找 Customer 表中的 StatementAddress_CustomerCode 和 StatementAddress_ForStatement 列,這顯然不是存在的東西 - 為什么它不尋找 Code 表并簡單地通過 True for AlwaysTrue?我原以為它的定義鍵的方式只是使用代碼和 Alwaystrue 值,但似乎正在尋找由屬性名稱的組合組成的兩列,附加來自另一個類的屬性名稱 =/
2 回答

波斯汪
TA貢獻1811條經驗 獲得超4個贊
我在這里建議兩件事:
您可以像這樣手動定義列名:
public class Customer
{
public int Id;
public CustomerAddress StatementAddress;
[Column("StatementAddress_ForStatement")]
public bool AlwaysTrue => true;
[Column("StatementAddress_CustomerCode")]
public string Code;
}
您可以ForeignKeyAttribute像這樣指定外鍵:
public class Customer
{
public int Id;
public CustomerAddress StatementAddress;
[ForeignKey("StatementAddress"), Column(Order = 1)]
public bool AlwaysTrue => true;
[ForeignKey("StatementAddress"), Column(Order = 0)]
public string Code;
}
請記住,對于此選項,您必須設置列順序屬性以匹配CustomerAddress表中外鍵所具有的屬性。
希望這可以幫助!
- 2 回答
- 0 關注
- 206 瀏覽
添加回答
舉報
0/150
提交
取消