defining-one-to-many-relationships-slides
defining-one-to-many-relationships-slides
Relationships
Julie Lerman
EF Core Expert and Software Coach
@julielerman | thedatafarm.com
Install a tool to visualize the data model
d er
min
R e
Override with Override with
Conventions Fluent Mappings Data Annotations
Default assumptions Apply in DbContext
Apply in entity
using Fluent API
public class Book t This child has a reference back to parent aka
{ “Navigation Property”
public int Id { get; set; } t It already understands the one-to-many
public string Title { get; set; } because of the parent’s List<Child>
public Author Author { get; set; } t The reference back to parent aka
} “Navigation Property” is a bonus
t AuthorId FK will be inferred in the database
FK is recognized because
of reference from parent
PubContext.cs
mybook.AuthorId=23
When/Why Ditch Navigation to the
Parent?
Considering the Parent Navigation Property
Tip: Default to no
Only add it if your biz logic requires it!
navigation property
Why have FK property?
public class Book t Only a navigation property to Author.
{ You must have an author in memory to
public int Id {get;set;} connect a book.
public string Title {get;set;} author.Books.Add(abook)
public Author Author{get;set;} abook.Author=someauthor
}
PubContext.cs