I have two classes in my Entity project, User and UserProfile, UserProfile inherit from User.
Some part of my code instantiate new User, than I want to add UserProfile to my sql database, which will be expanding my User class. But I don't know how to write it.
I was trying:
Users user = context.Users.FirstOrDefault(o => o.UserID == UserId);
if (user != null)
{
UserProfile userProfile= new UserProfile ();
userProfile.UserID = user.UserID;
context.Users.AddObject(userProfile);
context.SaveChanges();
return "ok";
}
but it's not working. Writing just pure sql it'd be no problem. But I don't know how to write it properly. Thanks for help.

Adil AnsariPosted Aug 28, 2012, 3:43 AM
These two entities would remain seperate, there shouldn't be any like 'Logically'
You may have a instance method into to the User class like
Users user = context.Users.FirstOrDefault(o => o.UserID == UserId);
UserProfile userProfile =user.createMyProfile();
So. method createMyProfile() will read the details from User class and will do the job and finally return the instance of the UserProfile
OR
You can have static method in the UserProfile class which will take the User as paramter to save it and finally return the instance of the UserProfile
UserProfile userProfile = UserProfile.create(Users user)
Hope i answer your question...
If anything not clear to you please don't hesitate, get back to me