【发布时间】:2015-02-19 06:16:32
【问题描述】:
将新员工保存到数据库时遇到一些麻烦。
项目模型(删除无用字段):
public class Project{
public virtual ICollection<EmployeeManager.Employee> Employees { get; set; }
}
EmployeeManager.Employee 模型:
public class Employee{
public virtual ApplicationUser User { get; set; }
public virtual ProjectsManager.Project ProjectObj { get; set; }
}
功能:
public async Task AddNewEmployeeAsync(string projectId, string email, string fullname){
var projectInfo = await GetByIdAsync(projectId);
var userInfo = new ApplicationUser();
if (await _context.Users.AnyAsync(_ => _.Email == email)){
userInfo = await _context.Users.FirstAsync(_ => _.Email == email);
}
else{
....
userInfo = user;
}
projectInfo.Employees.Add(new EmployeeManager.Employee{
User = userInfo
});
_context.Entry(projectInfo).State = EntityState.Modified;
_context.SaveChanges();
}
毕竟,我的数据库没有任何变化。
为什么?
如果我使用 _context.Employees.Add() -> 它会在数据库中创建新的项目实体。
【问题讨论】:
标签: asp.net entity-framework entity-framework-6