SHARE
TWEET

Untitled

a guest Feb 19th, 2020 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. namespace CinemaWorld.Data
  2. {
  3.     using System;
  4.     using System.Linq;
  5.     using System.Reflection;
  6.     using System.Threading;
  7.     using System.Threading.Tasks;
  8.  
  9.     using CinemaWorld.Data.Common.Models;
  10.     using CinemaWorld.Data.Models;
  11.  
  12.     using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
  13.     using Microsoft.EntityFrameworkCore;
  14.  
  15.     public class CinemaWorldDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string>
  16.     {
  17.         private static readonly MethodInfo SetIsDeletedQueryFilterMethod =
  18.             typeof(CinemaWorldDbContext).GetMethod(
  19.                 nameof(SetIsDeletedQueryFilter),
  20.                 BindingFlags.NonPublic | BindingFlags.Static);
  21.  
  22.         public CinemaWorldDbContext(DbContextOptions<CinemaWorldDbContext> options)
  23.             : base(options)
  24.         {
  25.         }
  26.  
  27.         public DbSet<Actor> Actors { get; set; }
  28.  
  29.         public DbSet<Author> Authors { get; set; }
  30.  
  31.         public DbSet<Cinema> Cinemas { get; set; }
  32.  
  33.         public DbSet<Country> Countries { get; set; }
  34.  
  35.         public DbSet<Director> Director { get; set; }
  36.  
  37.         public DbSet<Genre> Genres { get; set; }
  38.  
  39.         public DbSet<Hall> Halls { get; set; }
  40.  
  41.         public DbSet<Movie> Movies { get; set; }
  42.  
  43.         public DbSet<MovieActor> MovieActors { get; set; }
  44.  
  45.         public DbSet<MovieCountry> MovieCountries { get; set; }
  46.  
  47.         public DbSet<MovieDirector> MovieDirectors { get; set; }
  48.  
  49.         public DbSet<MovieGenre> MovieGenres { get; set; }
  50.  
  51.         public DbSet<MovieNews> MovieNews { get; set; }
  52.  
  53.         public DbSet<MovieProjection> MovieProjections { get; set; }
  54.  
  55.         public DbSet<News> News { get; set; }
  56.  
  57.         public DbSet<Promotion> Promotions { get; set; }
  58.  
  59.         public DbSet<Review> Reviews { get; set; }
  60.  
  61.         public DbSet<ReviewAuthor> ReviewAuthors { get; set; }
  62.  
  63.         public DbSet<SaleTransaction> SaleTransactions { get; set; }
  64.  
  65.         public DbSet<Seat> Seats { get; set; }
  66.  
  67.         public DbSet<Seller> Sellers { get; set; }
  68.  
  69.         public DbSet<Setting> Settings { get; set; } // Default from template
  70.  
  71.         public DbSet<Ticket> Tickets { get; set; }
  72.  
  73.         public override int SaveChanges() => this.SaveChanges(true);
  74.  
  75.         public override int SaveChanges(bool acceptAllChangesOnSuccess)
  76.         {
  77.             this.ApplyAuditInfoRules();
  78.             return base.SaveChanges(acceptAllChangesOnSuccess);
  79.         }
  80.  
  81.         public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default) =>
  82.             this.SaveChangesAsync(true, cancellationToken);
  83.  
  84.         public override Task<int> SaveChangesAsync(
  85.             bool acceptAllChangesOnSuccess,
  86.             CancellationToken cancellationToken = default)
  87.         {
  88.             this.ApplyAuditInfoRules();
  89.             return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
  90.         }
  91.  
  92.         protected override void OnModelCreating(ModelBuilder builder)
  93.         {
  94.             // Needed for many-to-many relationships
  95.             builder.Entity<MovieActor>()
  96.                 .HasKey(ma => new { ma.MovieId, ma.ActorId });
  97.  
  98.             builder.Entity<MovieCountry>()
  99.                 .HasKey(mc => new { mc.MovieId, mc.CountryId });
  100.  
  101.             builder.Entity<MovieDirector>()
  102.                 .HasKey(md => new { md.MovieId, md.DirectorId });
  103.  
  104.             builder.Entity<MovieGenre>()
  105.                 .HasKey(mg => new { mg.MovieId, mg.GenreId });
  106.  
  107.             builder.Entity<MovieNews>()
  108.                 .HasKey(mn => new { mn.MovieId, mn.NewsId });
  109.  
  110.             builder.Entity<ReviewAuthor>()
  111.                 .HasKey(ra => new { ra.ReviewId, ra.AuthorId });
  112.  
  113.             // Needed for Identity models configuration
  114.             base.OnModelCreating(builder);
  115.  
  116.             ConfigureUserIdentityRelations(builder);
  117.  
  118.             EntityIndexesConfiguration.Configure(builder);
  119.  
  120.             var entityTypes = builder.Model.GetEntityTypes().ToList();
  121.  
  122.             // Set global query filter for not deleted entities only
  123.             var deletableEntityTypes = entityTypes
  124.                 .Where(et => et.ClrType != null && typeof(IDeletableEntity).IsAssignableFrom(et.ClrType));
  125.             foreach (var deletableEntityType in deletableEntityTypes)
  126.             {
  127.                 var method = SetIsDeletedQueryFilterMethod.MakeGenericMethod(deletableEntityType.ClrType);
  128.                 method.Invoke(null, new object[] { builder });
  129.             }
  130.  
  131.             // Disable cascade delete
  132.             var foreignKeys = entityTypes
  133.                 .SelectMany(e => e.GetForeignKeys().Where(f => f.DeleteBehavior == DeleteBehavior.Cascade));
  134.  
  135.             foreach (var foreignKey in foreignKeys)
  136.             {
  137.                 foreignKey.DeleteBehavior = DeleteBehavior.Restrict;
  138.             }
  139.         }
  140.  
  141.         private static void ConfigureUserIdentityRelations(ModelBuilder builder)
  142.         {
  143.             builder.Entity<ApplicationUser>()
  144.                 .HasMany(e => e.Claims)
  145.                 .WithOne()
  146.                 .HasForeignKey(e => e.UserId)
  147.                 .IsRequired()
  148.                 .OnDelete(DeleteBehavior.Restrict);
  149.  
  150.             builder.Entity<ApplicationUser>()
  151.                 .HasMany(e => e.Logins)
  152.                 .WithOne()
  153.                 .HasForeignKey(e => e.UserId)
  154.                 .IsRequired()
  155.                 .OnDelete(DeleteBehavior.Restrict);
  156.  
  157.             builder.Entity<ApplicationUser>()
  158.                 .HasMany(e => e.Roles)
  159.                 .WithOne()
  160.                 .HasForeignKey(e => e.UserId)
  161.                 .IsRequired()
  162.                 .OnDelete(DeleteBehavior.Restrict);
  163.         }
  164.  
  165.         private static void SetIsDeletedQueryFilter<T>(ModelBuilder builder)
  166.             where T : class, IDeletableEntity
  167.         {
  168.             builder.Entity<T>().HasQueryFilter(e => !e.IsDeleted);
  169.         }
  170.  
  171.         private void ApplyAuditInfoRules()
  172.         {
  173.             var changedEntries = this.ChangeTracker
  174.                 .Entries()
  175.                 .Where(e =>
  176.                     e.Entity is IAuditInfo &&
  177.                     (e.State == EntityState.Added || e.State == EntityState.Modified));
  178.  
  179.             foreach (var entry in changedEntries)
  180.             {
  181.                 var entity = (IAuditInfo)entry.Entity;
  182.                 if (entry.State == EntityState.Added && entity.CreatedOn == default)
  183.                 {
  184.                     entity.CreatedOn = DateTime.UtcNow;
  185.                 }
  186.                 else
  187.                 {
  188.                     entity.ModifiedOn = DateTime.UtcNow;
  189.                 }
  190.             }
  191.         }
  192.     }
  193. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top