Advertisement
Guest User

EF no clearcache causes error on multiple updates

a guest
Aug 20th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.26 KB | None | 0 0
  1.             using (var ctx = new EntityContext())
  2.             {
  3.                 var newCustomer = new Customer
  4.                 {
  5.                     Description = "test",
  6.                     Name = "SOTest",
  7.                     IsActive = true
  8.                 };
  9.  
  10.                 ctx.Customers.Add(newCustomer);
  11.                 ctx.SaveChanges();
  12.  
  13.                 var customerID = newCustomer.CustomerID;
  14.                 var customer = ctx.Customers
  15.                     .AsNoTracking()
  16.                     .Where(x => x.CustomerID == customerID)
  17.                     .Select(x => x)
  18.                     .FirstOrDefault();
  19.                 customer.Description = "Some nasty ass description";
  20.  
  21.                 ctx.ClearCache();
  22.                 ctx.Entry(customer).State = EntityState.Modified;
  23.                 ctx.SaveChanges();
  24.  
  25.                 var anotherCustomer = ctx.Customers
  26.                     .AsNoTracking()
  27.                     .Where(x => x.CustomerID == customerID)
  28.                     .Select(x => x)
  29.                     .FirstOrDefault();
  30.                 anotherCustomer.Name = "Trying once more";
  31.  
  32.                 //ctx.ClearCache();
  33.                 ctx.Entry(anotherCustomer).State = EntityState.Modified;
  34.                 ctx.SaveChanges();
  35.             }
  36.  
  37.     public class Customer
  38.     {
  39.         public int CustomerID { get; set; }
  40.         public string Name { get; set; }
  41.         public string Description { get; set; }
  42.         public Boolean IsActive { get; set; }
  43.     }
  44.  
  45.     public class EntityContext : DbContext
  46.     {
  47.         public EntityContext() : base("DefaultConnectionString")
  48.         {
  49.             InitContext();
  50.         }
  51.  
  52.         void InitContext()
  53.         {
  54.             /// Should be enabled by default, if optimization is needed then turn it off.
  55.             Configuration.AutoDetectChangesEnabled = true;
  56.             /// We do not use proxies for lazy loading nor change-tracking.
  57.             Configuration.ProxyCreationEnabled = false;
  58.             /// Should always be false!
  59.             Configuration.LazyLoadingEnabled = false;
  60.             ///We set this to true to avoid unnecessary check in SQL
  61.             ///(if sending null parameter to query, SQL will check if column is null and parameter is null and we do not use this kind of filtering)
  62.             Configuration.UseDatabaseNullSemantics = true;
  63.             //Default timeout for connection
  64.             Database.CommandTimeout = 60;
  65.         }
  66.         public void ClearCache()
  67.         {
  68.             Configuration.AutoDetectChangesEnabled = false;
  69.             var trackedItems = ChangeTracker.Entries().ToList();
  70.             foreach (var item in trackedItems)
  71.             {
  72.                 Entry(item.Entity).State = System.Data.Entity.EntityState.Detached;
  73.             }
  74.             Configuration.AutoDetectChangesEnabled = true;
  75.         }
  76.  
  77.         public DbSet<Customer> Customers { get; set; }
  78.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement