c# - Open Entity in another DBContext in Entity Framework -


i have entity loaded 1 dbcontext changes made it. if create dbcontext how load same entity database? needs generic across different entities, can't lookup id's same - entities have different key properties.

the reason behind question i'm wanting use entity loaded second context validate changed properties in first one, comparing previous values new ones. second context purely read during validation of first entity.


edit

i tried keep simple think bit more detail needed.

say have entity:

public partial class someentity : ivalidatableobject {     public int id { get; set; }      public int statusid { get; set; }      //... other properties      public virtual icollection<someotherentity> relatedentity { get; set; }      public ienumerable<validationresult> validate(validationcontext validationcontext){         // validation code     } } 

what i'm trying when entity validated statusid compared against statusid in database , validation error occur - example statusid can't changed 1 5.

as pawel mentioned can done using originalvalues need able validate statusid based on values in relatedentity. example statusid can't changed 1 2 if values exist in relatedentity.

so in order validation need duplicate of someentity object in unmodifed form database can compared against object i'm trying modify.

it's bit messy idea i've come is:

  • make someentity member of new, empty, interface imyvalidationinterface.

    public interface imyvalidationinterface { }  public partial class someentity : ivalidatableobject, imyvalidationinterface {     public int id { get; set; }     // .... } 
  • override validateentity method on mydbcontext object original values passed validation method on entity.

    public class mydbcontext : dbcontext {     protected override dbentityvalidationresult validateentity(dbentityentry entityentry, idictionary<object, object> items)     {         if (entityentry.entity imyvalidationinterface)         {             var _validationcontext = new mydbcontext();              /// todo: code here load duplicated version of              ///       entity database              var originalentity; // unchanged entity here              // unmodified entity passed item entities             // validate method              items.add("originalentity", originalentity);          }           return base.validateentity(entityentry, items);      } }  public partial class someentity : ivalidatableobject, imyvalidationinterface {     // .....      public ienumerable<validationresult> validate(validationcontext validationcontext){         // validation code         if (validationcontext.items.containskey("originalentity")){               var originalentity = (someentity)validationcontext.items["originalentity"];                // validation here , yield return validation errors         }     } } 

the bit i'm stuck on todo part in above snippet.

i'm going @ wrong way?

a quick-and-dirty way store original status before modifying own property or field isn't mapped database, ideally when loaded database.

interface imaterializable {     void onmaterialized(); }  class someentity : imaterializable, ivalidatableobject {     public int id { get; set; }      public int statusid { get; set; }      //... other properties      public virtual icollection<someotherentity> relatedentity { get; set; }      int originalstatusid;      public void onmaterialized()     {         originalstatusid = statusid;     }      public ienumerable<validationresult> validate(validationcontext validationcontext)     {         // compare current status original + related     } } 

and add handler theobjectmaterialized event (see example in this answer):

void onobjectmaterialized( object sender, objectmaterializedeventargs e ) {     var entity = e.entity imaterializable;      if ( entity != null )     {         entity.onmaterialized();     } } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

jquery - Keeping Kendo Datepicker in min/max range -