c# - EF Updating entity related table is inserting instead of updating -


i have object trying update using entity framework 5.

once retrieve existing object , go update fields, correctly updates base object "coach", fails update address object , instead inserts again instead of updating new primary key though has been passed existing primary key use again.

any appreciated.

below dumbed down version of code:

using (altairentities context = new altairentities())                     {                         dtlcoach coach = context.dtlcoaches.firstordefault(x => x.coachid == coachid);                         coach.name = "bob";                         coach.description = "sample";                         coach.dtlcoachaddresses.add(prepareaddress(coach.dtlcoachaddresses.first().coachaddressid));                         context.database.connection.open();                         context.entry(coach).state = entitystate.modified;                         context.savechanges();                     }  public static dtlcoachaddress prepareaddress(int existingid)         {             dtlcoachaddress newaddress = new dtlcoachaddress();             try             {                 newaddress.coachaddressid = existingid;                 newaddress.addressline1 = "line 1";                 newaddress.addressline2 = "line 2";                  return newaddress;             }             catch (exception ex)             {                 throw ex;             }         } 

update: have found if feed existing dtlcoachaddress entity inside dtlcoach entity prepareaddress function parameter instead of declaring object new, updates correctly. difference between dtlcoachaddress object entity , dtlcoachaddress object defined new, if pass same parameters? 2 define if object gets inserted or updated?

i not sure how have arranged pks , fks in entities. solution has few assumptions.

updating again match ops methods.

using (altairentities context = new altairentities()) {     dtlcoach coach = context.dtlcoaches.firstordefault(x => x.coachid == coachid);     coach.name = "bob";     coach.description = "sample";     //coach.dtlcoachaddresses.add(prepareaddress(coach.dtlcoachaddresses.first().coachaddressid));     //context.database.connection.open();     //context.entry(coach).state = entitystate.modified;      var address = context.dtlcoachaddresses.firstordefault(a => a.coachaddressid == coachid);            if(address != null)    {      address.addressline1 = "line 1";      address.addressline2 = "line 2";                                       }          context.savechanges(); }  /*this function not required public static dtlcoachaddress prepareaddress(int existingid) { using (altairentities context = new altairentities()) { var address = context.dtlcoachaddresses.firstordefault(a => a.coachaddressid == coachid);         if(address != null)  {      address.addressline1 = "line 1";      address.addressline2 = "line 2";      context.savechanges();//update existing address.                               } } catch (exception ex) { throw ex; } }*/ 

Comments

Popular posts from this blog

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -