Is there a way to have EF Core populate navigation properties before the change tracker snapshots values?

I have an order model in my core with a collection of (abstract base) OrderLines which can be either a NoteLine or an ItemLine. Each ItemLine has various pricing details relevant to that specific line.

public class Order : IEntity<int>
{
    public int Id { get; private set; }

    protected ICollection<OrderLine> _Lines;
    public IEnumerable<OrderLine> Lines => _Lines.AsEnumerable();

    public decimal TotalSellEx
    {
        get => _Lines.OfType<ItemLine>().Sum(l => l.TotalSellEx);
        protected set { }
    }
}

public class NoteLine
{
    public string Note { get; protected set; }
}

public class ItemLine
{
    public int StockId { get; protected set; }

    public decimal TotalSellEx => ItemSellEx * Quantity;
    public decimal ItemSellEx { get; protected set; }
    public decimal GrossProfit { get; protected set; }
    public int Quantity { get; protected set; }
}

public abstract class OrderLine
{
    public decimal LineNumber { get; protected set; }
}

public class OrderConfiguration : EntityConfiguration<Order, int>
{
    public override void Configure(EntityTypeBuilder<Statement> builder)
    {
        base.Configure(builder);

        builder.HasMany(o => o.Lines)
            .WithOne(o => o.Statement);

        builder.Navigation(o => o.Lines)
            .AutoInclude();
    }
}

My Order model then uses a getter only property to keep a rolling sum of the total values as the list is manipulated by various functions and is successfully persisted to the database due to the empty setter to get around EF Core's need for a setter.

The problem occurs when I pull the record out of the database and change tracker kicks in. It seems to be that the snapshot of this model is created before navigation properties are populated so the total value is always 0 in the change tracker.

This isn't a big problem until the total value is modified in such a way that it becomes 0 properly. In which case EF Core doesn't see a change and doesn't populate the UPDATE statement with this column. At this point any projections containing this property are wrong, showing the previous value before it was set back to 0.

For the record, it always seems that once the object is retrieved the lines are all there, just not during change tracking snapshotting.

Is there a way to get around this behaviour? Or do I have to settle for bandaid work arounds?

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum