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是属于你业务逻辑,而不是数据库设计。这可能是将来某个时候可能会改变的事情(也就是说,如果您希望每场比赛的参赛人数不同)。所以在代码中拥有这样的逻辑似乎更加灵活。