![](/img/trans.png)
[英]Ef core Creating additional column for a navigation property with Name *ID1
[英]EF Core - ID1 property added when adding a migration
我的方案是投資物業(建築物)。 投資者對一個物業組進行投資,該組可以包含多個單獨的物業。 由於投資人可以投資於多個群體,而任何一個群體都可以有多個投資人,投資本身就是投資人與地產群體之間的多對多關系。
為了清楚起見,忽略簡單的屬性,模型看起來像這樣……
public class Investor {
public int ID { get; set; }
public ObservableCollection<Investment> Investments { get; set; }
}
public class Investment {
public int ID { get; set; }
public int InvestorID { get; set; }
public virtual Investor Investor { get; set; }
public int PropertyGroupID { get; set; }
public virtual PropertyGroup PropertyGroup { get; set; }
}
public class PropertyGroup {
public int ID { get; set; }
public ObservableCollection<Property> Properties { get; set; }
public ObservableCollection<Investment> Investments { get; set; }
}
public class Property {
public int ID { get; set; }
public int PropertyGroupID { get; set; }
public virtual PropertyGroup PropertyGroup { get; set; }
}
這一切都很好。
我現在有生成和存儲文件的需求。 這些將始終與特定投資者相關聯,但也可能與財產或團體相關聯。
我添加了以下 class...
public class Document {
public int ID { get; set; }
// Other simple properties omitted for clarity
public int InvestorId { get; set; }
public Investor Investor { get; set; } = null!;
public int? PropertyGroupId { get; set; }
public PropertyGroup? PropertyGroup { get; set; }
public int? PropertyId { get; set; }
public Property? Property { get; set; }
}
為了提供文檔的導航鏈接,我在Investor
、 PropertyGroup
和Property
模型中添加了以下行...
public ObservableCollection<Investment> Documents { get; set; }
當我嘗試添加遷移時,出現錯誤“無法確定類型為‘Investor’的導航‘Investment.Investor’所代表的關系。 ”
從遷移中讀取 output,我看到一條消息“沒有按照約定配置從‘Investment’到‘Investor’的關系,因為一個實體類型上有多個屬性 - {'Investor'} 可以與上的屬性匹配另一種實體類型 - {'Documents', 'Investments'}。 ”
我不理解任何一條消息,因為我確實有從Investment
到Investor
定義的明確關系,正如您在上面看到的那樣。 我有一個名為InvestorId
的int
屬性和一個類型(和名稱) Investor
的導航屬性。 我不確定第二條消息的最后一點是什么意思。
但是,我將以下內容添加到OnModelCreating
...
builder.Entity<Investment>()
.HasOne<Investor>()
.WithMany(i => i.Investments)
.HasForeignKey(i => i.InvestorID)
.OnDelete(DeleteBehavior.Restrict);
這擺脫了那個錯誤,但給出了一個類似的錯誤,“無法確定類型為‘PropertyGroup’的導航‘Investment.PropertyGroup’所代表的關系。 ”我添加了以下內容……
builder.Entity<Investment>()
.HasOne<PropertyGroup>()
.WithMany(i => i.Investments)
.HasForeignKey(i => i.PropertyGroupID)
.OnDelete(DeleteBehavior.Restrict);
這允許添加遷移,但它在Up
方法中包含以下內容......
migrationBuilder.AddColumn<int>(
name: "InvestorID1",
table: "Investments",
type: "int",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<int>(
name: "PropertyGroupID1",
table: "Investments",
type: "int",
nullable: false,
defaultValue: 0);
我可以看到遷移 output 包含警告“外鍵屬性‘Investment.InvestorID1’是在影子 state 中創建的,因為實體類型中存在簡單名稱‘InvestorID’的沖突屬性,但要么未映射,要么已被使用對於另一種關系,或者與關聯的主鍵類型不兼容。 ”但我不明白這是什么意思。
誰能解釋我做錯了什么? 謝謝
遺漏了一些東西(打字錯誤),要與Document
建立關系,您必須定義Document
而不是Investments
的集合props
,因此請更改
public ObservableCollection<Investment> Documents { get; set; }
到
public ObservableCollection<Document> Documents { get; set; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.