I'm trying to work with the entity framework but I've run into a problem with my design. My UserProfile class has three fields, UserId, UserName, and jk_userHandle, the first two update and can get saved when i make a change to the object. However I can't even create a non-null version jk_userHandle. I'm not certain where I went wrong.
I have google/stackoverflowed this to hell, and asked people in my office, no one seems to 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 i found on stackoverflow, but
found.jk_userHandle = model;
or
found.jk_userHandle.biography = model.biography
don't work either. In all scenarios, when I step through i see my dbset's value change, but will not persist out of the http post. I've tried db.SaveChanges() (outside this function call) as 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 the userprofile class:
[Table("UserProfile")]
public class UserProfile
{
public jk_userProfile jk_userHandle; //is the handle for the rest of the account info...
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public UserProfile()
{
jk_userHandle = new jk_userProfile();
}
}
and just in case, here's the 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 about yourself, favorite movies, shows, activities, what you like to do, etc...")]
public string biography { get; set; }
public jk_userProfile()
{
}
}