Notifications for tracked changes in EntityFrameworks DbContext

.net c# entity-framework entity-framework-6 winforms

Question

I'm using Entity Framework to create a data entry app that allows users to fill out forms and either Save or Cancel their submissions. The Save Button should only be active, though, when there is actual data that can be saved to the database, is my wish.

I'm familiar with DbContext. ChangeTracker. However, I was unable to locate a way to receive a notification everytime the context changes.

Of course I could do that manually, but that would be laborious and prone to mistakes.

Update The application is a WinForms one.

Question: How can I receive an alert when the DbContext "Is Dirty/Has Changes"?

2. Update

Maybe this may make my question clearer:

I don't want the following:

using(var ctx = new DbContext()) {
  var foo = new FooEntity();
  ctx.Add(foo);
  RaiseContextIsDirty();  //<-- don't want to do this, this should be automatic
  //.....
  ctx.SaveChanges();
  RaiseContextIsClean();  //<-- don't want to do this, this should be automatic
}

I'm searching for something along these lines:

using(var ctx = new DbContext()) {
  ctx.ChangeTracker.OnDirtyChanged += ContextDirtyChanged;
  var foo = new FooEntity();
  ctx.Add(foo);   //<- fires OnDirtyChanged
  //.....
  ctx.SaveChanges();   //<- fires OnDirtyChanged
}
1
0
12/11/2015 10:47:55 PM

Popular Answer

Therefore, you might override this implementation of theSaveChanges :

public class MyDbContext : DbContext
{
   public delegate void ChangeEventHandler(object sender, ChangeEventArgs e);
   public event ChangeEventHandler ChangeEvent;

   public override int SaveChanges()
   {
       var result = base.SaveChanges();

       var entitiesAdded = this.ChangeTracker.Entries.Where(x => x.State == EntityState.Added);
       var entitiesRemoved = this.ChangeTracker.Entries.Where(x => x.State == EntityState.Deleted);
       var entitiesModified = this.ChangeTracker.Entries.Where(x => x.State == EntityState.Modified);

       ChangeEvent(this, new ChangeEventArgs(entitiesAdded, entitiesRemoved, entitiesModified));

       return result;
   }

}

just make aChangeEventArgs by developing a class that adheres toEventArgs .

After that, you should be able to bind to the event as usual:

ctx.ChangeEvent += (s, e) => Console.WriteLine("Changes happened");

Edit

Since I don't have enough time to thoroughly test it, I simply took the most of this from MSDN. However, I don't think this solution is drastically incorrect.

Other Considerations

The DbSet<T> returns to aIDbSet<T> therefore there is nothing stopping you from implementing your own.IDbSet<T> furthermore, when you put your ownAdd event function wire in. This would proceed in the same manner as before.

Next, in yourMyDbContext Listen to all of these occurrences and manage them all at once or as they happen.

0
9/21/2015 10:26:08 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow