Reputation: 2352
Parent has a collection of Child. Child has a Foreign Key to Parent (ParentID), but that foreign key might by null / blank. I want Entity Framework to always load Child with null / blank foreign key for all Parent.
public class Parent
{
public string ID { get; set; }
public virtual ICollection<Child> Children { get; set; } //Should load it's children AND children without foreign key.
}
public class Child
{
public string ID { get; set; }
public string ParentID { get; set; } //This can be null / blank.
}
Upvotes: 0
Views: 778
Reputation: 11773
Without a ParentId, the child is what's known as an orphan. There's no way to link a child to a parent without a populated foreign key property.
If you're just looking to query the Children for records with a null or empty ParentId, do something like the following with your DbContext:
var orphans = myContext.Children.Where(child => String.IsNullOrEmpty(child.ParentId));
Upvotes: 2