I'm using Blazor Server Side and EF Core for a project. The issue I have: change tracking is redundant as it is suggested to create short lived contexts using DB factory.
E.g.
public class OrderService{ private readonly IDbContextFactory<ApplicationDbContext> _contextFactory; public OrderService(IDbContextFactory<ApplicationDbContext> contextFactory) { _contextFactory = contextFactory; } public Order GetById(int id) { using var context = _contextFactory.CreateDbContext(); return context.Orders .Include(o => o.Lines) .ThenInclude(o => o.Product) .Where(o => o.OrderId == id).FirstOrDefault(); }}This means you can't just pass the object from GetById to a component, let the user edit via the component, then reattach (ctx.Attach(order)) and save, as nothing will be flagged as modified.
Using ctx.Update(order), then saving, will resave everything within that entity, potentially there could be 100's of lines of items within the order that will get saved to the database needlessly.
Is there a known pattern for the best way to handle this?
I thought about attaching the returned entity to a fresh DbContext, but never querying or saving the to the database with that context. I hope it will not open a database connection, and therefore not need explicitly disposing.
Then I can query that DbContext's ChangeTracking to get the changes, them pass them changes to the correct update, add, remove short lived db contexts.
Or better yet, just using the change tracking abilities of EF Core, without a DbContext?