-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creating an EF Core DbContext and a service implementation that works…
… with the context
- Loading branch information
Showing
4 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
34
OdeToFoodRider/OdeToFoodRider/Services/SqlRestaurantData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
OdeToFoodVisualStudio/OdeToFoodVisualStudio/Data/OdeToFoodDbContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
OdeToFoodVisualStudio/OdeToFoodVisualStudio/Services/SqlRestaurantData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |