c# - CTP5 update cascade -
i need add or update pattern ctp5. assuming model:
public class user { public int userid { get; set; } public icollection<address> addresses { get; set; } } public class address { public int addressid { get; set; } public string location { get; set; } }
if add new user, coresponding address table populated. if user in db, dbupdateexception. in case want data in database updated new data. how can this?
data in database
table address
addressid location
1 johnplace
2 maryplace
3 jimmyplace
the user update has in addresses collection 1 item addressid:2, location=georgeplace. id=2, in db there record location=marryplace. want georgeplace overwrite marryplace.
i cannot have address not assigned user
i create user like:
var user=new user(); user.id=getuseridfromservice(); foreach(var address in getaddressesfromservice(user.id)){ user.addresses.add(address); } context.users.add(user); context.savechanges();//this may throw exception, because there user id.
if want update existing user not insert (do not call add()
) load user id first modify it's properties , call savechanges()
on context.
var db = new databasecontext(); var user = db.users.find(myuserid); user.name = "new name"; db.savechanges();
Comments
Post a Comment