c# - Check if EF entity already exist in current context -
i aware of fact there several questions already, none of them solves problem:
application background
i have web application uses entity framework 6.1 code first. i'm using repository pattern within dal, means, have repo's queries context , returns results services, uses automapper map entity view model, , returns vm. services called web api methods.
based of above architecture, clear i'm using detached entities.
the issue i'm having updating (put) existing entities. flow looks this:
angular issue http put webapi method passing vm parameter. webapi calls service method, passing vm parameter. services method converts vm ef entity using automapper service method calls respective repository, passing new instance of detached ef entity parameter.
example of service method:
public async task<list<dependantviewmodel>> savedependants(int customerid, list<dependantviewmodel> dependantviewmodels) { var dependantentities = mapper.map<list<dependantviewmodel>, list<dependant>>(dependantviewmodels); bool result = false; foreach (var dependantentity in dependantentities) { result = await _dependantrepository.insertorupdate(dependantentity); if (result != true) { // log errror } } return mapper.map<list<dependant>, list<dependantviewmodel>>(dependantentities); } baserepo:
public virtual async task<bool> insertorupdate(te entity) { if (entity.id == 0 || entity.id == modelstate.new) { // insert new item _context.entry<te>(entity).state = entitystate.added; } else { // update existing item _context.entry<te>(entity).state = entitystate.modified; _context.entry<te>(entity).property(o => o.createduserid).ismodified = false; _context.entry<te>(entity).property(o => o.createddate).ismodified = false; } return await _context.savechangesasync() > 0; } exception happens when trying set:
_context.entry(entity).state = entitystate.modified;
attaching entity of type 'business.data.entities.customer.dependant' failed because entity of same type has same primary key value. can happen when using 'attach' method or setting state of entity 'unchanged' or 'modified' if entities in graph have conflicting key values. may because entities new , have not yet received database-generated key values. in case use 'add' method or 'added' entity state track graph , set state of non-new entities 'unchanged' or 'modified' appropriate.
as far understand, happens because detached entity needs attached first before setting entity state. issue is, can't attach entity, because in context.dependant.local collection. reason, entity framework tracking entity.
is there way first check if entity exists in context, , if doesn't, attach, otherwise, retrieve attached entity, , apply changes modified entity attached entity, if makes sense.
appreciate feedback
this worked me:
public virtual async task<bool> insertorupdate(te entity) { if (entity.id == 0 || entity.id == modelstate.new) { // insert new item _context.entry<te>(entity).state = entitystate.added; } else { var attachedentity = _context.changetracker.entries<te>().firstordefault(e => e.entity.id == entity.id); if (attachedentity != null) { // entity want update attached, need detach , attach updated entity instead _context.entry<te>(attachedentity.entity).state = entitystate.detached; } _context.entry<te>(entity).state = entitystate.modified; // attach entity, , set state modified. _context.entry<te>(entity).property(o => o.createduserid).ismodified = false; _context.entry<te>(entity).property(o => o.createddate).ismodified = false; } return await _context.savechangesasync() > 0; }
Comments
Post a Comment