© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
4 replies
zuf

❔ EF Core Query filters and DbContext lifetime

Hey!

To my project,

Using jwt's which contains the users id, after normal auth etc run a middleware runs that will pull data from the database (injecting a DbContext) and create a Identity and add it to the HttpContext.
Now my DbContext is using query filters, those query filters are configured by data from the HttpContext which was modified by my middleware.

The issue is that since my scoped DbContext was already injected in the middleware it was configured with HttpContext data before it was modified with the extra information.

Whats the solution here? I would prefer keeping my DbContext scoped, but the only solution I found so far would be changing it to transient.

The query filter reads from properties in my DbContext (using a factory etc), would it be bad practice to manually assign the values I got from the database in my injected DbContext?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ EF Core, Proper DbContext Instantiation
C#CC# / help
3y ago
✅ Ef-core query efficiency?
C#CC# / help
15mo ago
✅ Ef Core unexpected query behaviour
C#CC# / help
17mo ago
✅ EF Core Query Filter Exception
C#CC# / help
3y ago