Wednesday, October 3, 2018

How do I use a connection string with Entity Framework in a .NET Core application?

Your connection string, per page 178 of "ASP.NET Core 2 and Angular 5" by Valerio De Sanctis, should get referenced in a doctored-up ConfigureServices method in Startup.cs like so:

public void ConfigureServices(IServiceCollection services)
{
   services.AddMvc();
   services.AddEntityFrameworkSqlServer();
   services.AddDbContext<OurDbContext>(x =>
         x.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}

 
 

This means having a using declaration like so:

using Microsoft.EntityFrameworkCore;

 
 

You will also need a using declaration to loop in your DbContext implementation.

No comments:

Post a Comment