One to Many Relationships
This article does not yet include information about One-To-Many relationships in Entity Framework Core 7. However, One-To-Many relationships have not changed much since Entity Framework Core 3, so check out this guide for now: Relationships in Entity Framework Core 3
Many To Many Relationships
Many-To-Many relationships in recent versions of Entity Framework Core can be done quite easily with the help of an automatic join entity type, which maps to a join table behind the scenes. While you may still manually configure the join entity type in your model (see docs), you don't have to.
Post Entity:
public class Post {
[Key]
public int Id { get; set; }
public string Title { get; set; }
public String Content { get; set; }
public List<Tag> Tags { get; set; }
}
Tag Entity:
public class Tag {
[Key]
public int Id { get; set; }
public string Name { get; set; }
public List<Post> Posts { get; set; }
}
DB Context Class:
public class BlogDB : DbContext {
public BlogDB(DbContextOptions<BlogDB> options) : base(options) { }
public DbSet<Post> Posts { get; set; }
public DbSet<Tag> Tags { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder) { }
}
Customizing Automatic Join Table Details
Entity Framework will use a join entity type and a join table behind the scenes to associate Posts and Tags. However convenient, you may not be satisfied with the default table name, default column names, or default column order used in the join table. To override each of those things, some additional code can be added to OnModelCreating().
Updated DB Context Class:
public class BlogDB : DbContext {
public BlogDB(DbContextOptions<BlogDB> options) : base(options) { }
public DbSet<Post> Posts { get; set; }
public DbSet<Tag> Tags { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder) {
// Specify join table name and column names
builder.Entity<Post>()
.HasMany(p => p.Tags)
.WithMany(t => t.Posts)
.UsingEntity<Dictionary<string, object>>
(
"PostTags", // Override default join table name
e => e.HasOne<Tag>().WithMany().HasForeignKey("TagId"), // Override default column name
e => e.HasOne<Post>().WithMany().HasForeignKey("PostId"), // Override default column name
e => e.HasKey("PostId", "TagId") // Override default column order
);
}
}
This should provide you with a join table named PostTags (instead of a possible default of TagPosts), column names "PostId" and "TagId" (instead of possible defaults like "PostsId" and "TagsId"), and a column order of PostId, TagId (instead of a possible default of TagId, PostId).
More customizations can be found in the official docs.
Adding, Editing, Removing Data
This article does not yet include updated code samples for adding, editing, removing, and selecting data for One-To-Many relationships in Entity Framework Core 7. However, the following other articles may cover what you're looking for and may work as-is or need very slight adjustments:
Relationships in Entity Framework Core 3
Relationships in Entity Framework 6