daily pastebin goal
63%
SHARE
TWEET

Untitled

a guest Jan 20th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public abstract class AuthorizedDbContext : DbContext
  2. {
  3.     //...
  4.     protected override void OnModelCreating(ModelBuilder modelBuilder)
  5.     {
  6.         var constraintOptions = this._authorizationOptions.ConstraintOptions;
  7.         constraintOptions.ApplyStaticConstraint(modelBuilder, this);
  8.         base.OnModelCreating(modelBuilder);
  9.     }
  10.  
  11.     public async override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default(CancellationToken))
  12.     {
  13.         this.VerifyResourceAccess();
  14.         return await base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
  15.     }
  16.  
  17.     protected void VerifyResourceAccess()
  18.     {
  19.         if (false == ChangeTracker.HasChanges())
  20.         {
  21.             return;
  22.         }
  23.  
  24.         //WARNING: if you don't set the change tracking behavior to no tracking, you could reload entities by accident
  25.         var previousTrackingBehavior = this.ChangeTracker.QueryTrackingBehavior;
  26.         this.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
  27.  
  28.         this._authorizationOptions.ContextValidator.ValidateAndThrow(this);
  29.  
  30.         this.ChangeTracker.QueryTrackingBehavior = previousTrackingBehavior;
  31.     }
  32. }
  33.    
  34. //Pseudo-Code
  35. AuthorizedDbContextScope<T>: IDisposable
  36.     where T: DbContext
  37. {
  38.     public void ApplyConstraints()
  39.     {
  40.         this._options.DynamicConstraints.Foreach(EntityFrameWorkZ.ApplyDynamicFilterFromAction)
  41.     }
  42. }
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