I want to be notified when data changes within a particular table, represented by an entity model. Is the model aware of underlying data changes? If it does actually fire an event, how can I subscribe to it?
-
1as far as I know.. No but any changes made to the edmx is done by your own code. so you could implement such a event. – Theun Arbeider Apr 26 '11 at 14:29
Your question is actually very unclear.
I want to be notified when data changes within a particular table, represented by an entity model.
What does it mean?
Do you want to be notified about changes in database not done by your application? Then the answer is: No EF doesn't provide any such notification system. You must build your own.
Do you want to be notified about changes you did to your entities? The the answer is INotifyPropertyChanged interface and ObservableCollection for navigation properties. use these constructs in your entities to be able to fire events when entity changes.
Do you want to know what changes will be executed in the database? Override SaveChanges
or handle SavingChanges
and use ObjectStateManager
to get list of changed entities. Here is some example how to get list of added entities of given type.
The NuGet package EntityFramework.Triggers nicely wraps up functionality of subscribing to an Entity for inserts, updates, and deletions.
Just wrap your context with the DbContextWithTriggers;
public class MyContext : DbContextWithTriggers {
public DbSet<Person> People { get; set; }
}
Then subscribe to trigger events
var mycontext = new MyContext() { TriggersEnabled = true };
Triggers<Person>.Inserting += entry =>
{
Console.WriteLine($"Person: {entry.Entity}");
};
No, there is no events fired on Change
, only when you do SaveChanges
you could catch what you need ...
for that, try looking at this question / answers
If you purpose is to save data only in case there are changes then refer to the following link:
http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.savingchanges.aspx