oorosco
oorosco

Reputation: 246

Entity Framework Is not saving child object of context

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()
        {

        }

    }

Upvotes: 0

Views: 118

Answers (1)

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93464

Well, you've gone wrong in a number of ways.

First, while not technically wrong, the naming is odd. You would be better off to use consistent naming conventions. Just call it UserProfile, or UserProfileJK or something if you really need the JK in there.

Second, you have created jk_userProfile as a field, rather than a property. Entity framework doesn't work with fields. It works with properties only, so you need to make it one.

Third, don't add methods to your DbContext like that, create a different data layer class to perform you business logic, and have it use your DbContext.

Upvotes: 1

Related Questions