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.

Erçin Dedeoğlu
  • 4,950
  • 4
  • 49
  • 69
  • 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 – Panagiotis Kanavos Oct 09 '18 at 09:06
  • 1
    Thanks for response. Could you give me an example with answer please? – Erçin Dedeoğlu Oct 09 '18 at 09: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 – G Clovs Mar 04 '21 at 11:23
  • And yeah I want to add my own "clean and readable" answer, to this legitimate question Regards – G Clovs Mar 04 '21 at 11:27

0 Answers0