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

It seems very clear to me, but EF (and sql) keeps complaining, no matter what moves I make to the hasmanny/hasrequired stuff. I can't even find google help cause I don't know the name of what I'm trying to implement (double one to one???!!!)

The idea is that an Event must have 2 not null Participants (first & second only, not many) and that each Participant may have many Events

Thanks

1 Answer 1

2

There are multiple problems in your code:

  • PK cannot be nullable
  • FK cannot be nullable if you want to define the relation as Required
  • When you define two relations with Participant you need two navigation properties - you cannot map two relations into single Events property
  • You didn't complete your mapping of CreatedBy in both Event and Participant

Try this:

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);  
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @ladislav-mrnka I'm answering from my job's account, so I can't check the solution right now (it's for my home projects), but I have following questions: 1) Participant.Events would be now Participant.FirstEvents + Participant.SecondEvents??? Will this create extra fields (or tables) in database??? 2) I make properties Nullable cause I prefer to check for null rather than default value... should I abandon this idea??? how are you actually handling it??? 3) how do you call this relationship???
1) Yes your Events collection is split between those two collections. It will not create any additional DB constructs. 2) Null has special meaning which is not allowed by your model. You cannot use it. 3) This is just more complex version of many to many relation where junction table (Event) contains additional data.
Hi @Ladislav-Mrnka thanks for your response. I finally have time to implement it, but it didn't work. I removed all nullables, split Events into First & Second, and completed the user foreign keys, but it still throws: Introducing FOREIGN KEY constraint 'FK_Event_Participant_SecondParticipantID' on table 'Event' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint. Do you think I'm missing something???
Hi @Ladislav-Mrnka I added the WillCascadeOnDelete statement you suggested in the other post and it worked. Thanks for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.