Hello again.
I had a problem with configuring many-to-many that it solved, thanks to @Edward Z.
Now i want to use it for CRUD operations.
How can i add related data, How can i get related data, how can i update and delete related data?
Again my models and configurations :
public class MyPost { [Key] public int Id { get; set; } public string Title { get; set; } public string Content { get; set; } public List<PostTag> PostTags { get; set; } } public class Tag { [Key] public int Id { get; set; } public string Title { get; set; } public List<PostTag> PostTags { get; set; } } public class PostTag { public int PostId { get; set; } public MyPost MyPost { get; set; } public int TagId { get; set; } public Tag Tag { get; set; } } public class BlogDbContext : IdentityDbContext<User> { public BlogDbContext(DbContextOptions<BlogDbContext> options) : base(options){} public DbSet<MyPost> MyPosts { get; set; } public DbSet<Tag> Tags { get; set; } public DbSet<PostTag> PostTags { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<PostTag>() .HasKey(t => new { t.PostId, t.TagId }); modelBuilder.Entity<PostTag>() .HasOne(pt => pt.MyPost) .WithMany(p => p.PostTags) .HasForeignKey(pt => pt.PostId); modelBuilder.Entity<PostTag>() .HasOne(pt => pt.Tag) .WithMany(t => t.PostTags) .HasForeignKey(pt => pt.TagId); base.OnModelCreating(modelBuilder); } }
Think i want to show all posts and tags in index page, Add a post and in add form i want to add some tags to post, in list of posts when i delete a post it should delete all tags related to this post, and update post and tags.
And another question, is it fine to delete posts whene i delete a tag?