Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework 5 - DbContext Has Changes?

I'm trying to find a way to determine of any changes have been made to a database context (DbContext). Note: I'm using Visual Studio 2012 with Entity Framework 5 on a Windows 7, 64-bit box.

Back when I used to use ObjectContext instead of DbContext, I could do something like:

public partial class MyObjectContext {     public Boolean HasUnsavedChanges()     {         return (this.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified | EntityState.Deleted).Any());     } } 

Now that I'm using DbContext, I tried to do this:

public partial class MyDbContext {     public ObjectContext ObjectContext()     {         return (this as IObjectContextAdapter).ObjectContext;     }      public Boolean HasUnsavedChanges()     {         return (this.ObjectContext().ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified | EntityState.Deleted).Any());     } } 

The problem that I'm having is that the method "HasUnsavedChanges()" always return "false" even when I know for a fact that the context has been changed. Does anyone have any ideas as to what I'm doing wrong?

like image 567
HydroPowerDeveloper Avatar asked Aug 17 '12 20:08

HydroPowerDeveloper


People also ask

What is the significance of has changes method in Entity Framework?

Functionally, calling this method is equivalent to checking if there are any entities or relationships in the Added, Updated, or Deleted state. Note that this method calls DetectChanges() unless AutoDetectChangesEnabled has been set to false.

How does EF core detect changes?

EF Core change tracking works best when the same DbContext instance is used to both query for entities and update them by calling SaveChanges. This is because EF Core automatically tracks the state of queried entities and then detects any changes made to these entities when SaveChanges is called.

How does DbContext change state of entity?

This can be achieved in several ways: setting the EntityState for the entity explicitly; using the DbContext. Update method (which is new in EF Core); using the DbContext. Attach method and then "walking the object graph" to set the state of individual properties within the graph explicitly.

How do I turn off change tracking in Entity Framework?

In Entity Framework, change tracking is enabled by default. You can also disable change tracking by setting the AutoDetectChangesEnabled property of DbContext to false. If this property is set to true then the Entity Framework maintains the state of entities.


2 Answers

For EF 5 use DbContext's ChangeTracker:

 public bool HasUnsavedChanges()  {     return this.ChangeTracker.Entries().Any(e => e.State == EntityState.Added                                               || e.State == EntityState.Modified                                               || e.State == EntityState.Deleted);  } 

For EF 6 use the ChangeTracker.HasChanges() method which will also detect changes in many to many relationships:

 public bool HasUnsavedChanges()  {     return this.ChangeTracker.HasChanges();  } 
like image 177
Mark Oreta Avatar answered Oct 02 '22 05:10

Mark Oreta


I know as you are using Entity Framework 5, this answer will not help you, but it may help others.

Starting with Entity Framework 6 and in Entity Framework Core you can check for changes simply by following line of code:

context.ChangeTracker.HasChanges() 
like image 37
Emdadul Sawon Avatar answered Oct 02 '22 07:10

Emdadul Sawon