asp.net - Cannot Update Entity Using EF 6 - ObjectStateManager Error -


i'm trying update entity using entity framework version 6.

i'm selecting entity database so...

 public t find<t>(object id) t : class     {         return this._dbcontext.set<t>().find(id);     } 

and updating entity so..

  public t update<t>(t entity) t : class     {         // primary key of entity         object id = this.getprimarykeyvalue(entity);          // original entry         t original = this._dbcontext.set<t>().find(id);           if (original != null)         {             // automatic stuff here (taken out example)              // overwrite original property values new values             this._dbcontext.entry(original).currentvalues.setvalues(entity);             this._dbcontext.entry(original).state = entitystate.modified;              // commit changes database             this.save();              // return entity new property values             return entity;         }          return default(t);     } 

the getprimarykeyvalue function so...

 private object getprimarykeyvalue<t>(t entity) t : class     {         var objectstateentry = ((iobjectcontextadapter)this._dbcontext).objectcontext             .objectstatemanager             .getobjectstateentry(entity);          return objectstateentry.entitykey.entitykeyvalues[0].value;     } 

just clarity. i'm selecting original entry out need perform concurrency logic (that ive taken out). i'm not posting data entity , need select manually out of db again perform checks.

i know getprimarykeyvalue function not ideal if there's more 1 primary key on entity. want work now.

when updating, entity framework coughs error below when trying execute getprimarykeyvalue function.

the objectstatemanager not contain objectstateentry reference object of type 'name_of_entity_it_cannot_find'

i've written many repositories before , i've never had issue, cannot seem find why not working (hence post).

any appreciated.

thanks guys!

steve

it seems having issues getting pk entity being passed in. instead of trying go through ef data either use key attribute or create own , use reflection collect key names are. allow retrieve multiple keys if needed. below example created inside of linqpad, should able set "program" mode , paste in , see work. hack code , use may. implemented ientity not required, , can change attribute really.

here results:

keys found:  customidentifier lookasecondkey 

here code:

// usage demo void main() {     // create object wherever     var car = new car(){ customidentifier= 1, lookasecondkey="secretkey", doors=4, make="nissan", model="altima" };      // pass object in     var keys = getprimarykeys<car>(car);      // have list of keys work them     console.writeline("keys found: ");       foreach(var k in keys)             console.writeline(k);    }  // want use method, add whatever custom logic or checking want, maybe put private ienumerable<string> getprimarykeys<t>(t entity) t : class, ientity {     // place store keys     var keys = new list<string>();      // loop through each propery on entity     foreach(var prop in typeof(t).getproperties())     {         // check custom attribute created, replace "entitykey" own         if(prop.customattributes.any(p => p.attributetype.equals(typeof(entitykey))))             keys.add(prop.name);     }      // check key , throw if not found (up you)     if(!keys.any())         throw new exception("no entitykey attribute found, please make sure entity includes attribute on @ least on property.");      // return keys     return keys; }  // example of custom attribute use [attributeusage(attributetargets.property)] public class entitykey : attribute {  }  // interface not needed restrict dal interface public interface ientity { }  // example of model public class car : ientity {     [entitykey] // add attribure property     public int customidentifier {get;set;}      [entitykey] // demonstrating multiple keys can have 1     public string lookasecondkey {get;set;}      public int doors {get;set;}     public string make {get;set;}     public string model {get;set;} } 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -