我有4個表/實體,分爲兩組,警報和收件人。組中的任何一個實體都可以映射到另一個組的任一實體(警報可以有許多收件人和收件人組等)。EntityFramework加入超過2個實體的表
表:
- 警報
- AlertGroups(1與警報一對多的關係)
- 收件人
- RecipientGroups(許多與收件人一對多的關係)
,而不是使4個連接表(AlertRecipients,AlertRecipientGroups等)我想創建一個包含4列的連接表,每個連接表lumn是我實體類型之一的可空FK。
我做了表中的SQL,併成立了我的環境中使用流利的API,像這樣:
modelBuilder.Entity<AlertGroup>()
.HasMany(ag => ag.RecipientGroups)
.WithMany(rg => rg.AlertGroups)
.Map(m => m.ToTable("AlertRecipients")
.MapLeftKey("AlertGroupID")
.MapRightKey("RecipientGroupID"));
modelBuilder.Entity<AlertGroup>()
.HasMany(ag => ag.Recipients)
.WithMany(rg => rg.AlertGroups)
.Map(m => m.ToTable("AlertRecipients")
.MapLeftKey("AlertGroupID")
.MapRightKey("RecipientID"));
但我得到這個錯誤:
Schema specified is not valid. Errors:
(251,6) : error 0019: The EntitySet 'AlertGroupRecipient' with schema 'dbo' and table 'AlertRecipients' was already defined. Each EntitySet must refer to a unique schema and table.
有一種解決方法做我在做什麼?
只是一個瘋狂的猜測..我認爲'AlertGroup'包含'Alerts'等。你不能將關聯限制爲一個多對多的關聯'AlertGroups'''RecipientGroups',其中有1個成員的組作爲單一表單嗎? (也許有一些特殊的商業邏輯)。具有可爲空的FK的聯結表不是一個很好的構造,並且只有幾個允許的列組合的聯結表沒有標準化並且容易出錯。最後,最好的選擇是4個聯結表。 – 2013-02-11 21:40:07
@GertArnold我知道它沒有標準化,但是4個聯接表看起來很笨重。另外,我還有一組關係,可以將兩個實體中的任何一個映射到任何一個或三個實體(現在有六個聯結表),我也想將這些全部推到一個表中。 – 2013-02-11 21:47:22
我明白你的反對意見。有時候這些連接桌也會讓我感到不安。儘管如此,我更喜歡簡潔的數據庫模型,1個關聯表。數據模型如此基礎,以後很難改變。任何妥協都會困擾你多年(我知道,我有一些)。 – 2013-02-11 21:55:51