Communication decided to do with ICollection . I have the following classes. How do we allow to output all Recipient for a certain group? I try to do this, but ThenInclude it does not allow to get Recipient .
{ var group = db.Groups.Include(x => x.GroupRecipients).ThenInclude(y=>y.) .FirstOrDefault(gr => gr.Id == id); } public class Recipient { public int Id { get; set; } public string Name { get; set; } public string Number { get;set; } public ICollection<grouprecipient> GroupRecipients { get; set; } } public class Group { public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } public ICollection<grouprecipient> GroupRecipients { get; set; } } public class GroupRecipient { public int GroupId { get; set; } public Group Group { get; set; } public int RecipientId { get; set; } public Recipient Recipient { get; set; } } public class ApplicationContext : DbContext { public ApplicationContext(DbContextOptions<ApplicationContext> options) : base(options) { } public DbSet<Group> Groups { get; set; } public DbSet<Recipient> Recipients { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<GroupRecipient>() .HasKey(t => new { t.GroupId, t.RecipientId }); modelBuilder.Entity<GroupRecipient>() .HasOne(sc => sc.Group) .WithMany(s => s.GroupRecipients) .HasForeignKey(sc => sc.GroupId); modelBuilder.Entity<GroupRecipient>() .HasOne(sc => sc.Recipient) .WithMany(c => c.GroupRecipients) .HasForeignKey(sc => sc.RecipientId); } } Method in controller
[HttpGet("{id}")] public Group Get(int id) { var group = db.Groups .Include(x => x.GroupRecipients) .ThenInclude(gr => gr.Recipient) .FirstOrDefault(x => x.Id == id); return group; }
.ThenInclude(y=>y.Recipient). - null