0

何か新しいことを学ぶ方法として、EFを使用したコードの最初のデータベースを使用してC#.Net Core MVCでモーターレーシング結果を追跡するサイトを作成することにしました。データベースがEFコアでモーターレーシング結果を追跡するための構造

私はゲートからまっすぐに私は私のデータ構造と一緒に間違ったアプローチを取っているのだろうかと思っています。

私は、ネイションとシーズンのために、retty straight-forward(ID、名前、シーズンの場合、レースのコレクション)のモデルを持っています。しかし、私がレースとドライバーのモデルをやったやり方は少し複雑です。私は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実際にはあまり良くないとは思わない「サイクルまたは複数のカスケードパス」が発生します。カスケード・デリートをオフにすることは可能ですが、エラー・メッセージは、私が本当に最初の間違った道にいると思うようにしています。私は間違ったツリーを完全に吠えていますか?

ご迷惑をおかけして申し訳ありません。

+0

26個のナビゲーションプロパティを持つべきではありません。おそらく、RaceParticipantやsomesuchのような明示的なリンクエンティティを使用して、RaceとDriverの多対多の関係を使用してください。 –

答えて

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;} 
} 

物事を、そのような事実として、すべてのレースは、IMOの一部であるべき正確に26の参加者を、持っていることをごビジネスロジックであり、データベース設計ではありません。これは、将来、いつか変更される可能性が高い(つまり、レースごとに異なる参加者数を望む場合など)だから、そのロジックをコードに持たせる方がより柔軟に思えます。

関連する問題