我有用户和人员表。一个用户有很多人。当我向现有用户添加新Person时,我是否必须检索用户对象并向其添加新Person或者是否足以将userid指定为person对象?
public void InsertUpdatePerson(int? personId, string firstName, string lastName,
string middleName, DateTime dob, string mobile, string email, int userid)
{
using (DBEntities context = new DBEntities())
{
Person pers;
if (personId.HasValue)
{
pers = context.People.Where(p => p.PersonID == personId).FirstOrDefault();
pers.FirstName = firstName;
pers.LastName = lastName;
pers.MiddleName = middleName;
pers.DOB = dob;
pers.Mobile = mobile;
pers.Email = email;
pers.UserID = userid;
}
}
}
答案 0 :(得分:1)
我首先使用EF 4.3.1代码,但事实并非如此。您只需设置外键而无需检索。