I have a layered architecture for a project as follows.
MyProject.Web <-- ASP.NET Core app
MyProject.BusinessLogic <-- Class Library
MyProject.DataAccess <-- Class Library
Web project references BusinessLogic, which in turn references DataAccess.
The connection string is stored in appsettings.json in Web project as follows.
{
"ConnectionStrings": {
"LocalDatabase": "Server=..."
},
// ...
}
}
How do I get a connection string in the DataAccess project?
appsettings.jsonshould be located in application's entry point. Which is web project. Read the connection string at startup and pass it to data layer. Sorry I don't see any problem yet.Web, then pass it toBusinessLogic, then pass it toDataAccess. This does not look very good.DbOptionsBuilder<TDbContext>, this is what happens behind.AddDbContext<T>method. It is registered with DI and injected into the DbContext when its resolved (or you can do it manually by requesting DbOptionsBuilder via DI and pass it to your context (and modify it before if necessary). Ideally you don't need to pass it anyways, most cases are covered with AddDBContext inside startup.csDataAccess.