2013-07-04 81 views
0

我正在爲我的管絃樂規劃工具開發實體關係數據模型。最終結果應該是ASP.NET/MVC4應用程序。如何在多對多關係代碼優先級上設置屬性?

這是我的E/R-圖的一部分:

Orchestra planning tool E/R diagram

在圖像上面我嘗試來可視化,有一個EventComposition之間的多對多的關係。在我的模型中,我還希望可以存儲任意EventComposition的錄音(可以存在來自不同事件/協奏曲的相同合成的許多不同錄製版本)。

這是我到目前爲止已經完成(與我的代碼,第一個數據模型相關的代碼):

public class Composition 
{ 
    public Composition() 
    { 
     Instruments = new Collection<Instrument>(); 
     Events = new Collection<Event>(); 
    } 

    [Key] 
    public int Id { get; set; } 
    public bool Active { get; set; } 

    public string Name { get; set; } 
    public string Description { get; set; } 
    public TimeSpan Durata { get; set; } 

    public virtual Composer Composer { get; set; } 
    public virtual Composer Genre { get; set; } 
    public virtual ICollection<Instrument> Instruments { get; set; } 
    public virtual ICollection<Event> Events { get; set; } 
} 

_

public class Event 
{  
    public Event() 
    { 
     Compositions = new Collection<Composition>(); 
     Members = new Collection<Member>(); 
    } 

    [Key] 
    public int Id { get; set; } 
    public bool Active { get; set; } 

    public string Name { get; set; } 
    public string Description { get; set; } 

    public DateTime StartDate { get; set; } 
    public DateTime EndDate { get; set; } 

    public virtual ICollection<Member> Members { get; set; } 
    public virtual ICollection<Composition> Compositions { get; set; } 
    public virtual Calendar Calendar { get; set; } 
    public virtual EventType EventType { get; set; } 
    public virtual Location Location { get; set; } 
} 

我的問題:如果在我的代碼做我添加關係屬性「錄製」?

編輯:我必須創建一個鏈接表還是有更好的選擇嗎?

回答

1

你試圖實現的是與屬性稱爲多對多關係。爲了在關係數據庫中建模N到M的關係,你總是需要一箇中間表。要使用EF添加屬性,您需要另一個實體來建模關係並正確映射它。在這個問題中,它是如何做到這一點的:

Entity Framework Code 1st - Mapping many-to-many with extra info

+0

Thx爲鏈接。在將此標記爲答案之前,我應該做一些測試。 –

+0

感謝您的答覆,我很高興您解決它。 – Tasio

相關問題