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?