Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using BusinessLayer;
- using Microsoft.EntityFrameworkCore;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace DataLayer
- {
- public class UserContext : IDB<User,int>
- {
- GamingDbContext _context;
- public UserContext(GamingDbContext context)
- {
- _context = context;
- }
- public void Create(User item)
- {
- try
- {
- _context.Users.Add(item);
- _context.SaveChanges();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public User Read(int key, bool noTracking = false, bool useNavigationProperties = false)
- {
- try
- {
- IQueryable<User> query = _context.Users;
- if (noTracking)
- {
- query = query.AsNoTrackingWithIdentityResolution();
- }
- if (useNavigationProperties)
- {
- query = query.Include(u => u.Games).Include(u => u.Friends);
- }
- return query.SingleOrDefault(u => u.ID == key);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public IEnumerable<User> Read(int skip, int take, bool useNavigationProperties = false)
- {
- try
- {
- IQueryable<User> query = _context.Users.AsNoTrackingWithIdentityResolution();
- if (useNavigationProperties)
- {
- query = query.Include(u => u.Games).Include(u => u.Friends);
- }
- return query.Skip(skip).Take(take).ToList();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public IEnumerable<User> ReadAll(bool useNavigationProperties = false)
- {
- try
- {
- IQueryable<User> query = _context.Users.AsNoTracking();
- if (useNavigationProperties)
- {
- query = query.Include(u => u.Games).Include(u => u.Friends);
- }
- return query.ToList();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public void Update(User item, bool useNavigationProperties = false)
- {
- try
- {
- User userFromDB = Read(item.ID);
- _context.Entry(userFromDB).CurrentValues.SetValues(item);
- _context.SaveChanges();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public void Delete(int key)
- {
- try
- {
- _context.Users.Remove(Read(key));
- _context.SaveChanges();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement