question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support to multiple databases in application

See original GitHub issue

Hi @hikalkan,

I wanna know how I can use multiple databases in my application. There are any attribute and/or interface to decorate repository to use a specific connectionString/database in repository.

PS: I’m new in ABP, I read the documentation and I don’t found anything to help me.

Could be anything like this:

[UseDatabase("ConnectionStringName")]
public class TaskRepository : SimpleTaskSystemRepositoryBase<Task, long>, ITaskRepository
{
    public TaskRepository(IDbContextProvider<SimpleTaskSystemDbContext> dbContextProvider)
        : base(dbContextProvider)
    {
    }
}

Thanks,

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
hikalkancommented, Apr 1, 2016

Hi,

It can not be in repository level, but in dbcontext level. Why don’t you create more than one dbcontext? There is an example: https://github.com/aspnetboilerplate/aspnetboilerplate-samples/tree/master/MultipleDbContextDemo

1reaction
hikalkancommented, Sep 7, 2018

Hi,

Solution 1

The best way, without workaround is to use different classes. For instance, you can derive from Person as a SecondPerson entity and use Person in first dbcontext and use SecondPerson in the second one. In this way, no additional setting is needed, it will just work as expected.

Solution 2

_unitOfWorkManager.Current.SetContextId(firstDbId) makes your code EF depended. If this is not a problem, then

  1. Resolve all implementations of a given repository:

iocManager.IocContainer.ResolveAll<IRepository<Person>>();

(inject IIocManager interface as iocManager)

  1. Once you get instances, you can cast them to IRepositoryWithDbContext and use GetDbContext() method. Then check if the given DbContext is the one you want.

You can create a helper method to not repeat yourself every time you need that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Integrate Multiple Databases
Check out our in-depth guide to learn how to integrate multiple databases, for all kinds of app projects.
Read more >
Managing Multiple Databases from a Single Application
Querying across Multiple Databases. When it comes to SQL queries, most DBMSes support a standardized set of SQL statements and functions.
Read more >
How to create an application supporting multiple databases
I have a situation where I need to create an application which supports multiple databases. Multiple databases means the client can use any ......
Read more >
How Do I Provide More than One Database for My ...
A step-by-step guide on how you can provide more than one database for your application on the Cloudways Platform.
Read more >
Multiple Databases with Active Record
Multiple Databases with Active Record. This guide covers using multiple databases with your Rails application. After reading this guide you will know:.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found