I have a rather large save routine that updates hundreds of items using the Entity Framework and then saves the whole lot in one go.
using (var dc = new EntityLayer.Entities())
{
dc.Entity1.Prop1 = 1;
dc.Entity1.Prop2 = 2;
dc.Entity2.Prop1 = 1;
dc.Entity2.Prop2 = 2; // Audit item
dc.Entity2.Prop3 = 3; // Audit item
dc.Entity3.Prop1 = 1;
...
dc.SaveChanges();
}
What I would love to be able to do is somehow either extend the Save for an individual Entity or pick up on this change and kick off another method.
The main purpose of this is to hook into our Audit system, which only audits some key items, (so for example only Entity2.Prop2 and Entity2.Prop3 in the simplified version above) but would also possibly kick off some other routines, creating tasks etc, but the crux is – can it be done?
Basically I’m aiming to remove the extra audit line we have to put in for specific types as below:
using (var dc = new EntityLayer.Entities())
{
dc.Entity1.Prop1 = 1;
dc.Entity1.Prop2 = 2;
dc.Entity2.Prop1 = 1;
dc.Entity2.Prop2 = 2;
dc.Audit.CreateObject() { Type = "Entity2", Change = "Prop2", From = oldVal, To = 2}
dc.Entity2.Prop3 = 3;
dc.Audit.CreateObject() { Type = "Entity2", Change = "Prop3", From = oldVal, To = 3}
...
dc.SaveChanges();
}
The only way is override
SaveChanges, find changed entities and call your routines for them.