Entity Framework and One to Many Relationships not saving right -


i worked entity framework several years ago, , may me being bit rusty. have detached entities, in turn have multiple child entities. in case it's person entity , each person has multiple addresses.

myperson.firstname="update first name"; //assuming have address in first entry  //with appropriate primary keys , foreign key ids, do: myperson.addresses.first().line1="update line 1"; myperson.addresses.add(new address(){line1="weee",line2="aaaa" postal="12345", type="work"}); mydb.person.attach(myperson); mydb.entry(myperson).state=entitystate.modified; mydb.savechanges(); 

when scenario this, expect first name. updates first name, , expected blanks out other fields (lastname, birthday etc etc) in above code. doesn't create new address person, nor update existing address.

i don't recall requiring work database context know how update related entities associated person above. in google searches seem hearing story need considerable amount of work loading collections db , going town way. hope isn't case.

the key part of code pasted these lines...

mydb.person.attach(myperson); mydb.entry(myperson).state=entitystate.modified; 

the first line attach entire object graph context , set state of each entity in graph unchanged. second line of code set state of root entity modified (the others, such address entities, remain in unchanged state). when changes saved, single entity updated.

of course, assumes working disconnected entities (using 1 context fetch object graph , perform update). assume you attaching entity.

/* rant on */

disconnected entities sadly isn't handled in ef space. find plenty of samples in books , blogs attempt address disconnected entities these strategies fall apart try implement more advanced scenarios (such object graphs including collections of related items).

/* rant off */


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -