Skip to content

Commit 16eb1ab

Browse files
committed
Configuring EF Core services
1 parent 39e7450 commit 16eb1ab

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

OdeToFoodRider/OdeToFoodRider/Startup.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,32 @@
66
using Microsoft.AspNetCore.Hosting;
77
using Microsoft.AspNetCore.Http;
88
using Microsoft.AspNetCore.Routing;
9+
using Microsoft.EntityFrameworkCore;
910
using Microsoft.Extensions.Configuration;
1011
using Microsoft.Extensions.DependencyInjection;
1112
using Microsoft.Extensions.Logging;
13+
using OdeToFoodRider.Data;
1214
using OdeToFoodRider.Services;
1315

1416
namespace OdeToFoodRider
1517
{
1618
public class Startup
1719
{
20+
private IConfiguration _configuration;
21+
22+
public Startup(IConfiguration configuration)
23+
{
24+
_configuration = configuration;
25+
}
26+
1827
// This method gets called by the runtime. Use this method to add services to the container.
1928
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
2029
public void ConfigureServices(IServiceCollection services)
2130
{
2231
services.AddSingleton<IGreeter, Greeter>();
23-
services.AddSingleton<IRestaurantData, InMemoryRestaurantData>();
32+
services.AddDbContext<OdeToFoodDbContext>(options =>
33+
options.UseSqlServer(_configuration.GetConnectionString("OdeToFood")));
34+
services.AddScoped<IRestaurantData, SqlRestaurantData>();
2435
services.AddMvc();
2536

2637
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"Greeting": "Hello from Rider",
33
"ConnectionStrings": {
4-
"DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=_CHANGE_ME;Trusted_Connection=True;MultipleActiveResultSets=true"
4+
"OdeToFood": "Server=(localdb)\\MSSQLLocalDB;Database=OdeToFood;Trusted_Connection=True;MultipleActiveResultSets=true"
55
}
66

77
}

OdeToFoodVisualStudio/OdeToFoodVisualStudio/Startup.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,34 @@
22
using Microsoft.AspNetCore.Hosting;
33
using Microsoft.AspNetCore.Http;
44
using Microsoft.AspNetCore.Routing;
5+
using Microsoft.EntityFrameworkCore;
56
using Microsoft.Extensions.Configuration;
67
using Microsoft.Extensions.DependencyInjection;
78
using Microsoft.Extensions.Logging;
9+
using OdeToFoodVisualStudio.Data;
810
using OdeToFoodVisualStudio.Services;
911
using System;
1012

1113
namespace OdeToFoodVisualStudio
1214
{
1315
public class Startup
1416
{
17+
private IConfiguration _configuration;
18+
19+
public Startup(IConfiguration configuration)
20+
{
21+
_configuration = configuration;
22+
}
23+
1524
// This method gets called by the runtime. Use this method to add services to the container.
1625
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
1726
public void ConfigureServices(IServiceCollection services)
1827
{
1928
// RDVS: Rider's completion works better with this generic method as it additionally adds the parentheses; additionally, VS overlaps the completion list inside the generic brackets
2029
// with with parameter info
2130
services.AddSingleton<IGreeter, Greeter>(); // singleton scope
22-
services.AddSingleton<IRestaurantData, InMemoryRestaurantData>(); // per-HTTP-request lifeime
31+
services.AddDbContext<OdeToFoodDbContext>(options => options.UseSqlServer(_configuration.GetConnectionString("OdeToFood")));
32+
services.AddScoped<IRestaurantData, SqlRestaurantData>(); // per-HTTP-request lifeime
2333
services.AddMvc();
2434
}
2535

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"Greeting": "Hello from Visual Studio",
33
"ConnectionStrings": {
4-
"DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=_CHANGE_ME;Trusted_Connection=True;MultipleActiveResultSets=true"
4+
"OdeToFood": "Server=(localdb)\\MSSQLLocalDB;Database=OdeToFood;Trusted_Connection=True;MultipleActiveResultSets=true"
55
}
66
}

0 commit comments

Comments
 (0)