I have a typical many-to-many relationship with these three tables
[Post] ( [PostId] int, (PK) [Content] nvarchar(max) ... ) [Tag] ( [TagId] int, (PK) [Name] nvarchar ... ) [TagPost] ( [TagId] int, (PK, FK) [PostId] int (PK, FK) )
And, TagId and PostId are the PK and FK installed in the tables, respectively. Then I have these classes and mapping in C #
public class Post { public Post() { this.Tags = new HashSet<Tag>(); } [Key] public int PostId { get; set; } ... public virtual ICollection<Tag> Tags { get; private set; } } public class Tag { public Tag() { this.Posts = new HashSet<Post>(); } [Key] public int TagId { get; set; } ... public virtual ICollection<Post> Posts { get; private set; } } internal class MyDbContext : DbContext { public DbSet<Post> Posts { get; set; } public DbSet<Tag> Tags { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<Post>().ToTable("Post"); modelBuilder.Entity<Tag>().ToTable("Tag"); modelBuilder.Entity<Post>() .HasMany(x => x.Tags) .WithMany(x => x.Posts) .Map(x => { x.ToTable("TagPost"); x.MapLeftKey("PostId"); x.MapRightKey("TagId"); }); }
Then I have this code to request them
var list = (from p in ctx.Posts.Include(p => p.Tags) from t in p.Tags where ...
This union returns the messages I was looking for, however the returned messages do not fill in the associated tags, although I have Include. Can someone help to indicate what I am missing so that I can return the message tags?
Thank you very much.
source share