【发布时间】:2015-09-30 13:56:10
【问题描述】:
早上好!
我在 Employee 和 Skill 实体之间有一个多对多的关系。当我创建一个新员工时,我选择的技能会毫无问题地添加到数据库中。但是,当我更新员工时,员工内容会更新,但不会添加/删除任何技能。我看到它们被传递到存储库,但它没有更新数据库。
我有以下多对多关系:
public class Employee : BaseEntity
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string JobTitle { get; set; }
public virtual ICollection<Skill> Skills { get; set; }
}
和:
public class Skill : BaseEntity
{
public string Name { get; set; }
public virtual ICollection<Employee> Employees { get; set; }
}
我的控制器通过以下方法添加/删除员工的技能:
public ActionResult Edit(int id, EmployeeEditViewModel viewModel)
{
try
{
if (!ModelState.IsValid)
{
viewModel.SkillsList = _skillService.GetAll().ToList();
return View(viewModel);
}
var employee = Mapper.Map<Employee>(viewModel);
UpdateSkills(employee, viewModel.NewSkills);
_employeeService.Update(employee);
return RedirectToAction("Index");
}
catch(Exception e)
{
ModelState.AddModelError("", e.Message);
viewModel.SkillsList = _skillService.GetAll().ToList();
return View(viewModel);
}
}
private void UpdateSkills(Employee employee, IEnumerable<int> updatedSkills)
{
if (employee.Skills != null)
{
var updatedSkillsList = updatedSkills as IList<int> ?? updatedSkills.ToList();
var addedSkills = updatedSkillsList.Except(employee.Skills.Select(x => x.Id));
var removedSkills = employee.Skills.Select(x => x.Id).Except(updatedSkillsList);
addedSkills.ForEach(x => employee.Skills.Add(_skillService.GetById(x)));
removedSkills.ForEach(x => employee.Skills.Remove(_skillService.GetById(x)));
}
else
{
employee.Skills = new List<Skill>();
newSkills.ForEach(x => employee.Skills.Add(_skillService.GetById(x)));
}
}
然后使用通用存储库插入/更新员工:
public void Insert(TEntity entity)
{
if (entity == null)
throw new ArgumentNullException("entity");
try
{
_dbSet.Add(entity);
_dbContext.SaveChanges();
}
catch (DbEntityValidationException ex)
{
ThrowValidationError(ex);
}
}
public void Update(TEntity entity)
{
if (entity == null)
throw new ArgumentNullException("entity");
try
{
_dbSet.Attach(entity);
_dbContext.Entry(entity).State = EntityState.Modified;
_dbContext.SaveChanges();
}
catch (DbEntityValidationException ex)
{
ThrowValidationError(ex);
}
}
这是从数据上下文中调用 Employee 对象的方式。
我的构造函数:
protected readonly NTierApplicationsDataContext _dbContext;
protected readonly DbSet<TEntity> _dbSet;
public EfRepository(NTierApplicationsDataContext dbContext)
{
_dbContext = dbContext;
_dbSet = _dbContext.Set<TEntity>();
}
这里是获取对象的find方法:
public TEntity GetById(int id)
{
return _dbSet.Find(id);
}
【问题讨论】:
-
你如何获得`employee'对象?你从上下文中得到它吗?为了使功能正常工作,EF 必须激活对象跟踪。
-
是的,在我的通用存储库中,我从应用程序数据上下文中调用它。
-
添加了上面的代码。
标签: c# asp.net entity-framework