4

I have 2 database connections on my project. I want to reach them from controllers with constructor.

I have added these to IServiceCollection.

serviceCollection.AddTransient<IDbConnection>(db => new OracleConnection(Configuration.Database1));
serviceCollection.AddTransient<IDbConnection>(db => new OracleConnection(Configuration.Database2));

When I tried to reach one of them, always lastone (Database2) is coming to controller

 : Controller
public ExtensionCoreController(IDbConnection connection)
{
}

How can I select one?

Thanks advance.

4
  • 2
    You can't have multiple implementations. How is the IoC container going to decide which connection you want? You should inject different repositories, custom purpose-specific classes (eg SourceConnection,TargetConnection), or a connection factory method that can take a parameter and return a connection instance Commented Oct 9, 2018 at 9:06
  • 1
    Thanks for response. Could you give me an example with answer please? Commented Oct 9, 2018 at 9:08
  • Hey, @PanagiotisKanavos Can you reopen the question? This question, sure is related to multiple implementation interface, but it is also specifiq to IDbConnection, This Post is top one on Google search "Multiple IDbConnection" It should have a proper, and direct answer. You will help the community, thx Commented Mar 4, 2021 at 11:23
  • And yeah I want to add my own "clean and readable" answer, to this legitimate question Regards Commented Mar 4, 2021 at 11:27

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.