5
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
Yes, you can use both, even i have used both in multiple projects.
6
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
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
Hello Rajesh,
Please refer below link.
https://code-maze.com/aspnetcore-multiple-databases-efcore/
Thanks
Naimish Makwana
3
Yes you can make it..
Create one class for Ado connection initiation and another for your dbcontext...
2
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
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
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
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");