Given the following over-simplified code:
public class Child
{
public virtual Parent Parent { get; set; }
}
public class Parent
{
public List<Child> Children { get; set; }
}
In my consumer code, I'd have:
parent.Children.Add(child);
This does not set the child.Parent, until I call db.SaveContext();
I see situations in which this is a problem, e.g. chaining a couple of operations on the same object before saving.
My question is, should I be doing this instead:
class Child
{
public virtual Parent Parent { get; set; }
public void SetParent(Parent parent) {
if (this.Parent != null) { this.Parent.Children.Remove(this); }
parent.Children.Add(this);
this.Parent = parent;
}
}
Please note the code snippet is just for illustrative purpose.
Generally my question is, should I handle the relational fix up myself, instead of relying on EF.