Open Closed

How to use repository for multiple dbcontext #3358


User avatar
0
cala created

Hi, we created a "default" repository class that should be used by all our modules. But we have no idea how to configure it to be useable by other module dbcontext. our idea was to have a base dbcontext (IBaseDbContext) that can be used instead of the IEfCoreDbContext.

so we tried:

  • IDbContext interface inherit from IBaseDbContext instead of IEfCoreDbContext
  • DbContext implement IBaseDbContext additionally
  • replace IBaseDbContext with new DbContext ( tried attribute, module option and both together )
  • manual SetDefaultRepositoryClasses on DbContext

without manual SetDefaultRepositoryClasses ( on each DbContext ) you get the default EfCoreRepository ( instead of the new "default" repository ) without ReplaceDbContext you InvalidOperationException because the Type is not included in the context

  • ABP Framework version: v5.3.0
  • DB provider: EF Core

In our prototype project we used only one dbcontext for all module, so SetDefaultRepositoryClasses has done the job. But now we are struggling to split it into seperated dbcontext for each module. What would be the best approach for this ?

Thanks


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

    hi

    You can create separate DbContext and Repository in your modules. Please check out our open source modules or the default module template.

    https://github.com/abpframework/abp/tree/dev/modules https://docs.abp.io/en/abp/latest/Entity-Framework-Core

  • User Avatar
    0
    cala created

    Well we would like to know, how to use one repository for multiple dbcontext - none of your modules share the repository.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    If multiple dbcontext have the same entity, they can be used independently, but not together.

  • User Avatar
    0
    cala created

    so we need to make the base repository generic and create a version for each module. It would be great to be able to override the generic EfCoreRepository independently from the dbcontext

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    EfCoreRepository and dbcontext are independent.

    so we need to make the base repository generic and create a version for each module

    You can give it a try

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