locked
EF 4.1 DbContext.Configuration.AutoDetectChangesEnabled Issue RRS feed

  • Question

  • I am following the msdn tutorial: Using DbContext in EF 4.1 Part 12: Automatically Detecting Changes located at http://blogs.msdn.com/b/adonet/archive/2011/02/06/using-dbcontext-in-ef-feature-ctp5-part-12-automatically-detecting-changes.aspx

    I can see how the application would speed up by calling:
     context.Configuration.AutoDetectChangesEnabled = false; 

    But this call also prevents the Unicorns from being saved to the database.
    In this circumstance it appears that using DbContext.ChangeTracker.DetectChanges is a necessity, not an option.

    I tried adding a unicorn while AutoDetectChanges was false. Then set Configuration.AutoDetectChanges to true.  Then called SaveChanges . ALL OF THE UNICORN RECORDS WERE AUTOMATICALLY DELETED FROM THE DATABASE.

    Saturday, September 24, 2011 7:12 PM

All replies

  • Mark,

    Are you sure the unicorn records were deleted, or perhaps you're looking at the wrong database?  Or perhaps your database from your solution overwrote the sdf database in the BIN folder.

     Make sure you're looking at the sdf in the BIN directory.  Also, make sure your sdf database has properties set to "copy if newer" and not "copy always" otherwise your empty sdf database in your solution will always overwrite the one in the BIN directory  (which is the one that gets updated when your program is running).  It will therefore always appear to be empty when you run your application.

     How VS handles sdf databases confuses a lot of people.  


    Tom Overton
    Saturday, September 24, 2011 7:30 PM
  • Thanks for the response Tom.

    I am certain that all the records from the Unicorn table in the Unicorns database were removed.  I used SQL Server Dev Edition for all 12 parts of this ms tutorial. I ran into 3 bugs in this series of 15 posts about DbContext, but this one is the scariest.

                   Console.WriteLine("All Unicorns");
                   foreach (var u in context.Unicorns.ToList())
                   {
                       Console.WriteLine(u.Name);
                   }
    
                   try
                   {
                       context.Configuration.AutoDetectChangesEnabled = false;
                       context.Unicorns.Add(new Unicorn { Name = "Sam" });
    
    
                       Console.WriteLine("\nAll Unicorns with Sam Added and No Tracking");
                       foreach (var u in context.Unicorns.ToList())
                       {
                           Console.WriteLine(u.Name);
                       }
    
                   }
                   finally
                   {
                       context.Configuration.AutoDetectChangesEnabled = true;
                   }
    
                   // Comment out SaveChanges and and all Unicorns will be in the database
                   // Uncomment SaveChanges and all Unicorns will be deleted from the database
                   context.SaveChanges();
    
                   Console.WriteLine("\nAll Unicorns with Sam Added and NoTracking Reenabled and Save Changes");
                   foreach (var u in context.Unicorns.ToList())
                   {
                       Console.WriteLine(u.Name);
                   }
               }
    
                Console.WriteLine("Press any key to exit");
                Console.ReadKey();
    


    I could send you the zipped project for verification. I am sure this problem exists.  I have checked it multiple times. Now for this tutorial,  I have created a 3 layer application Client, Domain, and Entities. And this architecture could be part of the problem.  But most of the rest of the tutorial worked just fine.

    However, I did run into three other issues:.   

    1. If the entities are in a separate namespace from the dbcontext, a call to entry.GetDatabaseValues() throws an error.  The DbContext is unable to find the entity. Many developers place their Domain entities in one project/namespace and their data access in another. 
    2. The entry object returned from DbUpdateConcurrencyException throws an error when this code is called  var currentValues = entry.CurrentValues; in a try/catch block. The error complains that all of the entry.CurrentValues have been deleted.
    3. if an entity's CurrentValues are changed, but then changed back to their original values, IsModified still returns true.

    I love a lot of what the EF team is doing, but these issues have me concerned and certainly add a big restriction to the great Concurrency Handling that they created.



    Sunday, September 25, 2011 5:47 AM