0

I'm working using ABP framework and Entity Framework and I have a simple query like this:

 // Ensure everything uses the same DbContext (CommonDbContext)
   var organizations = await _organizationRepository.GetQueryableAsync();
   var tenants = await _tenantRepository.GetQueryableAsync();
   var organizationStates = await _organizationStateRepository.GetQueryableAsync();
       //.Where(os => os.StateId == currentStateId);

   var states = await _stateRepository.GetQueryableAsync();

   var linkOptions = from organization in organizations
                     join tenant in tenants on organization.TenantId equals tenant.Id
                     join organizationState in organizationStates on organization.Id equals organizationState.OrganizationId
                     join state in states on organizationState.StateId equals state.Id
                     where tenant.Name.ToLower().Contains(searchTermLower)
                     select new GetLinkedOrganizationsDto
                     {
                         Address = organization.Address,
                         City = organization.City,
                         PostalCode = organization.PostalCode,
                         Country = organization.Country,
                         PhoneNumber = organization.PhoneNumber
                     };

   var result =  linkOptions.ToList();

However, this throws the following error:

Cannot use multiple context instances within a single query execution. Ensure the query uses a single context instance.

Steps Taken to Resolve the Issue

Added SaasDbContext to CommonDbContext: I modified my CommonDbContext to include SaasDbContext as follows:

[ReplaceDbContext(typeof(IIdentityProDbContext))]
[ReplaceDbContext(typeof(ISaasDbContext))]
[ConnectionStringName("Default")]
public class CommonDbContext : AbpDbContext<CommonDbContext>, IIdentityProDbContext, ISaasDbContext
{
    public DbSet<State> States { get; set; }
    public DbSet<Tenant> Tenants { get; set; }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        builder.ConfigureIdentityPro();
        builder.ConfigureSaas();
        builder.ConfigureCommon();
    }
}

Replaced DbContext in the CommonEntityFramework class:

I updated the service configuration in the CommonEntityFramework class to replace the DbContext:

 public override void ConfigureServices(ServiceConfigurationContext context)
{
    context.Services.AddAbpDbContext<CommonDbContext>(options =>
    {
        options.AddDefaultRepositories(includeAllEntities: false);
        options.ReplaceDbContext<ISaasDbContext>();
    });
    ...
}

Current Issue

Despite these changes, the error persists, apparently the TenantRepository is still using SaasDbContext instead of CommonDbContext.

Question Am I missing something in the configuration, or is there another step required to ensure all repositories use the same CommonDbContext?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.