Danny Chen Danny Chen - 2 months ago 25
C# Question

How to add/update child entities when updating a parent entity in EF

The two entities are one-to-many relationship (built by code first fluent api).

public class Parent
{
public Parent()
{
this.Children = new List<Child>();
}

public int Id { get; set; }

public virtual ICollection<Child> Children { get; set; }
}

public class Child
{
public int Id { get; set; }

public int ParentId { get; set; }

public string Data { get; set; }
}


In my WebApi controller I have actions to create a parent entity(which is working fine) and update a parent entity(which has some problem). The update action looks like:

public void Update(UpdateParentModel model)
{
//what should be done here?
}


Currently I have two ideas:


  1. Get a tracked parent entity named
    existing
    by
    model.Id
    , and assign values in
    model
    one by one to the entity. This sounds stupid. And in
    model.Children
    I don't know which child is new, which child is modified(or even deleted).

  2. Create a new parent entity via
    model
    , and attached it to the DbContext and save it. But how can the DbContext know the state of children (new add/delete/modified)?



What's the correct way of implement this feature?

Answer

Because the model that gets posted to the WebApi controller is detached from any EF context the only option is to load the object graph (parent including its children) from the database and compare which children have been added, deleted or updated. (Unless you would track the changes with your own tracking mechanism during the detached state (in the browser or whereever) which in my opinion is more complex than the following.) It could look like this:

public void Update(UpdateParentModel model)
{
    var existingParent = _dbContext.Parents
        .Where(p => p.Id == model.Id)
        .Include(p => p.Children)
        .SingleOrDefault();

    if (existingParent != null)
    {
        // Update parent
        _dbContext.Entry(existingParent).CurrentValues.SetValues(model);

        // Delete children
        foreach (var existingChild in existingParent.Children.ToList())
        {
            if (!model.Children.Any(c => c.Id == existingChild.Id))
                _dbContext.Children.Remove(existingChild);
        }

        // Update and Insert children
        foreach (var childModel in model.Children)
        {
            var existingChild = existingParent.Children
                .Where(c => c.Id == childModel.Id)
                .SingleOrDefault();

            if (existingChild != null)
                // Update child
                _dbContext.Entry(existingChild).CurrentValues.SetValues(childModel);
            else
            {
                // Insert child
                var newChild = new Child
                {
                    Data = childModel.Data,
                    //...
                };
                existingParent.Children.Add(newChild);
            }
        }

        _dbContext.SaveChanges();
    }
}

...CurrentValues.SetValues can take any object and maps property values to the attached entity based on the property name. If the property names in your model are different to the names in the entity you can't use this method and must assign the values one by one.