Quote:
I have checked the program.cs file and all services are registered as "Transient" in it.
Well, there's your problem. Services registered as "transient" will have a new instance created every time they are resolved.
You have two (or more) repositories which resolve your
DbContext
service, so each will receive a different instance. When you try to write a query for data from multiple repositories, you're querying multiple
DbContext
instances, which isn't supported.
Change your service registration code to register the
DbContext
as "scoped" instead. If you're using EF Core, this is the default lifetime used by the
AddDbContext
method:
EntityFrameworkServiceCollectionExtensions.AddDbContext Method (Microsoft.Extensions.DependencyInjection) | Microsoft Learn[
^]
DbContext Lifetime, Configuration, and Initialization - EF Core | Microsoft Learn[
^]