Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. public class BaseContext : BaseDbContext
  2. {
  3. protected BaseWebsiteContext(DbContextOptions dbContextOptions) : base(dbContextOptions)
  4. {
  5. }
  6.  
  7. public DbSet<Image> Images { get; set; }
  8. public DbSet<Tag> Tags { get; set; }
  9. public DbSet<ImageTags> ImageTags { get; set; }
  10. }
  11.  
  12. public class Image : Entity.Entity
  13. {
  14. public string Title { get; set; }
  15. public string Filepath { get; set; }
  16. public DateTime CreateDate { get; set; }
  17.  
  18. public ICollection<ImageTags> ImageTags { get; set; } = new List<ImageTags>();
  19. }
  20.  
  21. public class Tag : Entity.Entity
  22. {
  23. public string Name { get; set; }
  24. public string Description { get; set; }
  25.  
  26. public ICollection<ImageTags> ImageTags { get; set; } = new List<ImageTags>();
  27. }
  28.  
  29. public class ImageTags
  30. {
  31. public int ImageId { get; set; }
  32. public Image Image { get; set; }
  33. public int TagId { get; set; }
  34. public Tag Tag { get; set; }
  35. }
  36.  
  37. modelBuilder.Entity<ImageTags>().HasKey(x => new {x.ImageId, x.TagId});
  38.  
  39. return await _dataContext.Images
  40. .Include(x => x.ImageTags)
  41. .ThenInclude(x => x.Tag)
  42. .Where(w => w.ImageTags.Any(x => x.TagId == tagId))
  43. .ToListAsync();
  44.  
  45. SELECT [x.ImageTags].[ImageId], [x.ImageTags].[TagId], [i.Tag].[Id], [i.Tag].[Description], [i.Tag].[Name]
  46. FROM [ImageTags] AS [x.ImageTags]
  47. INNER JOIN [Tags] AS [i.Tag] ON [x.ImageTags].[TagId] = [i.Tag].[Id]
  48. INNER JOIN (
  49. SELECT [x1].[Id]
  50. FROM [Images] AS [x1]
  51. WHERE EXISTS (
  52. SELECT 1
  53. FROM [ImageTags] AS [x2]
  54. WHERE ([x2].[TagId] = @__tagId_0) AND ([x1].[Id] = [x2].[ImageId]))
  55. ) AS [t] ON [x.ImageTags].[ImageId] = [t].[Id]
  56. ORDER BY [t].[Id]
  57.  
  58. modelBuilder.Entity<Tag>().ToTable("Tag");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement