11
Answers

Connection String Use .Net Core

Rajesh Kumar

Rajesh Kumar

2y
1.1k
1

Dear Sir,

Can i call multiple connection string use in .Net Core like ADO.Net and entity framework both . Please advise me. If any solution please give me.

 

Thanks 

Rajesh Kumar

Answers (11)
5
Rajeesh Menoth

Rajeesh Menoth

66 27.1k 2.7m 2y

Hi,

Possible and you can inject the connection strings in the startup class.

public void ConfigureServices(IServiceCollection services) {
    // Add framework services.
    services
        .AddEntityFramework()
        .AddSqlServer()
        .AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnextionString("DefaultConnection")))
        .AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnextionString("NewConnection")));
}

 App settings should be in the following format.


{
  "ConnectionStrings": {
    "DefaultConnection": "Server=.\\SQLEXPRESS;Database=Bar;Trusted_Connection=True;MultipleActiveResultSets=true",
    "NewConnection": "Server=.\\SQLEXPRESS;Database=Bar;Trusted_Connection=True;MultipleActiveResultSets=true"
  }
}

Reference :

https://stackoverflow.com/questions/41525856/using-multiple-connection-strings

Accepted
6
Vishal Yelve

Vishal Yelve

106 17.2k 633.7k 2y

Yes you can use both 

6
Sachin Singh

Sachin Singh

NA 55.8k 87.8k 2y

Yes, you can use both, even i have used both in multiple projects.

6
Jignesh Kumar

Jignesh Kumar

29 39.5k 2.9m 2y

Yes yo can use both togather,

https://social.msdn.microsoft.com/Forums/en-US/37176d9c-f27c-4c7a-954f-f78e6866ed9a/adonet-and-entitty-frame-work-together-in-a-project?forum=aspdotnetcore

4
Rajesh Gami

Rajesh Gami

77 24.4k 1.3m 2y

Yes, You can use both

To define the connection strings in appsettings.json it is important to specify it in the right section of the JSON structure.

{
  "ConnectionStrings": {
    "myDb1": "Server=myServer;Database=myDb1;Trusted_Connection=True;",
    "myDb2": "Server=myServer;Database=myDb2;Trusted_Connection=True;"
  }
}

Now we can read it in our code by calling the GetConnectionString method in the Microsoft.Extensions.Configuration namespace.

string myDb1ConnectionString = _configuration.GetConnectionString("myDb1");
string myDb2ConnectionString = _configuration.GetConnectionString("myDb2");

To be able to call the method you must import the Microsoft.Extensions.Configuration namespace like this:

using Microsoft.Extensions.Configuration;
4
Naimish Makwana

Naimish Makwana

134 13.8k 201.2k 2y

Hello Rajesh,

Please refer below link. 

https://code-maze.com/aspnetcore-multiple-databases-efcore/

Thanks

Naimish Makwana

3
Aravind  Govindaraj

Aravind Govindaraj

314 5.9k 331.9k 2y

Yes you can make it..

Create one class for Ado connection initiation and another for your dbcontext... 

2
Rajeev Kumar

Rajeev Kumar

745 1.1k 74.7k 2y

Hi

Yes you can use Both connectionstrings as below-

{ "ConnectionStrings": { "DefaultConnection": "Server=.\\SQLEXPRESS;Database=Bar;Trusted_Connection=True;MultipleActiveResultSets=true", "NewConnection": "Server=.\\SQLEXPRESS;Database=Bar;Trusted_Connection=True;MultipleActiveResultSets=true" } }

2
Satya Karki

Satya Karki

9 56.7k 3.6m 2y

Yes, you can use multiple connection strings in the same app. Check the implementation example below.
https://stackoverflow.com/questions/41525856/using-multiple-connection-strings

2
Hammad Maqbool

Hammad Maqbool

NA 428 296.6k 2y

Yes you can

in appsettings.json file use the following syntax to define connection strings

{
  "ConnectionStrings": {
    "FirstConString": "connectionstring will go here"
    "SecondConString": "connectionstring will go here"
  },
}
2
Rajan Kumar

Rajan Kumar

1.3k 412 13.5k 2y

yes you use for both like:

{ "ConnectionStrings":

{

"myconnectionstring1": "Server=1.1.1;Database=database1;Trusted_Connection=True;",

"myconnectionstring2": "Server=1.1.2;Database=database2;Trusted_Connection=True;" }

}

and Now we can read it in our code by calling the GetConnectionString method in the Microsoft.Extensions.Configuration namespace. like thiis type:

string connectionstring1= _configuration.GetConnectionString("myconnectionstring1");

string connectionstring2= _configuration.GetConnectionString("myconnectionstring2");