0
class Event{ 
    int? EventID{get;set;} 
    int? FirstParticipantID{get;set;} 
    Participant FirstParticipant{get;set;} 
    int? SecondParticipantID{get;set;} 
    Participant SecondParticipant{get;set;} 
    int? CreatedByID{get;set;} 
    User CreatedBy{get;set;} 
} 


class Participant{ 
    int? ParticipantID{get;set;} 
    List<Event> Events{get;set;} 
    int? CreatedByID{get;set;} 
    User CreatedBy{get;set;} 
} 

protected override void OnModelCreating(DbModelBuilder modelBuilder){ 
    modelBuilder.Entity<Event>().HasRequired(m => m.FirstParticipant).WithMany(m => m.Events).HasForeignKey(m => m.FirstParticipantID); 
    modelBuilder.Entity<Event>().HasRequired(m => m.SecondParticipant).WithMany(m => m.Events).HasForeignKey(m => m.SecondParticipantID); 
    modelBuilder.Entity<Event>().HasRequired(m => m.CreatedBy); 
    modelBuilder.Entity<Participant>().HasRequired(m => m.CreatedBy); 
} 

私には非常にはっきりしているようですが、EF(とSQL)はhasmanny/hasrequiredのものにどのような動きをしても不平を言っています。私は私が実装しようとしているものの名​​前を知らないので、私はgoogleの助けを見つけることができません(!!!!)エンティティフレームワークのコード最初に2つ1対1の関係

アイデアは、 nullの参加者(第一&二だけで、多くはない)と、各参加者は、多くのイベント

おかげ

を有することができること

答えて

2

あなたのコード内の複数の問題があります:

  • PKがNULL可能
  • FKすることはできませんできないあなたが必要な
  • ような関係を定義したい場合は、 Participantとの2つの関係を定義するときは、2つのナビゲーションプロパティを必要とする
  • NULL可能 - あなたは両方でCreatedByのあなたのマッピングを完了しなかった単一Eventsプロパティ
  • に2つの関係をマッピングすることはできませんEventParticipant

はこれを試してみてください:私は私の仕事の口座から答えてるので、私は、ゾルを確認することができませんラディスラフ・mrnka @

public class Event{ 
    public int EventID {get;set;} 
    public int FirstParticipantID{get;set;} 
    public Participant FirstParticipant{get;set;} 
    public int SecondParticipantID{get;set;} 
    public Participant SecondParticipant{get;set;} 
    public int CreatedByID{get;set;} 
    public User CreatedBy{get;set;} 
} 


public class Participant{ 
    public int ParticipantID {get;set;} 
    public int CreatedByID {get;set;} 
    public User CreatedBy {get;set;} 
    public ICollection<Event> FristEvents { get; set; } 
    public ICollection<Event> SecondEvents { get; set; } 
} 

protected override void OnModelCreating(DbModelBuilder modelBuilder){ 
    modelBuilder.Entity<Event>() 
       .HasRequired(m => m.FirstParticipant) 
       .WithMany(m => m.FirstEvents) 
       .HasForeignKey(m => m.FirstParticipantID); 
    modelBuilder.Entity<Event>() 
       .HasRequired(m => m.SecondParticipant) 
       .WithMany(m => m.SecondEvents) 
       .HasForeignKey(m => m.SecondParticipantID); 
    modelBuilder.Entity<Event>() 
       .HasRequired(m => m.CreatedBy) 
       .WithMany() 
       .HasForeignKey(m => m.CreatedByID); 
    modelBuilder.Entity<Participant>() 
       .HasRequired(m => m.CreatedBy) 
       .WithMany() 
       .HasForeignKey(m => m.CreatedByID); 
} 
+0

感謝をution今、私の家のプロジェクトのためですが、私は以下の質問をしています:1)参加者。イベントは今やParticipant.FirstEvents + Participant.SecondEventsになりますか?これはデータベースに余分なフィールド(またはテーブル)を作成しますか? 2)私はデフォルト値ではなくヌルをチェックすることを好むため、プロパティをNullableにする...このアイデアを放棄するべきか?どのように実際にそれを処理していますか? 3)あなたはこの関係をどのように呼んでいますか? –

+0

1)はい、イベントコレクションは2つのコレクションに分かれています。追加のDB構成は作成されません。 2)ヌルは、あなたのモデルによって許可されていない特別な意味を持っています。それを使うことはできません。 3)これは、ジャンクション・テーブル(イベント)に追加のデータが含まれている多対多リレーションのちょっと複雑なバージョンです。 –

+0

こんにちは@ Ladislav-Mrnkaあなたの応答に感謝します。ついにそれを実装する時間がありましたが、うまくいかなかったのです。私はすべてのnullableを削除し、最初と2番目にイベントを分割し、ユーザーの外部キーを完了しましたが、まだスローされます:FOREIGN KEY制約 'FK_Event_Participant_SecondParticipantID'を 'Event'テーブルに導入すると、サイクルまたは複数のカスケードパスが発生する可能性があります。 NO DELETE NO ACTIONまたはUP UP NO NO ACTIONを指定するか、他のFOREIGN KEY制約を変更してください。制約を作成できませんでした。私は何かが不足していると思いますか? –

関連する問題