c# - Entity Framework Is not saving child object of context -
i'm trying work entity framework i've run problem design. userprofile class has 3 fields, userid, username, , jk_userhandle, first 2 update , can saved when create alter object. can't create non-null version jk_userhandle. i'm not went wrong.
i have google/stackoverflowed hell, , asked people in office, no 1 seems know -_-
public class userscontext : dbcontext { public userscontext() : base("defaultconnection") { } public dbset<userprofile> userprofiles { get; set; } public void changedataitem_edit(userprofile choice, jk_userprofile model) { var found = userprofiles.find(choice.userid); //finds prof if (found == null) throw new keynotfoundexception(); userprofile tempprofile = choice; tempprofile.jk_userhandle = model; /* stuff found on stackoverflow, found.jk_userhandle = model; or found.jk_userhandle.biography = model.biography don't work either. in scenarios, when step through see dbset's value change, not persist out of http post. i've tried db.savechanges() (outside function call) well. stackoverflow link: http://stackoverflow.com/questions/7850774/updating-a-reference-to-a-child-object-in-entity-framework-4-1-codefirst */ this.entry(found).currentvalues.setvalues(tempprofile); found.jk_userhandle = tempprofile.jk_userhandle; this.savechanges(); } }
here's userprofile class:
[table("userprofile")] public class userprofile { public jk_userprofile jk_userhandle; //is handle rest of business relationship info... [key] [databasegeneratedattribute(databasegeneratedoption.identity)] public int userid { get; set; } public string username { get; set; } public userprofile() { jk_userhandle = new jk_userprofile(); } }
and in case, here's jk_userhandle
public class jk_userprofile { public string username { get; set; } [datatype(datatype.date)] public string lastlogin { get; set; } [datatype(datatype.date)] public string creationdate { get; set; } [datatype(datatype.multilinetext)] [display(name = "just connekt biography", description = "write yourself, favorite movies, shows, activities, do, etc...")] public string biography { get; set; } public jk_userprofile() { } }
well, you've gone wrong in number of ways.
first, while not technically wrong, naming odd. improve off utilize consistent naming conventions. phone call userprofile, or userprofilejk or if need jk in there.
second, have created jk_userprofile field, rather property. entity framework doesn't work fields. works properties only, need create one.
third, don't add together methods dbcontext that, create different info layer class perform business logic, , have utilize dbcontext.
c# .net entity-framework
No comments:
Post a Comment