2

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 :

  1. When I need to modify DbContext
  2. When the primary keys will link to each other
  3. When I need to explicitly use Association to create the relationship

Any explanation is appreciated.

Community
  • 1
  • 1
Pete
  • 169
  • 2
  • 14

2 Answers2

1

Okay so... I can't answer the ForeignKey attribute question on EF beta for I haven't had the chance to check it out yet.

However...

modelBuilder.Entity<Match>() - Take the entity "Match" and perform following operations on it
.HasRequired(m => m.HomeTeam) - The entity needs to have a non-null navigation HomeTeam...
.WithMany(t => t.HomeMatches) - ... which has a subset of Matches by navigation HomeMatches
.HasForeignKey(m => m.HomeTeamId) ... and the associating foreign key is HomeTeamId on Match
.WillCascadeOnDelete(false); ... and don't cascade when the entity is deleted.

That's the beauty of LINQ, it's more often than not self-documenting.

Now, as for your three questions...

  1. Only modify DbContext when you are changing your model relations or adding/deleting an entity. If you're adding, you need to do a public DbSet Entities { get; set; And remove it if deleting, etc.

  2. Primary keys don't link to each other. Foreign Keys link to Primary Keys. By convention, if you have a ProjectId, a navigation object called Project and another entity called Project with a property called Id, it will automatically map ProjectId from the first entity to the Id of Project entity and give the Project entity as a navigation item to first entity when you fetch data from the DB via EF:

  3. Only if you need non-convention based relationships. Ie, your primary keys are along the lines of "tblId" or "ParentId" instead of "Id" and "ProjectId", for example. Or you need a different kind of behaviour on some items, such as cascading on deletion for only select entities.

NeroS
  • 1,179
  • 1
  • 12
  • 28
1

In EF 5, if you're using Migrations, you can alter the migration code to not implement the cascade delete:

CreateTable(
            "dbo.Match",
            c => new
                {
                    MatchId = c.Long(nullable: false, identity: true),
                    Description = c.String(),
                    HomeTeamId = c.Long(nullable: false),
                 })
            .PrimaryKey(t => t.MatchId)
            .ForeignKey("dbo.Team", t => t.HomeTeamId, cascadeDelete: false)                
            .Index(t => t.MatchId)
            .Index(t => t.HomeTeamId);

    }

Or something like that.

Mike
  • 217
  • 4
  • 16