I have an object with two objects as properties ( User , PrimaryNode ), both potentially could be null, see below:
public class Item { [Key] public int ItemId { get; set; } public string ItemName { get; set; } public Node PrimaryNode { get; set; } public User User { get; set; } }
I am using Entity Framework 6 to populate the Item object and use PrimaryNode to populate the PrimaryNode and User objects inside it.
When the first related Include has a null object, then the whole object is returned as null, for example:
using (var db = new MyContext()) { var item = db.Items.Include(i => i.User).Include(n => n.PrimaryNode).FirstOrDefault(i => i.ItemId == id); }
If i.User is null in the above example, then the Item variable is null. What is the best way to populate both sub-objects in such a way that if the sub-object is null then the parent object and the other sub-object will still be filled?
source share