Let's start with a few changes in the models.
- In this case, there is a cycle and we need to make foreign keys nullable.
- Make properties
public
public class Review
{
public int Id { get; set; }
public string Level1 { get; set; }
public string Level2 { get; set; }
public string Level3 { get; set; }
public List<File>? FilesForMainReview { get; set; }
public List<File>? FilesForSecondaryReview { get; set; }
public List<File>? FilesForOtherReview { get; set; }
}
public class File
{
public int Id { get; set; }
public int? MainReviewId { get; set; }
public Review? MainReview { get; set; }
public int? SecondaryReviewId { get; set; }
public Review? SecondaryReview { get; set; }
public int? OtherReviewId { get; set; }
public Review? OtherReview { get; set; }
}
- For these kind of relations we can use FluentAPI
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Review>().HasMany(x => x.FilesForMainReview).WithOne(x => x.MainReview).HasForeignKey(x => x.MainReviewId).OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Review>().HasMany(x => x.FilesForSecondaryReview).WithOne(x => x.SecondaryReview).HasForeignKey(x => x.SecondaryReviewId).OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Review>().HasMany(x => x.FilesForOtherReview).WithOne(x => x.OtherReview).HasForeignKey(x => x.OtherReviewId).OnDelete(DeleteBehavior.Restrict);
}
These are my sample data:
SET IDENTITY_INSERT [dbo].[Review] ON
GO
INSERT [dbo].[Review] ([Id], [Level1], [Level2], [Level3]) VALUES (1, N'R1L1', N'R1L2', N'R1L3')
GO
INSERT [dbo].[Review] ([Id], [Level1], [Level2], [Level3]) VALUES (2, N'R2L1', N'R2L2', N'R2L3')
GO
INSERT [dbo].[Review] ([Id], [Level1], [Level2], [Level3]) VALUES (3, N'R3L1', N'R3L2', N'R3L3')
GO
SET IDENTITY_INSERT [dbo].[Review] OFF
GO
SET IDENTITY_INSERT [dbo].[File] ON
GO
INSERT [dbo].[File] ([Id], [MainReviewId], [SecondaryReviewId], [OtherReviewId]) VALUES (2, 1, 2, 3)
GO
INSERT [dbo].[File] ([Id], [MainReviewId], [SecondaryReviewId], [OtherReviewId]) VALUES (5, 2, 1, 3)
GO
INSERT [dbo].[File] ([Id], [MainReviewId], [SecondaryReviewId], [OtherReviewId]) VALUES (6, 3, 2, 1)
GO
SET IDENTITY_INSERT [dbo].[File] OFF
GO
And in the end, this is the code and the result:
using (var db = new MyDbContext())
{
var result = db.File.Include(x => x.MainReview).Include(x => x.SecondaryReview).Include(x => x.OtherReview).ToList();
}
