Suppose I have a simple entity with children:
public class Entity
{
public Guid ID { get; set; }
public ICollection<Child> Children { get; set; }
}
public class Child
{
public Guid ID { get; set; }
public Guid ParentEntityID { get; set; }
}
The "Children" property is configured as a one to many relationship. There a few scenarios below that I need clarification on what will happen.
Scenario 1: Assume I load the entity but don't "Include" the Children property:
- If I then initialize the collection to an empty list and save it... will that wipe out the children or do nothing? I've read that initializing collections is optional, which suggests that initializing it to an empty set is beneign. However, that contradicts the notion that the assigned set represents the related objects, in which case assigning an empty set would suggest you want to clear the related objects.
Scenario 2: Assume I load the entity and also "Include" the Children property:
- If I clear the collection and save the entity, would that wipe out the children? Likewise, since it was included, if I assign a new empty set, will it remove the related objects? In other words, does assigning a new empty set and saving the entity function differently when I include the navigation property vs not include it?
Scenario 3: Assume I have thousands of Children and want to add or remove just a few of them:
- Do I have to "Include" (i.e. preload) the entire collection in order to add or remove items? If so, how can I perform a partial update of the set without preloading all the children?