Skip to content

Commit

Permalink
Configuring EF Core services
Browse files Browse the repository at this point in the history
  • Loading branch information
gorohoroh committed Jul 6, 2018
1 parent 39e7450 commit 16eb1ab
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
13 changes: 12 additions & 1 deletion OdeToFoodRider/OdeToFoodRider/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,32 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using OdeToFoodRider.Data;
using OdeToFoodRider.Services;

namespace OdeToFoodRider
{
public class Startup
{
private IConfiguration _configuration;

public Startup(IConfiguration configuration)
{
_configuration = configuration;
}

// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IGreeter, Greeter>();
services.AddSingleton<IRestaurantData, InMemoryRestaurantData>();
services.AddDbContext<OdeToFoodDbContext>(options =>
options.UseSqlServer(_configuration.GetConnectionString("OdeToFood")));
services.AddScoped<IRestaurantData, SqlRestaurantData>();
services.AddMvc();

}
Expand Down
2 changes: 1 addition & 1 deletion OdeToFoodRider/OdeToFoodRider/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"Greeting": "Hello from Rider",
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=_CHANGE_ME;Trusted_Connection=True;MultipleActiveResultSets=true"
"OdeToFood": "Server=(localdb)\\MSSQLLocalDB;Database=OdeToFood;Trusted_Connection=True;MultipleActiveResultSets=true"
}

}
12 changes: 11 additions & 1 deletion OdeToFoodVisualStudio/OdeToFoodVisualStudio/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,34 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using OdeToFoodVisualStudio.Data;
using OdeToFoodVisualStudio.Services;
using System;

namespace OdeToFoodVisualStudio
{
public class Startup
{
private IConfiguration _configuration;

public Startup(IConfiguration configuration)
{
_configuration = configuration;
}

// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
// 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
// with with parameter info
services.AddSingleton<IGreeter, Greeter>(); // singleton scope
services.AddSingleton<IRestaurantData, InMemoryRestaurantData>(); // per-HTTP-request lifeime
services.AddDbContext<OdeToFoodDbContext>(options => options.UseSqlServer(_configuration.GetConnectionString("OdeToFood")));
services.AddScoped<IRestaurantData, SqlRestaurantData>(); // per-HTTP-request lifeime
services.AddMvc();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"Greeting": "Hello from Visual Studio",
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=_CHANGE_ME;Trusted_Connection=True;MultipleActiveResultSets=true"
"OdeToFood": "Server=(localdb)\\MSSQLLocalDB;Database=OdeToFood;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}

0 comments on commit 16eb1ab

Please sign in to comment.