我有EF的问题,当我试图通过Id获取一些模型数据时,就像在这些示例中一样:
Company company = context.Companies.Find(id);
EF让我接受这个例外: 该物业' Id'是对象的关键信息的一部分,不能修改。 它早期工作的问题,但现在没有任何理由很多(不是全部)Find()函数显示它。
控制器:
var modelEmpl = (RegisterEmployeeView)model;
user.Employee = modelEmpl.Employee;
user.Avatar = Galary.Avatar(file, "Avatar", context);
result = UserManager.Create(user, modelEmpl.Password);
var prof = (Profesion)context.Profesions.Find(modelEmpl.Profesion.Id);
user.Employee.Profesion = prof;
CreatingEmployee(user);
...
private void CreatingEmployee(ApplicationUser user)
{
Company comp = context.Companies.Find(System.Web.HttpContext.Current.User.Identity.GetUserId());
Employee empl = context.Employees.Find(user.Id);
//empl.Profesion = user.Employe.Profesion;
empl.Family = new Family() {SalaryAcsess = false , GalleryAcsess = false , WeatherAcsess = false , LocationAcsess = false};
comp.Employees.Add(empl);
context.Entry(comp).State = EntityState.Modified;
context.SaveChanges();
}
模型(我将仅发送公司模型,但它会抛出所有模型的异常):
public class Company
{
public Company()
{
Employees = new List<Employee>();
}
[Key]
public string Id { get; set; }
public string Name { get; set; }
[Required]
public virtual ApplicationUser ApplicationUser { get; set; }
public virtual ICollection<Employee> Employees { get; set; }
EF版本:6.0.0.0,运行时版本:v4.0.30319
更新 还有一个&#34; simptom&#34;:当我在UserManager.Create(user,modelEmpl.Password)之前调用Find()时,它正在工作