It's the first time my .Include()
doesn't return the entity. I don't know what is missing.
Billings
class
public Order Order { get; set; }
public List<BillingItem>? BillingItems { get; set; }
ApplicationDbContext
:
modelBuilder.Entity<Billing>()
.HasOne(e => e.Order)
.WithOne(e => e.Billing)
.HasForeignKey<Billing>(e => e.OrderID)
.OnDelete(DeleteBehavior.Restrict);
Execution watch variable
It's the first time my .Include()
doesn't return the entity. I don't know what is missing.
Billings
class
public Order Order { get; set; }
public List<BillingItem>? BillingItems { get; set; }
ApplicationDbContext
:
modelBuilder.Entity<Billing>()
.HasOne(e => e.Order)
.WithOne(e => e.Billing)
.HasForeignKey<Billing>(e => e.OrderID)
.OnDelete(DeleteBehavior.Restrict);
Execution watch variable
For inserting a new item where one doesn't already exist:
var billing await db.Billings
.Include(b => b.Order)
.ThenInclude(o => o.OrderItems)
.SingleOrDefaultAsnc(b => b.OrderId == orderId && b.Status != Status.Deleted);
if(billing != null)
{
billing = new Billing();
order = await db.Orders
.Include(o => o.OrderItems)
.SingleAsync(o => o.Id == orderId && o.Status != Status.Deleted);
billing.CreateFromOrder(order);
db.Billings.Add(billing);
await db.SaveChangesAsync();
}
Fetch the existing billing with associated Order/OrderItems at the start. If we don't find one, create one, fetching the Order /w OrderItems, and associating that to the new billing, then Add()
the billing before saving.
We don't need to attempt to reload the billing after inserting, we can fetch any existing billing at the start. Importantly use DbSet.Add()
not Attach()
to ensure EF knows this is a new Billing. Attach()
is only used when dealing with detached, existing entities we want to ensure the DbContext treats as existing rows. For inserts we use tracking queries for any entities we want to associate (Order/OrderItems).
If we just wanted to check if a billing does not exist then we don't need to load it, we can just do an exists check /w .Any()
:
var billingExists db.Billings
.Any(b => b.OrderId == orderId && b.Status != Status.Deleted);
if(billing != null)
{
Billing billing = new Billing();
...
One important change I highly recommend to avoid errors when it comes to navigation properties is wherever you have collection navigation properties such as Order.OrderItems you should not have an accessible setter. For instance code like order.OrderItems = orderItems
is very bad as the change tracker will have had a proxy in place to notice when collection items are added/removed and when you re-assign a collection, that proxy is blasted.
Instead of:
public ICollection<OrderItem> OrderItems { get; set; }
Collection navigation properties should be declared as:
public virtual ICollection<OrderItem> OrderItems { get; } = [];
// or
public virtual ICollection<OrderItem> OrderItems { get; protected set; } = new List<OrderItem>(); // for older C# versions.
No code should ever set a collection navigation property after it is initialized.