Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure UseSqlServer?

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?

like image 717
Mohsen Avatar asked Dec 07 '22 15:12

Mohsen


1 Answers

 services.AddDbContext<BloggingContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("ConnectionStrings")));

for more details see -> link

like image 101
Irakli Gabisonia Avatar answered Dec 15 '22 21:12

Irakli Gabisonia