I'm trying to configure the app to use my class (derived from DbContext
) ApplicationDbContext
to connect to my database. I already made the configuration file appsetting.json
:
"Data": {
"SportStoreProducts": {
"ConnectionStrings":"Server=(localdb)\\MSSQLLocalDB;Database=SportStore;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
I used dependency injection to pass the object implementing IConfiguration
(which is appsetting.json
) through Startup construction :
public class Startup
{
public Startup(IConfiguration configuration) => Configuration = configuration;
public IConfiguration Configuration { get; }
}
Now I want to use this configuration file in ConfigureServices
method of Startup
and use extension method AddDbContext
to register my ApplicationDbContext
to use the SQL database (SportStore
) I assigned in the configuration file :
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(
options => options.UseSqlServer(***);
}
My question is that what should I pass into the UseSqlServer
method as parameters (***) so it can connect context to the SQL Server database using my supplied configuration property?
services.AddDbContext<BloggingContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("ConnectionStrings")));
for more details see -> link
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With