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:
-
Get a tracked parent entity named
existing
bymodel.Id
, and assign values inmodel
one by one to the entity. This sounds stupid. And inmodel.Children
I don't know which child is new, which child is modified(or even deleted). -
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?
Best Answer
Because the model that gets posted to the WebApi controller is detached from any entity-framework (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 wherever) which in my opinion is more complex than the following.) It could look like this:
...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 from the names in the entity you can't use this method and must assign the values one by one.