Ends in:
7 DAYS
21 HRS
38 MIN
37 SEC
Ends in:
7 D
21 H
38 M
37 S
Open Closed

Using LINQ with Multiple DbContexts in ABP Framework #7833


User avatar
0
IbrahimSarigoz created

Hello;

I have two projects: ModuleA and DemoCenterApp. DemoCenterApp can use ModuleA.

ModuleA contains an entity named Iletisim. DemoCenterApp contains an entity named Musteri.

The databases of both projects are different.

In DemoCenterAppDbContext, I used [ReplaceDbContext(typeof(IModuleADbContext))] and added:

public DbSet<Iletisim> Iletisims { get; set; }

My problem is that I want to perform a single LINQ query in EfCoreMusteriRepository that includes Iletisim.

However, I am getting the following error:

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

I followed the method described in this YouTube video. As mentioned there, isn't using ReplaceDbContext sufficient?

  • ABP Framework version: v8.2.2
  • UI Type: Angular / MVC
  • Database System: EF Core (Oracle)
  • Tiered (for MVC) or Auth Server Separated (for Angular): yes

9 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    The databases of both projects are different.

    What does that mean?

    ModuleA is a module, why is has use a database?

  • User Avatar
    0
    IbrahimSarigoz created

    https://blog.antosubash.com/posts/abp-add-new-module-with-seperate-db

    Our requirement is this: we have a large project with separate CRM and ERP modules, each with its own database. I want to use these modules' DbContexts in my central application, but we couldn't connect two different DbContexts in the same LINQ query. However, a YouTube video suggests it's possible. How can this be achieved?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    but we couldn't connect two different DbContexts in the same LINQ query.

    I don't think this can be done.

    https://github.com/dotnet/efcore/issues/32872#issuecomment-1910570373

  • User Avatar
    0
    IbrahimSarigoz created

    [ReplaceDbContext(typeof(IIdentityProDbContext))] [ReplaceDbContext(typeof(ISaasDbContext))]

    As you can see in the image, the first method results in an error because the audit tables use a different DbContext. However, the second method works successfully. So, why are we using ReplaceDbContext ?

  • User Avatar
    0
    ahmetfarukulu created

    Hi @IbrahimSarigoz,

    Did you use the same interface in your EfCoreIletisimRepository? It should like;

    public class EfCoreIletisimRepository : EfCoreRepository<IModuleADbContext, Iletisim, Guid>, IIletisimRepository
    {
        public EfCoreIletisimRepository(
            IDbContextProvider<IModuleADbContext> dbContextProvider) 
            : base(dbContextProvider)
        {
        }
    }
    

    Lastly don't forget the add repository to your ModuleNameEntityFrameworkCoreModule class.

    public class ModuleAEntityFrameworkCoreModule : AbpModule 
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            context.Services.AddAbpDbContext<ModuleADbContext>(options =>
            {
                options.AddRepository<Iletisim, EfCoreIletisimRepository>();
            });
        }
    }
    
  • User Avatar
    0
    IbrahimSarigoz created

    I completed all of these. If you want, I can share the github repo with you.

  • User Avatar
    0
    ahmetfarukulu created

    No need, I created a sample application that you can check out in abp-samples. Before testing it, make sure to build the entire solution using the dotnet build command. Afterward, you can debug the SampleRepositoryTests in the EntityFrameworkCore.Tests project.

  • User Avatar
    0
    IbrahimSarigoz created

    Hello, thank you for your response

    Currently, when the 'Contact' entity is in the same database and tablespace, everything works fine. However, we need to use a separate tablespace for this entity (module). When I create a different tablespace and attempt to use this entity in linq, I encounter the following error:

    'Cannot use multiple context instances within a single query execution. Ensure the query uses a single context instance.' System.InvalidOperationException: 'Cannot use multiple context instances within a single query execution. Ensure the query uses a single context instance.

    I did some research, and they say it doesn't work with different tablespaces or databases in one linq. I want to make sure that it also doesn't work in the ABP framework.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    The ABP framework also uses EF Core as a repository implementation. so, it doesn't work in ABP either.

Made with ❤️ on ABP v9.1.0-preview. Updated on November 20, 2024, 13:06