c# - How can you set an entire object using LINQ to SQL? -
so if want update row in db using values in received object same pk, how that. reason needed because objects quite extensive , create place have change field names if update database. question how 1 go assigning object object retrieved database using linq? show i'm talking clarify.
//foo object received here, called recfoo using (var newcontext = new foodatacontext()) { var obj = newcontext.t_foo.single(x=>x.id == recfoo.id); //set obj = recfoo (directly) newcontext.submitchanges(); }
i know can set each individual attribute (obj.name = recfoo.name...), there way can take received object using pk id , assign values inside of recfoo?
edit: solution
using (var newcontext = new foodatacontext()) { //var obj = newcontext.t_foo.single(x=>x.id == recfoo.id); //set obj = recfoo (directly) newcontext.t_foo.attach(recfoo); newcontext.refresh(system.data.linq.refreshmode.keepcurrentvalues, recfoo); newcontext.submitchanges(); }
have tried newcontext.yourentity.attach(youralreadypopulatedobject)
in tell linqtosql record exists in database , updating it.
Comments
Post a Comment