0

作爲學習新東西的一種方式,我決定嘗試創建一個站點來跟蹤C#.Net Core MVC中的賽車結果,並使用EF代碼優先數據庫。數據庫的結構跟蹤EF核心中的賽車結果

但是直接出門我想知道我的數據結構完全採用了錯誤的方法。

我有模型的民族和季節,這是retty直接(ID,名稱和季節,收集的種族)。但是我完成Race和Driver模型的方式稍微複雜一些。我想跟蹤a)比賽的地點,b)哪個賽季是賽事的一部分,以及c)哪個車手完成了比賽。

每場比賽將有26名車手參賽。

這是我的比賽創建的模型:

public class Race 
    { 
     public int ID { get; set; } 
     public int SeasonID { get; set; } 
     public Season Season { get; set; } 
     public int NationID { get; set; } 
     public Nation Nation { get; set; } 

     public int Driver1ID { get; set; } 
     [ForeignKey("Driver1ID")] 
     public virtual Driver Driver1 { get; set; } 

     public int Driver2ID { get; set; } 
     [ForeignKey("Driver2ID")] 
     public virtual Driver Driver2 { get; set; } 

     public int Driver3ID { get; set; } 
     [ForeignKey("Driver3ID")] 
     public virtual Driver Driver3 { get; set; } 

     // And so on, I'll spare you the rest, it goes all the way down to 26... 

     public int Driver26ID { get; set; } 
     [ForeignKey("Driver26ID")] 
     public virtual Driver Driver26 { get; set; } 


    } 

而這裏的駕駛模式:

public class Driver 
    { 
     public int ID { get; set; } 
     public string FirstName { get; set; } 
     public string LastName { get; set; } 
     public int NationID { get; set; } 
     public Nation Nation { get; set; } 
     public ICollection<Race> P1 { get; set; } 
     public ICollection<Race> P2 { get; set; } 
     public ICollection<Race> P3 { get; set; } 
// Again I'll save you the whole thing, it goes down to 26... 
     public ICollection<Race> P26 { get; set; } 
    } 

爲了使這項工作我想我必須設置在上下文中的關係類是這樣的...

protected override void OnModelCreating(ModelBuilder modelBuilder) 
     { 
      modelBuilder.Entity<Race>().HasOne(r => r.Driver1).WithMany(d => d.P1); 
      modelBuilder.Entity<Race>().HasOne(r => r.Driver2).WithMany(d => d.P2); 
      modelBuilder.Entity<Race>().HasOne(r => r.Driver3).WithMany(d => d.P3); 
// Snip down to 26 again... 
      modelBuilder.Entity<Race>().HasOne(r => r.Driver26).WithMany(d => d.P26); 
     } 

但是當我嘗試和更新數據庫我得到一個錯誤,這STRUC這會導致「循環或多個級聯路徑」,這聽起來不太好。我知道可以將Cascade Delete設置爲關閉狀態,但是錯誤信息讓我覺得我真的在這裏走錯了路徑......我在這裏完全吠叫了錯誤的樹嗎?

任何幫助或建議將不勝感激!

+0

您不應該有26個單獨的導航屬性。只需使用Race和Driver之間的多對多關係,可能會使用一個名爲RaceParticipant等的顯式鏈接實體。 –

回答

2

我想我會像這個模型(留出這樣的東西的季節,國家):

public class Race 
{ 
    public int ID { get; set; } 

    public virtual ICollection<RaceParticipation> Participants { get; set; } 
} 

public class Driver 
{ 
    public int ID { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public ICollection<RaceParticipation> Races { get; set; } 
} 

public class RaceParticipation 
{ 
    public int ID {get;set;} 
    public Race Race {get;set;} 
    public Driver Driver {get;set;} 
    // maybe information like this: 
    public int StartingPosition {get;set;} 
    public int FinalPosition {get;set;} 
} 

的東西,如一個事實,即每場比賽正好有26名參加者,應IMO是屬於你業務邏輯,而不是數據庫設計。這可能是將來某個時候可能會改變的事情(也就是說,如果您希望每場比賽的參賽人數不同)。所以在代碼中擁有這樣的邏輯似乎更加靈活。