[英]Multiple lists of same class in Entity Framework
我正在嘗試首先在Entity Framework代碼中創建具有相同類型關系的公司的數據模型。 我想單向創建關系,所以當公司將另一個作為其客戶添加時,另一家公司不會自動將該公司作為供應商。
public class Company {
[Key]
public string Id { set; get; }
public string Name { set; get; }
public virtual ICollection<Company> Customers { set; get; }
public virtual ICollection<Company> Suppliers { set; get; }
}
當我像這樣更新數據庫時,我最終得到一個名為“CompanyCompanies”的連接表。
CompanyCompanies
| Company_Id | Company_Id1 |
|------------|-------------|
| 1234 | 1234 |
我想創建兩個表來連接這些值,如下所示:
CompanyCustomers
| Company_Id | Customer_Id |
|------------|-------------|
| 1234 | 1234 |
CompanySuppliers
| Company_Id | Supplier_Id |
|------------|-------------|
| 1234 | 1234 |
我看了很多流利的api,並試圖定義那里的關系,但我似乎無法理解如何做到這一點。 我發現了許多使用不同實體的例子,但只有這個答案與自身有關系。 但是,當我從該問題實現解決方案並更新數據庫時,我收到一條錯誤消息,指出連接已超時。
modelBuilder.Entity<Company>()
.HasMany(c => c.Customers)
.WithMany()
.Map(m =>
{
m.ToTable("CompanyCustomer");
m.MapLeftKey("CompanyId");
m.MapRightKey("CustomerId");
});
錯誤號:-2,狀態:0,類:11執行超時已過期。
操作完成之前經過的超時時間或服務器沒有響應。
當我嘗試使用CompanyCustomer和CompanySupplier時,我得到了antoher錯誤
參數@objname是不明確的,或者聲明的@objtype(COLUMN)是錯誤的。
我還嘗試為關系創建一個新對象,並讓Company對象保存CompanyCustomers列表,但是當我用這個更新數據庫時,我得到了一個包含三列的連接表。
public class CompanyCustomer
{
[Key, Column(Order = 0)]
[ForeignKey("Company")]
public string CompanyId { set; get; }
[Key, Column(Order = 1)]
[ForeignKey("Customer")]
public string CustomerId { set; get; }
public virtual Company Company { set; get; }
public virtual Company Customer { set; get; }
}
CompanyCustomer
| CompanyId | CustomerID | Company_Id |
|-----------|------------|------------|
| 1234 | 1234 | 1234 |
我不明白我如何在同一個實體之間建立幾種關系類型。 我可以在流暢的api的幫助下做到這一點還是我必須重新設計我的模型?
流暢的配置
modelBuilder.Entity<Company>()
.HasMany(c => c.Customers)
.WithMany()
.Map(m =>
{
m.ToTable("CompanyCustomer");
m.MapLeftKey("CompanyId");
m.MapRightKey("CustomerId");
});
確實是配置第一個關系的正確方法。 您需要為第二個做類似的事情:
modelBuilder.Entity<Company>()
.HasMany(c => c.Suppliers)
.WithMany()
.Map(m =>
{
m.ToTable("CompanySupplier");
m.MapLeftKey("CompanyId");
m.MapRightKey("SupplierId");
});
在我干凈的EF測試環境中(如果重要的話,最新的EF6.1.3)產生以下遷移:
CreateTable(
"dbo.Company",
c => new
{
Id = c.String(nullable: false, maxLength: 128),
Name = c.String(),
})
.PrimaryKey(t => t.Id);
CreateTable(
"dbo.CompanyCustomer",
c => new
{
CompanyId = c.String(nullable: false, maxLength: 128),
CustomerId = c.String(nullable: false, maxLength: 128),
})
.PrimaryKey(t => new { t.CompanyId, t.CustomerId })
.ForeignKey("dbo.Company", t => t.CompanyId)
.ForeignKey("dbo.Company", t => t.CustomerId)
.Index(t => t.CompanyId)
.Index(t => t.CustomerId);
CreateTable(
"dbo.CompanySupplier",
c => new
{
CompanyId = c.String(nullable: false, maxLength: 128),
SupplierId = c.String(nullable: false, maxLength: 128),
})
.PrimaryKey(t => new { t.CompanyId, t.SupplierId })
.ForeignKey("dbo.Company", t => t.CompanyId)
.ForeignKey("dbo.Company", t => t.SupplierId)
.Index(t => t.CompanyId)
.Index(t => t.SupplierId);
這正是你想要的。
您獲得的例外情況如何,它們可能與您使用數據庫的實驗有關。 確保要么從干凈的數據庫開始,要么刪除Company
實體(以及CompanyCustomer
如果它在那里),相應的DbSet
和流暢的配置,更新數據庫以清除之前的混亂並再試一次。
您只需使用屬性即可完成此操作。 您的問題是您在錯誤的屬性上使用[ForeignKey]
屬性 - 它應該在導航屬性上。
[ForeignKey(nameof(CompanyId))]
public virtual Company Company { set; get; }
[ForeignKey(nameof(CustomerId))]
public virtual Company Customer { set; get; }
此外,您可以將[InverseProperty]
屬性添加到模型上的集合中。 這有助於理解關系導航。
[InverseProperty(nameof(CompanyCustomer.Company))]
public virtual ICollection<Company> Customers { set; get; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.