Skip to content

Commit

Permalink
Creating an EF Core DbContext and a service implementation that works…
Browse files Browse the repository at this point in the history
… with the context
  • Loading branch information
gorohoroh committed Jul 6, 2018
1 parent 6601b7d commit 39e7450
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
12 changes: 12 additions & 0 deletions OdeToFoodRider/OdeToFoodRider/Data/OdeToFoodDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.EntityFrameworkCore;
using OdeToFoodRider.Models;

namespace OdeToFoodRider.Data
{
public class OdeToFoodDbContext : DbContext
{
public OdeToFoodDbContext(DbContextOptions options) : base(options) { }

public DbSet<Restaurant> Restaurants { get; set; }
}
}
34 changes: 34 additions & 0 deletions OdeToFoodRider/OdeToFoodRider/Services/SqlRestaurantData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Collections.Generic;
using System.Linq;
using OdeToFoodRider.Data;
using OdeToFoodRider.Models;

namespace OdeToFoodRider.Services
{
class SqlRestaurantData : IRestaurantData
{
private OdeToFoodDbContext _context;

public SqlRestaurantData(OdeToFoodDbContext context)
{
_context = context;
}

public Restaurant Add(Restaurant restaurant)
{
_context.Restaurants.Add(restaurant);
_context.SaveChanges();
return restaurant;
}

public Restaurant Get(int id)
{
return _context.Restaurants.FirstOrDefault(r => r.Id == id);
}

public IEnumerable<Restaurant> GetAll()
{
return _context.Restaurants.OrderBy(r => r.Name);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.EntityFrameworkCore;
using OdeToFoodVisualStudio.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace OdeToFoodVisualStudio.Data
{
public class OdeToFoodDbContext : DbContext
{
public OdeToFoodDbContext(DbContextOptions options) : base(options) {}

public DbSet<Restaurant> Restaurants { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using OdeToFoodVisualStudio.Data;
using OdeToFoodVisualStudio.Models;

namespace OdeToFoodVisualStudio.Services
{
public class SqlRestaurantData : IRestaurantData
{
private OdeToFoodDbContext _context;

public SqlRestaurantData(OdeToFoodDbContext context)
{
_context = context;
}

public Restaurant Add(Restaurant restaurant)
{
_context.Restaurants.Add(restaurant);
_context.SaveChanges();
return restaurant;
}

public Restaurant Get(int id)
{
return _context.Restaurants.FirstOrDefault(r => r.Id == id);
}

// Possible performance issues if returning an IEnumerable with large data sets - IQueryable is performance-safer
public IEnumerable<Restaurant> GetAll()
{
return _context.Restaurants.OrderBy(r => r.Name);
}
}
}

0 comments on commit 39e7450

Please sign in to comment.