Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BaseContext : BaseDbContext
- {
- protected BaseWebsiteContext(DbContextOptions dbContextOptions) : base(dbContextOptions)
- {
- }
- public DbSet<Image> Images { get; set; }
- public DbSet<Tag> Tags { get; set; }
- public DbSet<ImageTags> ImageTags { get; set; }
- }
- public class Image : Entity.Entity
- {
- public string Title { get; set; }
- public string Filepath { get; set; }
- public DateTime CreateDate { get; set; }
- public ICollection<ImageTags> ImageTags { get; set; } = new List<ImageTags>();
- }
- public class Tag : Entity.Entity
- {
- public string Name { get; set; }
- public string Description { get; set; }
- public ICollection<ImageTags> ImageTags { get; set; } = new List<ImageTags>();
- }
- public class ImageTags
- {
- public int ImageId { get; set; }
- public Image Image { get; set; }
- public int TagId { get; set; }
- public Tag Tag { get; set; }
- }
- modelBuilder.Entity<ImageTags>().HasKey(x => new {x.ImageId, x.TagId});
- return await _dataContext.Images
- .Include(x => x.ImageTags)
- .ThenInclude(x => x.Tag)
- .Where(w => w.ImageTags.Any(x => x.TagId == tagId))
- .ToListAsync();
- SELECT [x.ImageTags].[ImageId], [x.ImageTags].[TagId], [i.Tag].[Id], [i.Tag].[Description], [i.Tag].[Name]
- FROM [ImageTags] AS [x.ImageTags]
- INNER JOIN [Tags] AS [i.Tag] ON [x.ImageTags].[TagId] = [i.Tag].[Id]
- INNER JOIN (
- SELECT [x1].[Id]
- FROM [Images] AS [x1]
- WHERE EXISTS (
- SELECT 1
- FROM [ImageTags] AS [x2]
- WHERE ([x2].[TagId] = @__tagId_0) AND ([x1].[Id] = [x2].[ImageId]))
- ) AS [t] ON [x.ImageTags].[ImageId] = [t].[Id]
- ORDER BY [t].[Id]
- modelBuilder.Entity<Tag>().ToTable("Tag");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement