當我要在 Ef 核心中的兩個表之間進行通信并且關系是一對多時,我遇到了問題。我想在不更改型別主鍵的情況下解決它。我的錯誤訊息如下
從 'TMSGCategory.TemporaryMessage' 到 'TemporaryMessage.TMSGCategories' 與外鍵屬性 {'MsgCategoryId' : short} 的關系不能以主鍵 {'Id' : int} 為目標,因為它不兼容。為此關系配置一個主鍵或一組兼容的外鍵屬性。
public class TemporaryMessageConfiguration:IEntityTypeConfiguration<TemporaryMessage>
{
public void Configure(EntityTypeBuilder<TemporaryMessage> builder)
{
builder.HasKey(x => x.Id);
builder.HasMany(a => a.TMSGCategories).WithOne(a =>
a.TemporaryMessage).HasForeignKey(a => a.MsgCategoryId);
}
}
public class TMSGCategoryConfiguration:IEntityTypeConfiguration<TMSGCategory>
{
public void Configure(EntityTypeBuilder<TMSGCategory> builder)
{
builder.HasKey(x => x.MsgCategoryId);
builder.HasOne(a => a.TemporaryMessage).WithMany(a => a.TMSGCategories);
}
}
uj5u.com熱心網友回復:
您可以先在代碼中將 TemporaryMessage.MsgCategoryId 和 TMSGCategories.Id 設定為 int16 并解決問題。您不能將 int 轉換為 int16,并且兩種資料型別在 efcore 關系中必須相同。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/517083.html
標籤:C#网实体框架流利
下一篇:如何填充MVC資料模型?
