.net - How to add C# automatic calculations for a POCO entity in EF? -


i using db first ef 6.1.3 .net 4.5.1, t4 generated entities poco classes generated auto properties. calculations (a calculated int field actually) before saving db.

i update trigger of course prefer c# algorithm. i've looked override in dbcontext , found nothing. closest bet validateentity(...) smells me use different name implies. (not talking seems not trivial how access entity instance)

how , accomplish auto calculations before update or better: upon property set?

you following, i.e. override savechanges.

public partial class myentities : objectcontext {     /// <summary>     /// persists updates data source specified system.data.objects.saveoptions.     /// </summary>     /// <param name="saveoptions">a system.data.objects.saveoptions value determines behavior of operation.</param>     /// <returns>the number of objects in system.data.entitystate.added, system.data.entitystate.modified, or      /// system.data.entitystate.deleted state when system.data.objects.objectcontext.savechanges() called.</returns>     public override int savechanges(saveoptions saveoptions)     {         list<itinerary> itinerarylocations = itineraryaddeddeletedmonitor.getitinerarylocations(this);          alertmanager.objectcontextsavingchanges(this, null);         int changes = base.savechanges(saveoptions);          if (itineraryaddeddeletedmonitor.processitinerarylocations(this, itinerarylocations))         {             // new rows have been added             base.savechanges(saveoptions);         }          return changes;     } } 

update:

to access entity instance, objectstateentries in example below gets modified, added , deleted entities (alter required):

ienumerable<objectstateentry> objectstateentries = objectcontext.objectstatemanager.getobjectstateentries(entitystate.modified | entitystate.added | entitystate.deleted);  foreach (objectstateentry entry in objectstateentries) {     if (!entry.isrelationship && entry.entity.gettype() == typeof(customertransfer) && entry.state == entitystate.deleted)     {         // magic if entity type customertransfer has been deleted.     } } 

update:

... , variable objectcontext is:

var objectcontext = ((iobjectcontextadapter)this).objectcontext; 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -