How to combine one-to-many relationship and many-to-many relationship in Entity Framework Core

Pham Nam 1 Reputation point
2021-05-11T10:53:48.153+00:00

My requirement is design a system that: Allow a user to create a post, tag one or more other users in post. So I tried to create 3 classes of entities as below:

public class User 
    { 
        public int Id { get; set; } 
        public string Username { get; set; } 
        public List<Post> CreatedPosts{ get; set; }.  //To get all posts user created 
        public List<PostTag> PostTags{ get; set; }.   //Use with PostTag table to get all posts user was tagged 
    }  

public class Post 
    { 
        public int Id { get; set; } 
        public string Title { get; set; } 
        public User Author { get; set; }.  //To set user who created post  
        public List<PostTag> PostTags { get; set; }.  
    } 

public class PostTag 
    { 
        public int PostId { get; set; } 
        public Post Post { get; set; } 
        public int UserId { get; set; } 
        public User User { get; set; }  //user who tagged in post 
    } 

Because a user can create many post so it will be a one-to-many relationship. A user can be tagged in many posts and many users can be tagged in a post so it will be a many-to-many relationship. So I tried to configure using Fluent API as below:

public class ApplicationDbContext : DbContext 
    { 
        public DbSet<User> Users { get; set; } 

        public DbSet<Post> Posts { get; set; } 

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
        { 
                optionsBuilder.UseSqlServer(@"Server=localhost\SQLEXPRESS;Database=DbRelationshipDB;Trusted_Connection=True;MultipleActiveResultSets=true");
        } 

        protected override void OnModelCreating(ModelBuilder modelBuilder) 
        { 
            //Configure One-to-many for: a user can create many posts 
            modelBuilder.Entity<User>() 
                .HasMany(x => x. CreatedPosts) 
                .WithOne(x => x. Author) 
                .IsRequired(); 

            //Configure many-to-many for: a post can contains many users and a user can be tagged in many posts 
            modelBuilder.Entity<PostTag>() 
                .HasKey(t => new { t.PostId, t.UserId }); 

            modelBuilder.Entity<PostTag>() 
                .HasOne(x => x.Post) 
                .WithMany(x => x. PostTags) 
                .HasForeignKey(x => x.PostId); 

            modelBuilder.Entity<PostTag>() 
                .HasOne(x => x. User) 
                .WithMany(x => x.PostTags) 
                .HasForeignKey(x => x.UserId); 
        } 
    } 

I could add migration successfully but could not update the database. Please help me how to configure this.
I am sorry English not my tongue language.

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
726 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
{count} votes