c# - Can't update a foreign key on an attached graph -
i have attached graph object , can't figure out how change foreign key on record. have load record saved in db. retrieve said record id tracking on.
public partial class load { [key] public virtual int id {get;set;} [required] public virtual int accountid { get; set; } public virtual account account { get; set; } [required] public virtual int customerid { get; set; } [foreignkey("customerid")] public virtual customer customer { get; set; } }
i retrieve customer record want associate load record
public partial class customer { [key] public virtual int id {get;set;} [required] [maxlength(100)] public virtual string name { get; set; } }
no matter do, sort of exception thrown.
var customer = context.customers.where(x => x.id == customerid).singleordefault(); load.customer = customer; load.customerid = customer.id; load.modifiedbyapplicationuserid = userid; load.modifieddatetime = datetime.now; context.entry(load).state = entitystate.modified; context.savechanges();
what proper way go changing foreign key relationship on attached graph object?
edit. have added exception.
[invalidoperationexception: attaching entity of type 'triton.core.models.identity.applicationuser' 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.] system.data.entity.core.objects.objectcontext.attachsingleobject(ientitywrapper wrappedentity, entityset entityset) +690 system.data.entity.core.objects.dataclasses.relatedend.addentitytoobjectstatemanager(ientitywrapper wrappedentity, boolean doattach) +130 system.data.entity.core.objects.dataclasses.entityreference.addentitytoobjectstatemanager(ientitywrapper wrappedentity, boolean doattach) +65 system.data.entity.core.objects.dataclasses.relatedend.addgraphtoobjectstatemanager(ientitywrapper wrappedentity, boolean relationshipalreadyexists, boolean addrelationshipasunchanged, boolean doattach) +67 system.data.entity.core.objects.dataclasses.relatedend.includeentity(ientitywrapper wrappedentity, boolean addrelationshipasunchanged, boolean doattach) +340 system.data.entity.core.objects.dataclasses.entityreference
1.include(boolean addrelationshipasunchanged, boolean doattach) +209 system.data.entity.core.objects.dataclasses.relationshipmanager.addrelatedentitiestoobjectstatemanager(boolean doattach) +164 system.data.entity.core.objects.objectcontext.attachto(string entitysetname, object entity) +510 system.data.entity.internal.linq.<>c__displayclassa.<attach>b__9() +97 system.data.entity.internal.linq.internalset
1.actonset(action action, entitystate newstate, object entity, string methodname) +354 system.data.entity.internal.linq.internalset1.attach(object entity) +200 system.data.entity.internal.internalentityentry.set_state(entitystate value) +357 system.data.entity.infrastructure.dbentityentry
1.set_state(entitystate value) +42 triton.service.loadservice.updatecustomer(load load, int32 customerid, int32 userid) in d:\bitbucket\trunk\triton\triton.service\loadservice.cs:73 triton.web.controllers.loadscontroller.customer(loadviewmodel model) in d:\bitbucket\trunk\triton\triton.web\controllers\loadscontroller.cs:138 lambda_method(closure , controllerbase , object[] ) +180 system.web.mvc.actionmethoddispatcher.execute(controllerbase controller, object[] parameters) +59 system.web.mvc.reflectedactiondescriptor.execute(controllercontext controllercontext, idictionary2 parameters) +434 system.web.mvc.controlleractioninvoker.invokeactionmethod(controllercontext controllercontext, actiondescriptor actiondescriptor, idictionary
2 parameters) +60 system.web.mvc.async.actioninvocation.invokesynchronousactionmethod() +76 system.web.mvc.async.asynccontrolleractioninvoker.b__39(iasyncresult asyncresult, actioninvocation innerinvokestate) +36 system.web.mvc.async.wrappedasyncresult2.callenddelegate(iasyncresult asyncresult) +73 system.web.mvc.async.wrappedasyncresultbase
1.end() +136 system.web.mvc.async.asyncresultwrapper.end(iasyncresult asyncresult, object tag) +102 system.web.mvc.async.asynccontrolleractioninvoker.endinvokeactionmethod(iasyncresult asyncresult) +49 castle.proxies.asynccontrolleractioninvokerproxy.endinvokeactionmethod_callback(iasyncresult asyncresult) +42 castle.proxies.invocations.asynccontrolleractioninvoker_endinvokeactionmethod.invokemethodontarget() +103 castle.dynamicproxy.abstractinvocation.proceed() +117 glimpse.core.extensibility.castleinvocationtoalternatemethodcontextadapter.proceed() +48 glimpse.mvc.alternatetype.endinvokeactionmethod.newimplementation(ialternatemethodcontext context) +152 glimpse.core.extensibility.alternatetypetocastleinterceptoradapter.intercept(iinvocation invocation) +183 castle.dynamicproxy.abstractinvocation.proceed() +483 castle.proxies.asynccontrolleractioninvokerproxy.endinvokeactionmethod(iasyncresult asyncresult) +203 system.web.mvc.async.asyncinvocationwithfilters.b__3d() +117 system.web.mvc.async.<>c__displayclass46.b__3f() +323 system.web.mvc.async.<>c__displayclass46.b__3f() +323 system.web.mvc.async.<>c__displayclass33.b__32(iasyncresult asyncresult) +44 system.web.mvc.async.wrappedasyncresult1.callenddelegate(iasyncresult asyncresult) +47 system.web.mvc.async.wrappedasyncresultbase
1.end() +136 system.web.mvc.async.asyncresultwrapper.end(iasyncresult asyncresult, object tag) +102 system.web.mvc.async.asynccontrolleractioninvoker.endinvokeactionmethodwithfilters(iasyncresult asyncresult) +50 system.web.mvc.async.<>c__displayclass2b.b__1c() +72 system.web.mvc.async.<>c__displayclass21.b__1e(iasyncresult asyncresult) +184 system.web.mvc.async.wrappedasyncresult1.callenddelegate(iasyncresult asyncresult) +42 system.web.mvc.async.wrappedasyncresultbase
1.end() +133 system.web.mvc.async.asyncresultwrapper.end(iasyncresult asyncresult, object tag) +56 system.web.mvc.async.asynccontrolleractioninvoker.endinvokeaction(iasyncresult asyncresult) +40 system.web.mvc.controller.b__1d(iasyncresult asyncresult, executecorestate innerstate) +34 system.web.mvc.async.wrappedasyncvoid1.callenddelegate(iasyncresult asyncresult) +70 system.web.mvc.async.wrappedasyncresultbase
1.end() +139 system.web.mvc.async.asyncresultwrapper.end(iasyncresult asyncresult, object tag) +59 system.web.mvc.async.asyncresultwrapper.end(iasyncresult asyncresult, object tag) +40 system.web.mvc.controller.endexecutecore(iasyncresult asyncresult) +44 system.web.mvc.controller.b__15(iasyncresult asyncresult, controller controller) +39 system.web.mvc.async.wrappedasyncvoid1.callenddelegate(iasyncresult asyncresult) +62 system.web.mvc.async.wrappedasyncresultbase
1.end() +139 system.web.mvc.async.asyncresultwrapper.end(iasyncresult asyncresult, object tag) +59 system.web.mvc.async.asyncresultwrapper.end(iasyncresult asyncresult, object tag) +40 system.web.mvc.controller.endexecute(iasyncresult asyncresult) +39 system.web.mvc.controller.system.web.mvc.async.iasynccontroller.endexecute(iasyncresult asyncresult) +39 system.web.mvc.mvchandler.b__5(iasyncresult asyncresult, processrequeststate innerstate) +39 system.web.mvc.async.wrappedasyncvoid1.callenddelegate(iasyncresult asyncresult) +70 system.web.mvc.async.wrappedasyncresultbase
1.end() +139 system.web.mvc.async.asyncresultwrapper.end(iasyncresult asyncresult, object tag) +59 system.web.mvc.async.asyncresultwrapper.end(iasyncresult asyncresult, object tag) +40 system.web.mvc.mvchandler.endprocessrequest(iasyncresult asyncresult) +40 system.web.mvc.mvchandler.system.web.ihttpasynchandler.endprocessrequest(iasyncresult result) +38 system.web.callhandlerexecutionstep.system.web.httpapplication.iexecutionstep.execute() +9651688 system.web.httpapplication.executestep(iexecutionstep step, boolean& completedsynchronously) +155
it turned out had object in sub class being referenced ef trying add graph. once nullified object, worked properly.
Comments
Post a Comment