首先,错误是“一个实体对象不能被多个IEntityChangeTracker实例引用”
我有两个实体 User 和 UsersInHospitals
这是我为用户创建新帐户的方法
var user = new User(); user.UserID = Guid.NewGuid(); user.Address = employmentAddress.Trim(); user.ZipCode = int.Parse(zipcode.Trim());
UsersInHospital hospitalAff;
hospitalAff = new UsersInHospital()
{
UserID = user.UserID,
HospitalID = Guid.Parse(hospitalAff1),
IsEmployed = Boolean.Parse(isEmployed1)
};
user.UsersInHospitals.Add(hospitalAff);
hospitalAff = new UsersInHospital()
{
UserID = user.UserID,
HospitalID = Guid.Parse(hospitalAff1),
IsEmployed = Boolean.Parse(isEmployed2)
};
user.UsersInHospitals.Add(hospitalAff);
并保存
_db.AddToUsers(user);
return _db.SaveChanges(); //THIS LINE GIVES ME ERROR
我正在使用实体框架 4。我怀疑我是否必须为每个医院实体单独创建变量