I looked at this SO question.
I want to do something similar in EF 5. I don't see the ForeignKey attribute but instead an Association attribute in EF5.
Also, can someone explain what this does/means :
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Match>()
.HasRequired(m => m.HomeTeam)
.WithMany(t => t.HomeMatches)
.HasForeignKey(m => m.HomeTeamId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Match>()
.HasRequired(m => m.GuestTeam)
.WithMany(t => t.AwayMatches)
.HasForeignKey(m => m.GuestTeamId)
.WillCascadeOnDelete(false);
}
This is the explanation :
Primary keys are mapped by default convention. Team must have two collection of matches. You can't have single collection referenced by two FKs. Match is mapped without cascading delete because it doesn't work in these self referencing many-to-many.
What I want to do is very similar to the example in the link but I don't know :
- When I need to modify DbContext
- When the primary keys will link to each other
- When I need to explicitly use Association to create the relationship
Any explanation is appreciated.