简体   繁体   English

在 EF 6 中更新父实体时如何删除子实体?

[英]How to delete child entities when updating a parent entity in EF 6?

This is my entities:这是我的实体:

public class Question
{
    public int Id { get; set; }
    public string Text { get; set; } = string.Empty;
    public ICollection<Answer> Answers { get; set; } = new List<Answer>();
    public TimeSpan TimeForAnswer { get; set; }
    public int TestId { get; set; }
}
public class Answer
{
    public int Id { get; set; }
    public string Text { get; set; } = string.Empty;
    public int QuestionId { get; set; }
    public int Points { get; set; }
}

If I update just my parent entity:如果我只更新我的父实体:

public void Update(Question entity)
{
    _dbContext.Questions.Update(entity);
}

EF will add/update child entities, but not delete if any missing. EF 将添加/更新子实体,但如果有任何缺失则不会删除。 I searched, and found this post .我搜索了一下,找到了这篇文章 And modified my method like this:并像这样修改了我的方法:

public void Update(Question entity)
{
    var existingParent = _db.Questions
        .Include(p => p.Answers)
        .SingleOrDefault(p => p.Id == entity.Id);
        
    if (existingParent != null)
    {
        existingParent.Answers.Clear();
    }

    _db.Questions.Update(entity);
}

And now I 'am getting this error:现在我收到这个错误:

System.InvalidOperationException: The instance of entity type 'Question' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.

How to fix this?如何解决这个问题?

The call to:调用:

_db.Questions.Update(entity);

tries to start tracking entity, but it's already being tracked by the query:尝试开始跟踪实体,但它已被查询跟踪:

var existingParent = _db.Questions
    .Include(p => p.Answers)
    .SingleOrDefault(p => p.Id == entity.Id);

I'd go for:我会 go 为:

public void Update(Question entity)
{
    var existingParent = _db.Questions
        .Include(p => p.Answers)
        .SingleOrDefault(p => p.Id == entity.Id);
        
    if (existingParent != null)
    {
        // Update Parent Entity Properties
        existingParent.Text = entity.Text;
        existingParent.TimeForAnswer = entity.TimeForAnswer;
        existingParent.TestId = entity.TestId;
        // remove existing from tracked Question
        existingParent.Answers.Clear();
        // add answers from Question in method param
        existingParent.Answers.AddRange(entity.Answers);
    }
    else
    {
        // Add the question with answers to the db
        _db.Questions.Add(entity);
    }
}

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

相关问题 如何在 EF 中更新父实体时添加/更新子实体 - How to add/update child entities when updating a parent entity in EF 在 EF 中更新父实体时更新子实体 - update child entities when updating a parent entity in EF 如何从 EF Core 中的父实体中删除子项? - How do you delete Child from Parent entities in EF Core? 在 Entity Framework Core 中更新父实体时添加或更新子实体 - Add or Update child entities when updating a parent entity in Entity Framework Core 多个父实体首先在EF代码中包含一个子实体 - multiple parent Entities with one child entity in EF code first 无法在EF4中将父实体与现有子实体一起添加 - Unable to add parent entity with existing child entities in EF4 避免使用 EF Core 在子实体上附加父实体 - Avoid attaching parent entity on child entities using EF Core 子实体添加到父实体时不保留 - Child entities not persisting when added to parent entity 父实体上的软删除,但子/关系 EF Core 上的硬删除 - Soft delete on Parent Entity but Hard delete on Child/Relationship EF Core 为什么先更新EF代码中的父实体和子实体时,为什么不必更新父对象 - Why don't I have to update a parent object when updating both the parent and child entity in EF code first
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM