简体   繁体   English

EF导航属性更新

[英]EF navigation property update

Sorry for my bad English. 对不起,我的英语不好。

I have some class and EF Context generate from DB 我有一些从数据库生成的类和EF上下文

1.Question 1.Question

public int Id { get; set; }
public string Content { get; set; }

public virtual ICollection<Answer> Answer { get; set; }

2.Answer 2.Answer

public int Id { get; set; }
public string Content { get; set; }
public bool IsTrue { get; set; }

public virtual Question Question { get; set; }

All CRUD made through the Service. 通过服务进行的所有CRUD。 This is Update method for Question 这是问题的更新方法

public void Update(Question q)
{
   var editQuestion = GetById(q.Id); //get entity by id
   editQuestion.Answer = null;
   _repository.Update(editQuestion);

    q.Answer.Each(answer => answer.Question=editQuestion);

    editQuestion.Answer = q.Answer;
    editQuestion.Content = q.Content;

   _repository.Update(editQuestion);
   _unitOfWork.Commit();
}

_repository.Update(T Entity) => _repository.Update(T实体)=>

public virtual void Update(T entity)
{
   DbEntityEntry entityEntry = _context.Entry<T>(entity);
   entityEntry.State = EntityState.Modified;
}

Now in Db not update old Answer, but added new (q.Answer). 现在在Db中,不更新旧的Answer,而是添加新的(q.Answer)。 In q.Answer may contains updated old Answer and some new. 在q.Answer中可能包含更新的旧答案和一些新答案。

How I can update Question? 如何更新问题?

UPD: _unitOfWork.Commit() UPD:_unitOfWork.Commit()

public void Commit()
{
   _context.SaveChanges();
}

There is no need to play with the entry state. 无需使用进入状态。 Simply get the entity by its Id, modify the relevant properties, remove old answers from the context, add the new ones to the collection, call SaveChanges() and it's job done. 只需通过其ID获得实体,修改相关属性,从上下文中删除旧答案,将新答案添加到集合中,调用SaveChanges()完成工作。

Typically, one would write something like: 通常,人们会这样写:

public void Update(Question q)
{
  using(var context = new MyDbContext())
  {
    var entity = context.Question.Find(q.Id);
    entity.Content = q.Content;

    foreach(var oldAnswer in entity.Answer)
    { 
      context.Answers.Remove(oldAnswer);
    }

    entity.Answer.Clear(); 

    foreach(var newAnswer in q.Answer)
    {
      entity.Answer.Add(newAnswer);
    }

    context.SaveChanges();
  } 
}

If you are not using the using pattern, don't forget to dispose the context at the end by calling context.Dispose() . 如果您没有使用using模式,请不要忘记通过调用context.Dispose()在最后放置上下文。

Please note this is a solution where answer records would be deleted from the database and re-created even if they have not changed. 请注意,这是一种解决方案,即使未更改答案记录,也将从数据库中删除它们并重新创建。

Alternatively, you could write a solution where existing records would be updated and only answers that no longer exist would be deleted: 或者,您可以编写一个解决方案,其中将更新现有记录,而仅删除不再存在的答案:

public void Update(Question q)
{
  using(var context = new MyDbContext())
  {
    var entity = context.Question.Find(q.Id);
    entity.Content = q.Content;

    foreach(var newAnswer in q.Answer)
    {
      if (entity.Answer.All(a => a.Id != newAnswer.Id)
      {
        entity.Answer.Add(newAnswer);
      }
    }

    foreach(var oldAnswer in entity.Answer)
    { 
      if (q.Answer.All(a => a.Id != oldAnswer.Id)
      {
        context.Answers.Remove(oldAnswer);
      }
      else
      {
        var newAnswer = q.Answer.Single(a => a.Id == oldAnswer.Id);
        oldAnswer.Content = newAnswer.Content;
        oldAnswer.IsTrue = newAnswer.IsTrue;
      }
    }

    context.SaveChanges();
  } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM