I have an Entity for which I have property that’s dependent on a virtual member. While loading the entity I am getting the virtual member as null even though I have included it in .Include.
My entity looks like this
public class Bill
{
private int _totalQuantityStored;
public static Bill Create()
{
var bill = new Bill();
InitializeVirtualMembers(bill);
}
private static void InitializeVirtualMembers(Bill bill)
{
bill.AllSales = new List<Sales>();
}
private Bill()
{
}
[AutoIncrement]
public int BillId { get; set; }
public int TotalQuantity
{
get
{
if (_totalQuantityStored > 0)
{
return _totalQuantityStored;
}
if (AllSales is null)
{
throw VirualMemberNotLoadedException<Sales>(nameOf(AllSales));
}
}
set => _totalQuantityStored = value;
}
public virtual IList<Sales> AllSales { get; private set; }
}
Command i use to load my entity
var bill = context.Bills.Where(b => b.BillId == billId).Include(b => b.AllSales).SingleAsync();
While loading I get VirualMemberNotLoadedException
which should have been thrown only when AllSales is null. But since I am including it in .Include
i expect it to be non null.
Here if the relace the exception throwing with return 0; and then look at the bill
that I fetched from the database in debug mode I see AllSales have indeed been loaded and is not null, but if I see the same during loading of object while butting debuger on if (AllSales is null)
then it is indeed null there.
I would expect Ef core to load all the included entities first and then load the properties of parent entity, or at least call the get of the loaded entities after it has loaded all the Included properties.
Due to this I am unable to move ahead with my current model of having my property be dependent on entity and throwing excepting if property is fetched with default value and the corresponding virtual member has not been loaded.