Thursday 15 April 2010

c# - Entity framework AddOrUpdateOrDontBother with nesting -


I am using the first EF code with the following model:

  public class Root {Public Child AA (Receive; Set;} Public Child Bb {Received; Set;} Public ChildC C {get; Set; Suppose you have a Controller  
  Public Class Record Controller {... Public ZeroAccess (Root Root) {...} ...}   

And your root controller got a model from the client Wherein The changes include: Properties A is not completely new yet has been added to the database and it needs to be created, property b is already present in the database And it needs to be updated, the property C has not changed.

Action Save is not information about what the property changes are, It is necessary to properly update records and unavailable or existing The sub-model needs to be updated, it is also possible that some of the hair classes can have their own nested changes, so I need a method that allows someone to model Will revise the way, compare the new model to the existing one and apply appropriate changes. So how do I do this?

I have ended up with the insertion of each of my models and Voododal classes EntityState < / Code> with property, so when I change some properties, then EntityState has been changed to state when I create one I set the property I will do added , initially the model is started with the unchanged state, basically it is the following : ()] [known type (typef (country))] Public square City {public city (city) • airports = new list & amp; Airport & gt; (); Last update = date time.Now; } [Key] [Database Generated (Database Generated Oppression Identity)] Public ITO 32 (Receive; Set;) Public Int32? Country ID {Receive; Set;} [String Length (50)] Name of Public String {get; Set;} [Category (-12, 13)] Public Int32? Time Zone {Receive; Set;} Public Boolean? Summer Time {Receive; Set;} Public Date Time? Last Update {Receive; Set;} [ Foreign key ("country id")] public A country of country receives {set;} [NotMapped] Public EntityState? EntityState {get; set;} //

Then I do the following on the server

  [http post, httpAGGT] public HTTPPPS messages service record (recordview model record) {var model = mapper.map  record> ( Record); if (! ModelState.IsValid) {return request.Creative irresponsions (HTTPTus code.badwinist, modelstate);} Db.Attach (MO Dl; try {db.SaveChanges ();} hold (DbUpdateConcurrencyException pre) {return request.CreativeReuters.com (HTTTus code. Notfound, East); } Return request. CreteSpons (HTTitas.CO.OK); }   

The implementation for the editable method is:

  Attach public zeroes (city unit) {if (entity! = Null) {attach (entity .ountry); AttachAndMarkAs (Entity, entity.EntityState? EntityState.Added, Example = & Example; ID;); }} Attach Public Zero (country unit) {if (unit! = Null) {Attach and Marka (unit, unit. Entitestate? EntityState.Added, Example = & Example; ID;); Attach   

and the following implementation is in the market:

  Public Zero Attachments and Markka & lt; T & gt; (T Entity, Antitastat State, Function and LT; T, Object & gt; ID) where T: class {var entry = entry (unit); If (entry.State == EntityState.Detached) {var set = set & lt; T & gt; (); T attachedenti = set.find (id (unit)); If (enclosed entity! = Null) {var Attached Entry = Entry; AttachedEntry.CurrentValues.SetValues ​​(unit); } And {entry.State = state; }}}    

No comments:

Post a Comment