Configuring the services

In the Startup.cs class, in the ConfigureServices method, add the DbContext service with the following code:

string connection = Configuration.GetConnectionString("LocalDBConnection"); 
services.AddDbContext<ResearchContext>(options => options.UseSqlServer(connection)); 

As you can see, we set a connection string variable from the configuration, and then pass that in as an options parameter for SqlServer for our DbContext.

But hold on. Where does LocalDBConnection come from? We haven't set anything in our configuration. Not yet, anyway. Let's get that done now.

Open the appsettings.json file in the root of the project:

By default, you should see a logging entry. Add your ConnectionStrings section after the Logging ...

Get C# 7 and .NET Core 2.0 Blueprints now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.