Abp: Multiple databases, one module - how do I do it? Mulitple contexts?

Created on 8 Jun 2020  路  5Comments  路  Source: abpframework/abp

Hi,

What do I do if I have one module that needs to access 3 different databases.

Creating additional contexts does not seem to work.

When I run the migrator, it fails with an error: Invalid object name 'AbpUsers'

I have read through:

  1. https://docs.abp.io/en/abp/master/Entity-Framework-Core-Migrations
  2. https://docs.abp.io/en/abp/latest/Multi-Tenancy (I do not want multi-tenancy)

The closest I came to a possible answer is at https://docs.abp.io/en/abp/master/Connection-Strings#database-migrations-for-the-entity-framework-core, but I do not understand it

Thank-you!

question

All 5 comments

What do I do if I have one module that needs to access 3 different databases.

Can you explain the use case in detail?

Hi @maliming ,

Thanks for responding. Sorry for the late reply. I have now managed to make it work, but would still need guidance if it is good practice

I have a single module which needs to access 3 different databases.

In the _MyCompany.Project._EntityFrameworkCore, I have 3 different DbContexts of type
AbpDbContext<MyDbContext>, IMyDbContext

Each has a relevant [ConnectionStringName()] attribute

The difference between my additinal DbContexts and the one generated by the ABP template is that they just have the constructor

In the _MyCompany.Project._EntityFrameworkCoreModule, I then add the additional contexts in public override void ConfigureServices(ServiceConfigurationContext context) method

It now works.

Question is, is this the recommended practice?

I think that's a good implementation: Create one DbContext for each database you want to connect with different ConnectionStringName attributes. If target databases have necessary tables then it will work as expected.

Thank-you

Very useful... thanks for sharing. I did the same thing.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ChangYinShung picture ChangYinShung  路  3Comments

hikalkan picture hikalkan  路  3Comments

hikalkan picture hikalkan  路  3Comments

wakuflair picture wakuflair  路  3Comments

SmallShrimp picture SmallShrimp  路  3Comments