Use both AddDbContextFactory() and AddDbContext() extension methods in the same project

It is, it’s all about understanding the lifetimes of the various elements in play and getting those set correctly. By default the DbContextFactory created by the AddDbContextFactory() extension method has a Singleton lifespan. If you use the AddDbContext() extension method with it’s default settings it will create a DbContextOptions with a Scoped lifespan (see the … Read more

How to select top N rows for each group in a Entity Framework GroupBy with EF 3.1

Update (EF Core 6.0): EF Core 6.0 added support for translating GroupBy result set projection, so the original code for taking (key, items) now works as it should, i.e. var query = context.Set<DbDocument>() .Where(e => partnerIds.Contains(e.SenderId)) .GroupBy(e => e.SenderId) .Select(g => new { g.Key, Documents = g.OrderByDescending(e => e.InsertedDateTime).Take(10) }); However flattening (via SelectMany) is … Read more