Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. public class ReadonlyDbContext : MyDbContext
  2. {
  3. static ReadonlyDbContext()
  4. {
  5. Database.SetInitializer<ReadonlyDbContext>(null);
  6. }
  7.  
  8. public ReadonlyDbContext()
  9. {
  10. SetConfigurationOptions();
  11. }
  12.  
  13. private void SetConfigurationOptions()
  14. {
  15. Configuration.LazyLoadingEnabled = false;
  16. Configuration.AutoDetectChangesEnabled = false;
  17. Configuration.ProxyCreationEnabled = false;
  18. }
  19.  
  20. public override int SaveChanges()
  21. {
  22. throw new Exception("Attempted to save using a read-only dbContext");
  23. }
  24.  
  25. public override Task<int> SaveChangesAsync()
  26. {
  27. throw new Exception("Attempted to save using a read-only dbContext");
  28. }
  29.  
  30. public override Task<int> SaveChangesAsync(CancellationToken cancellationToken)
  31. {
  32. throw new Exception("Attempted to save using a read-only dbContext");
  33. }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement