【发布时间】:2017-11-20 21:08:02
【问题描述】:
作为学习新事物的一种方式,我决定尝试创建一个网站,以使用 EF 的代码优先数据库在 C# .Net Core MVC 中跟踪赛车结果。
但是,我想知道我的数据结构完全是错误的方法。
我有 Nation 和 Season 的模型,它们非常简单(ID、Name 和 Season 的 Races 集合)。然而,我完成 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; }
}
这是 Driver 的模型:
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; }
}
为了完成这项工作,我认为我必须像这样在 Context 类中设置关系...
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);
}
但是当我尝试更新数据库时,我收到一个错误,即这种结构会导致“循环或多个级联路径”,这听起来确实不太好。我知道可以将 Cascade Delete 设置为关闭,但错误消息让我认为我一开始就真的走错了路……我在这里完全是在吠叫错误的树吗?
任何帮助或建议将不胜感激!
【问题讨论】:
-
您不应该有 26 个单独的导航属性。只需使用 Race 和 Driver 之间的多对多关系,可能使用称为 RaceParticipant 之类的显式链接实体。
标签: c# asp.net-mvc entity-framework model-view-controller asp.net-core