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
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
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Rajeesh MenothRank #118 · 18.4K reputationPosted Jan 19, 2023, 9:16 AM EST
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(options => options.UseSqlServer(Configuration.GetConnextionString("DefaultConnection"))) .AddDbContext(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
Jignesh KumarRank #91 · 22.9K reputationPosted Jan 19, 2023, 5:26 AM EST
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
Sachin SinghRank #66 · 26.2K reputationPosted Jan 19, 2023, 5:47 AM EST
Yes, you can use both, even i have used both in multiple projects.
Vishal YelveRank #178 · 13.6K reputationPosted Jan 19, 2023, 8:42 AM EST
Yes you can use both
Naimish MakwanaRank #324 · 7.7K reputationPosted Jan 19, 2023, 5:58 AM EST
Hello Rajesh,
Please refer below link.
https://code-maze.com/aspnetcore-multiple-databases-efcore/
Thanks
Naimish Makwana
Rajesh GamiRank #30 · 41.5K reputationPosted Jan 19, 2023, 9:39 AM EST
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;Aravind GovindarajRank #430 · 5.8K reputationPosted Jan 19, 2023, 1:44 PM EST
Yes you can make it..
Create one class for Ado connection initiation and another for your dbcontext...
Rajan KumarRank #4,480 · 341 reputationPosted Jan 20, 2023, 2:10 AM EST
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");
Hammad MaqboolRank #3,594 · 405 reputationPosted Jan 20, 2023, 10:44 AM EST
Yes you can
in appsettings.json file use the following syntax to define connection strings
Satya KarkiRank #78 · 24.6K reputationPosted Jan 23, 2023, 2:20 AM EST
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
Rajeev KumarRank #1,334 · 1.3K reputationPosted Mar 14, 2023, 3:26 AM EDT
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" } }
Sandhiya PriyaRank #26 · 43.2K reputationPosted Jan 2, 2026, 6:51 AM EST
Yes, you can absolutely use multiple connection strings in .NET Core, and you can use them together with ADO.NET and Entity Framework Core in the same application. This is very common in real projects.
Below is a clear, short, and practical explanation.
1. Define multiple connection strings (appsettings.json)
{ "ConnectionStrings": { "EFConnection": "Server=.;Database=AppDb;Trusted_Connection=True;", "ADOConnection": "Server=.;Database=LegacyDb;Trusted_Connection=True;" } }You can add any number of connection strings.
2. Use connection string with Entity Framework Core
Program.cs / Startup.cs
builder.Services.AddDbContext(options => options.UseSqlServer(builder.Configuration.GetConnectionString("EFConnection")));EF Core will use EFConnection only.
3. Use connection string with ADO.NET
string connStr = configuration.GetConnectionString("ADOConnection"); using SqlConnection con = new SqlConnection(connStr); using SqlCommand cmd = new SqlCommand("SELECT * FROM Users", con); con.Open(); using SqlDataReader reader = cmd.ExecuteReader();ADO.NET will use ADOConnection.
4. Use BOTH EF Core and ADO.NET in the same service (Recommended)
public class UserService { private readonly AppDbContext _context; private readonly IConfiguration _config; public UserService(AppDbContext context, IConfiguration config) { _context = context; _config = config; } public List GetUsersEF() { return _context.Users.ToList(); } public DataTable GetUsersADO() { using SqlConnection con = new SqlConnection(_config.GetConnectionString("ADOConnection")); // ADO.NET logic } }This is fully supported
Clean and maintainable
Best for legacy + new code
5. Using multiple DbContexts (optional)
If you want multiple databases with EF Core:
builder.Services.AddDbContext(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DB1"))); builder.Services.AddDbContext(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DB2")));When should you mix EF Core & ADO.NET?