Adding child objects fails

Apr 20, 2014 at 12:52 PM
Starting to explore the framework and have a question you might be able to direct me to the right direction. When you have foreign keys specified in your DAL do you have to set the state when adding them?
public partial class User : Entity
    {
        public int Id { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
        public DateTime DateCreated { get; set; }
        public int UserProfileId { get; set; }
        public virtual UserProfile UserProfile { get; set; }
    }
public partial class UserProfile : Entity
    {
        public UserProfile()
        {
            this.Users = new List<User>();
        }

        public int Id { get; set; }
        public string Title { get; set; }
        public string Firstname { get; set; }
        public string Lastname { get; set; }
        public DateTime? Dob { get; set; }
        public string Email { get; set; }
        public DateTime? DateCompleted { get; set; }
        public virtual ICollection<User> Users { get; set; }
    }
With this I can't simply insert a user without having to set the user profiles state to 'Added'. I'm new to Ef and wondering why we shouldn't use the built in change tracking to eliminate this step of manually setting states in child objects?
Coordinator
Apr 22, 2014 at 9:55 PM
This is simply because we've abstracted away from EF and that abstraction is why we have to set the IObjectState so that we can sync with EF or another provider we implement in the future e.g. nHibernate.
Marked as answer by lelong37 on 4/22/2014 at 1:55 PM