MeGaDeTH_91

BlogDbContext

Aug 16th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.71 KB | None | 0 0
  1. namespace MyBlog.Data
  2. {
  3. using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
  4. using Microsoft.EntityFrameworkCore;
  5. using MyBlog.Data.EntityConfiguration.ArticlesConfiguration;
  6. using MyBlog.Data.EntityConfiguration.BooksConfiguration;
  7. using MyBlog.Data.EntityConfiguration.BrandsConfiguration;
  8. using MyBlog.Data.EntityConfiguration.FunConfiguration;
  9. using MyBlog.Data.EntityConfiguration.GamesConfiguration;
  10. using MyBlog.Data.EntityConfiguration.MusicConfiguration.ArtistsConfiguration;
  11. using MyBlog.Data.EntityConfiguration.MusicConfiguration.InstrumentsConfiguration;
  12. using MyBlog.Data.EntityConfiguration.MusicConfiguration.SongsConfiguration;
  13. using MyBlog.Data.EntityConfiguration.ProductsConfiguration;
  14. using MyBlog.Data.EntityConfiguration.ReviewsConfiguration;
  15. using MyBlog.Models.Users;
  16. using MyBlog.Models.Articles;
  17. using MyBlog.Models.Books;
  18. using MyBlog.Models.Brands;
  19. using MyBlog.Models.Fun;
  20. using MyBlog.Models.Games;
  21. using MyBlog.Models.Music.Artists;
  22. using MyBlog.Models.Music.Instruments;
  23. using MyBlog.Models.Music.Songs;
  24. using MyBlog.Models.ProductsForSale;
  25. using MyBlog.Models.Reviews;
  26. using System.Reflection;
  27. using System;
  28. using System.Linq;
  29. using MyBlog.Data.EntityConfiguration.UsersConfiguration;
  30. using System.Collections.Generic;
  31.  
  32. public class BlogDataDbContext : IdentityDbContext<User>
  33. {
  34. public BlogDataDbContext(DbContextOptions<BlogDataDbContext> options)
  35. : base(options)
  36. {
  37. }
  38.  
  39. public DbSet<Article> Articles { get; set; }
  40. public DbSet<ArticleCategory> ArticleCategories { get; set; }
  41.  
  42. public DbSet<Book> Books { get; set; }
  43. public DbSet<BookCategory> BookCategories { get; set; }
  44. public DbSet<BookAuthor> BookAuthors { get; set; }
  45. public DbSet<BookAuthorGenre> BookAuthorGenres { get; set; }
  46.  
  47. public DbSet<Brand> Brands { get; set; }
  48. public DbSet<BrandType> BrandTypes { get; set; }
  49.  
  50. public DbSet<Joke> Jokes { get; set; }
  51. public DbSet<Meme> Memes { get; set; }
  52.  
  53. public DbSet<Game> Games { get; set; }
  54. public DbSet<GameType> GameTypes { get; set; }
  55.  
  56. public DbSet<Artist> Artists { get; set; }
  57. public DbSet<ArtistType> ArtistTypes { get; set; }
  58.  
  59. public DbSet<Instrument> Instruments { get; set; }
  60. public DbSet<InstrumentType> InstrumentTypes { get; set; }
  61.  
  62. public DbSet<Song> Songs { get; set; }
  63. public DbSet<SongGenre> SongGenres { get; set; }
  64.  
  65. public DbSet<Product> Products { get; set; }
  66. public DbSet<ProductType> ProductTypes { get; set; }
  67.  
  68. public DbSet<Review> Reviews { get; set; }
  69. public DbSet<ReviewType> ReviewTypes { get; set; }
  70.  
  71.  
  72. public DbSet<UserArticles> FavouriteUserArticles { get; set; }
  73.  
  74. public DbSet<UserArtists> FavouriteUserArtists { get; set; }
  75.  
  76. public DbSet<UserBooks> FavouriteUserBooks { get; set; }
  77.  
  78. public DbSet<UserBrands> FavouriteUserBrands { get; set; }
  79.  
  80. public DbSet<UserJokes> FavouriteUserJokes { get; set; }
  81.  
  82. public DbSet<UserMemes> FavouriteUserMemes { get; set; }
  83.  
  84. public DbSet<UserGames> FavouriteUserGames { get; set; }
  85.  
  86. public DbSet<UserInstruments> FavouriteUserInstruments { get; set; }
  87.  
  88. public DbSet<UserSongs> FavouriteUserSongs { get; set; }
  89.  
  90. public DbSet<UserProducts> UserBoughtProducts { get; set; }
  91.  
  92. public DbSet<UserReviews> FavouriteUserReviews { get; set; }
  93.  
  94. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  95. {
  96. if (!optionsBuilder.IsConfigured)
  97. {
  98. optionsBuilder.UseSqlServer(ServerConfig.ConnectionString);
  99. }
  100.  
  101. base.OnConfiguring(optionsBuilder);
  102. }
  103.  
  104. protected override void OnModelCreating(ModelBuilder builder)
  105. {
  106. base.OnModelCreating(builder);
  107.  
  108. ApplyAllEntityConfigurations(builder);
  109.  
  110. // Uncomment if the Reflection strategy above is not working for some reason
  111.  
  112. //builder.ApplyConfiguration(new ArticleCategoryConfig());
  113. //builder.ApplyConfiguration(new ArticleConfig());
  114.  
  115. //builder.ApplyConfiguration(new BookAuthorConfig());
  116. //builder.ApplyConfiguration(new BookAuthorGenreConfig());
  117. //builder.ApplyConfiguration(new BookCategoryConfig());
  118. //builder.ApplyConfiguration(new BookConfig());
  119.  
  120. //builder.ApplyConfiguration(new BrandConfig());
  121. //builder.ApplyConfiguration(new BrandTypeConfig());
  122.  
  123. //builder.ApplyConfiguration(new JokeConfig());
  124. //builder.ApplyConfiguration(new MemeConfig());
  125.  
  126. //builder.ApplyConfiguration(new GameConfig());
  127. //builder.ApplyConfiguration(new GameTypeConfig());
  128.  
  129. //builder.ApplyConfiguration(new ArtistConfig());
  130. //builder.ApplyConfiguration(new ArtistTypeConfig());
  131.  
  132. //builder.ApplyConfiguration(new InstrumentConfig());
  133. //builder.ApplyConfiguration(new InstrumentTypeConfig());
  134.  
  135. //builder.ApplyConfiguration(new SongConfig());
  136. //builder.ApplyConfiguration(new SongGenreConfig());
  137.  
  138. //builder.ApplyConfiguration(new ProductConfig());
  139. //builder.ApplyConfiguration(new ProductTypeConfig());
  140.  
  141. //builder.ApplyConfiguration(new ReviewConfig());
  142. //builder.ApplyConfiguration(new ReviewTypeConfig());
  143.  
  144. //builder.ApplyConfiguration(new UserArticlesConfig());
  145. //builder.ApplyConfiguration(new UserArtistsConfig());
  146. //builder.ApplyConfiguration(new UserBooksConfig());
  147. //builder.ApplyConfiguration(new UserBrandsConfig());
  148. //builder.ApplyConfiguration(new UserJokesConfig());
  149. //builder.ApplyConfiguration(new UserMemesConfig());
  150. //builder.ApplyConfiguration(new UserGamesConfig());
  151. //builder.ApplyConfiguration(new UserInstrumentsConfig());
  152. //builder.ApplyConfiguration(new UserSongsConfig());
  153. //builder.ApplyConfiguration(new UserProductsConfig());
  154. //builder.ApplyConfiguration(new UserReviewsConfig());
  155. }
  156.  
  157. private static void ApplyAllEntityConfigurations(ModelBuilder builder)
  158. {
  159. var applyGenericMethod = typeof(ModelBuilder).GetMethods().Where(x => x.Name.StartsWith("ApplyConfiguration")).ToList().First();
  160. // replace GetExecutingAssembly with assembly where your configurations are if necessary
  161. foreach (var type in Assembly.GetExecutingAssembly().GetTypes()
  162. .Where(c => c.IsClass && !c.IsAbstract && !c.ContainsGenericParameters))
  163. {
  164. // use type.Namespace to filter by namespace if necessary
  165. foreach (var iface in type.GetInterfaces())
  166. {
  167. // if type implements interface IEntityTypeConfiguration<SomeEntity>
  168. if (iface.IsConstructedGenericType && iface.GetGenericTypeDefinition() == typeof(IEntityTypeConfiguration<>))
  169. {
  170. // make concrete ApplyConfiguration<SomeEntity> method
  171. var applyConcreteMethod = applyGenericMethod.MakeGenericMethod(iface.GenericTypeArguments[0]);
  172. // and invoke that with fresh instance of your configuration type
  173. applyConcreteMethod.Invoke(builder, new object[] { Activator.CreateInstance(type) });
  174. break;
  175. }
  176. }
  177. }
  178. }
  179. }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment