Mapping Relationships in EF Core: Julie Lerman
Mapping Relationships in EF Core: Julie Lerman
Julie Lerman
MOST TRUSTED AUTHORITY ON ENTITY FRAMEWORK
@julielerman thedatafarm.com
Learning more about one-to-many
relationships
Module
Understanding many-to-many
Overview relationships
Tracking additional data in a join entity
Adding a one-to-one relationship
Visualize the model with EF Core Power
Tools
Migrating the database to reflect model
changes
Learning More About
One-to-Many Relationships
One Samurai Character Can Have Many Quotes
!
?
####
@@@
Convention Over Configuration
Default behavior that can be overridden using
configurations.
public class Samurai
List<Child> {
in Parent public int Id { get; set; }
public string Name { get; set; }
public List<Quote> Quotes { get; set; }
}
Child needs no public class Quote
references back to {
Parent not required public int Id {get;set;}
for EF Core to public string Text {get;set;}
}
understand 1:*
relationship
public class Samurai
List<Child> {
in Parent public int Id { get; set; }
public string Name { get; set; }
public List<Quote> Quotes { get; set; }
}
You can override with mappings, to configure SamuraiFK as the foreign key
public class Samurai
{
List<Child> public int Id { get; set; }
in Parent public string Name { get; set; }
public List<Quote> Quotes { get; set; }
}
Child’s (properly
named) FK public class Quote
property with no {
public int Id {get;set;}
navigation is public string Text {get;set;}
recognized as FK public int
Samurai Samurai
SamuraiId {get;set;}
{get;set;}
}
public class Quote t Only a navigation property to Samurai
{ You must have a samurai object to
public int Id {get;set;} connect a quote
public string Text {get;set;} samurai.Quotes.Add(thequote)
public Samurai Samurai {get;set;}
thequote.Samurai=someobject
}
modelBuilder.Entity<Samurai>()
modelBuilder.Entity<Samurai>() .HasMany(s => s.Quotes)
.HasMany(s => s.Quotes) .WithOne(q => q.Samurai)
.WithOne(q => q.Samurai) .HasForeignKey(q => q.SamuraiFK);
.HasForeignKey(q => q.SamuraiFK);
EF Core’s Default Many-to-Many Mapping
Many-to-Many support
has changed dramatically in
EF Core 5
Many-to-Many with Skip Navigations
Samurai Battle
List<Battle> List<Samurai>
EF Core 5 Can Interpret Skip Navigations
When?
Samurai Kikuchiyo Battle of Anegawa
Many-to-Many Relationship
Identify the
known
many-to-many
relationship
Configuring the Join Entity
Add more
Identify the information
known to that mapping
many-to-many
relationship
modelBuilder
.Entity<End1>
.HasMany(e1=>e1.E2List )
.WithMany(e2=>e2.E1List)
That other end also is WITH MANY of End1, represented by the E1 list property
modelBuilder.Entity<BattleSamurai>()
modelBuilder.Entity<BattleSamurai>()
Additional Considerations
Default Advanced
Dependent gets its own table Dependent data in principal table
Learned more about 1:* relationships
Learned about EF Core 5’s conventional
Review many-to-many support
Implemented a *:* with payload data
Review
Learned about 1:1 defaults e.g., optional
dependents and separate tables
Visualized model with EF Core Power Tools
More tricks with Fluent API and migrations
Migrated database to apply model changes
Coming Up
Interacting with your
EF Core data model
and logging
Resources
Entity Framework Core on GitHub github.com/dotnet/efcore
Julie Lerman
MOST TRUSTED AUTHORITY ON ENTITY FRAMEWORK
@julielerman thedatafarm.com