Reputation: 36573
I need to get all the ObjectStateEntries from my ObjectStateManager in the order that they will be saved (in order of dependency that is). Is there any easy way to do this?
Thanks.
Upvotes: 2
Views: 296
Reputation: 1140
Yes, there is a way to plug into the saving process if Entity Framework in order to perform some custom logic.
Here is an example how to extend a simple NorthwindEntities model and do some work during the adding/updating/deleting process:
public partial class NorthwindEntities
{
partial void OnContextCreated()
{
SavingChanges += OnSavingChanges;
}
private void OnSavingChanges(object sender, EventArgs e)
{
var context = sender as NorthwindEntities;
if (context != null)
{
context.DetectChanges();
var objectStateManager = context.ObjectStateManager;
foreach (var entry in objectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified | EntityState.Deleted))
{
switch (entry.State)
{
case EntityState.Added:
// Perform custom 'add' logic here
break;
case EntityState.Modified:
// Perform custom 'update' logic here
break;
case EntityState.Deleted:
// Perform custom 'delete' logic here
break;
}
}
}
}
}
Upvotes: 1