[英]EF is not creating a column for a foreign key
在我的域模型中,它的大小適中-大約有20個生成的表-實體框架沒有映射只有一種關系,我不知道為什么。 它是AccountRecord
和Account
之間的關系(1-1)。 Account
是指具有用戶的帳戶。
這些類如下所示:
public class Account
{
int ID { get; set; }
/* snip */
AccountRecord Record { get; set; }
}
public class AccountRecord
{
int ID { get; set; }
Account Account { get; set; }
/* snip */
}
映射定義如下:
public AccountConfiguration()
{
// Table name and primary key
ToTable("Accounts");
HasKey(x => x.ID);
Property(x => x.ID)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
.HasColumnName("AccountId");
HasOptional(x => x.AccountRecord).WithRequired(x => x.Account);
}
public AccountRecordConfiguration()
{
// Table name and primary key
ToTable("AccountRecords");
HasKey(x => x.ID);
Property(x => x.ID)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
.HasColumnName("AccountRecordId");
HasRequired(x => x.Account).WithOptional(x => x.AccountRecord);
}
這兩個實體的所有其他列都將被創建,而不僅僅是這個。 當我嘗試為一個帳戶設置一個帳戶記錄時,我得到以下信息: A dependent property in a ReferentialConstraint is mapped to a store-generated column
我只是想出了這一點,以為如果有人找到這個谷歌搜索,我會回答。
EF沒有創建該列,而是將AccountRecordId上的外鍵放在Accounts表中,因為存在1-1關系。 解決方案是從帳戶記錄PK中刪除DatabaseGeneratedOption.Identity
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.