none
Entity reference id value changed but reference context not updated ? RRS feed

  • Question

  • Hello All ,

                  When i change the Id Of the reference the Reference context not updated  , but when i call  

    ChangeTracker.DetectChanges()

                       then it get updated , is any option to do it automatically . Hear i add my code , i am using North wind database with EF 5.0 in vs 2012 .

      NorthwindEntities Dc;
    
            public MainWindow()
            {
                InitializeComponent();
    
                Dc = new NorthwindEntities();
                
                Dc.Configuration.AutoDetectChangesEnabled = true;
    
                var dd = Dc.Products.Include("Category").AsQueryable();
              
                ////change the CategoryID == 3  => CategoryID == 2
                dd.First().CategoryID = 2;
                
                // But dd.First().Category not changed 
                //// dd.First().Category.CategoryID == 3 ????
    
                /// When we call 
                Dc.ChangeTracker.DetectChanges();
                //It updated . why ??
    
            }

    With regards ,

    jophy .

    Tuesday, January 22, 2013 4:42 AM

Answers

  • Hi Jophy,

    Welcome to the MSDN forum.

    By default, the Entity Framework performs Detect Changes automatically when the following methods are called:

    • DbSet.Find
    • DbSet.Local
    • DbSet.Remove
    • DbSet.Add
    • DbSet.Attach
    • DbContext.SaveChanges
    • DbContext.GetValidationErrors
    • DbContext.Entry
    • DbChangeTracker.Entries

    Thus, after you call DetectChanges explicitly, the Category.CategoryID is refreshed and corrected. If you call SaveChanges() directly, it also will be updated. Please check this page: http://msdn.microsoft.com/en-us/data/jj556205.aspx

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Alexander Sun Wednesday, January 30, 2013 8:43 AM
    Wednesday, January 23, 2013 8:27 AM