Sub topic 10
Modeling Relationships Between Entities
Real data is connected. A blog has many posts, and a post can carry many tags. A relationship is a link between two entities, stored in the database as a foreign key. A foreign key is a column that holds the primary key of a row in another table. EF Core lets you describe these links with ordinary C# properties.
Navigation Properties
A navigation property is a property that points to a related entity, or to a collection of them. EF Core reads these properties and works out the relationship for you.
There are two kinds:
- A reference navigation points to one entity, such as
Post.Blog. - A collection navigation points to many entities, such as
Blog.Posts.
One-to-Many
In a one-to-many relationship, one row links to many rows. One blog has many posts, and each post belongs to exactly one blog.
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; } = "";
public List<Post> Posts { get; } = new();
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; } = "";
public string Content { get; set; } = "";
public int BlogId { get; set; }
public Blog Blog { get; set; } = null!;
}EF Core follows a naming rule here. The Post class has a property called BlogId, which matches the key of Blog, so EF Core makes it the foreign key. You don't need any extra setup.
Many-to-Many
In a many-to-many relationship, rows on both sides can link to many rows on the other side. One post can have many tags, and one tag can belong to many posts.
public class Tag
{
public int TagId { get; set; }
public string Name { get; set; } = "";
public List<Post> Posts { get; } = new();
}
// Add this property to the Post class:
// public List<Tag> Tags { get; } = new();Also add public DbSet<Tag> Tags => Set<Tag>(); to BloggingContext. A database can't store a list inside a single column, so EF Core creates a join table. A join table is a table whose rows each pair one post with one tag. EF Core names it PostTag and fills it in for you.
Using Relationships in Code
You link entities by adding them to navigation properties. EF Core fills in the foreign keys when you save.
using var db = new BloggingContext();
var csharp = new Tag { Name = "csharp" };
var blog = new Blog { Url = "https://example.com/blog" };
var post = new Post { Title = "Hello", Content = "First post" };
post.Tags.Add(csharp);
blog.Posts.Add(post);
db.Blogs.Add(blog);
await db.SaveChangesAsync();One SaveChangesAsync call inserts the blog, the post, the tag, and the join row. EF Core inserts them in the right order so that each foreign key points to a row that already exists.
After you change the model, add a migration so the database gets the new tables:
dotnet ef migrations add AddTags
dotnet ef database update| Relationship | Example | How it is stored |
|---|---|---|
| One-to-many | Blog and Posts | Foreign key column on `Posts` |
| Many-to-many | Posts and Tags | Join table `PostTag` |
Exercise
You add a Comment class with CommentId, Text, PostId, and Post properties. What else should you add to Post so that you can reach a post's comments from the post, and what kind of relationship is this?
Show answer
Add a collection navigation, public List<Comment> Comments { get; } = new();, to Post. This is a one-to-many relationship: one post has many comments, and Comment.PostId is the foreign key.
Exercise
Why does EF Core need a join table for posts and tags but not for blogs and posts?
Show answer
Each post belongs to only one blog, so one foreign key column on Posts is enough. A post can have many tags and a tag can have many posts, and a single column can't hold many values, so each pair is stored as a row in a separate join table.