Advertisement
mitakabg3

CRUD syntax

Jun 24th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.38 KB | None | 0 0
  1. using CarDealer.Database.Repository.Interfaces;
  2. using Microsoft.EntityFrameworkCore;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7.  
  8. namespace CarDealer.Database.Repository.RepositoryModels
  9. {
  10.     public class BaseRepositoryModel<TEntity> : IBaseRepository<TEntity> where TEntity : class
  11.     {
  12.         private DbSet<TEntity> Querable;
  13.  
  14.         protected CarDealerDbContext DBContext;
  15.  
  16.         public BaseRepositoryModel(CarDealerDbContext context)
  17.         {
  18.             Querable = context.Set<TEntity>();
  19.             DBContext = context;
  20.         }
  21.  
  22.         public IQueryable<TEntity> All()
  23.         {
  24.             return Querable;
  25.         }
  26.  
  27.         public IQueryable<TEntity> AllIncluding(params System.Linq.Expressions.Expression<Func<TEntity, object>>[] includeProperties)
  28.         {
  29.             return Querable.Include(x => includeProperties);
  30.         }
  31.  
  32.         public TEntity GetById(int id)
  33.         {
  34.             return Querable.Find(id);
  35.         }
  36.  
  37.         public void AddRange(IEnumerable<TEntity> entityList, bool saveChanges = true)
  38.         {
  39.             Querable.AddRange(entityList);
  40.             if (saveChanges) DBContext.SaveChanges();
  41.         }
  42.  
  43.         public void Delete(TEntity entity)
  44.         {
  45.             Querable.Remove(entity);
  46.             DBContext.SaveChanges();
  47.         }
  48.  
  49.         public void DeleteRange(List<int> ids)
  50.         {
  51.             if (ids != null && ids.Any())
  52.             {
  53.                 var entitiesToDelete = new List<TEntity>();
  54.  
  55.                 ids.ForEach(x => entitiesToDelete.Add(Querable.Find(x)));
  56.                 if (entitiesToDelete.Any())
  57.                 {
  58.                     Querable.RemoveRange(entitiesToDelete);
  59.                     DBContext.SaveChanges();
  60.                 }
  61.             }
  62.         }
  63.  
  64.         public TEntity Add(TEntity entity, bool saveChanges = true)
  65.         {
  66.             Querable.Add(entity);
  67.             if (saveChanges) DBContext.SaveChanges();
  68.             return entity;
  69.         }
  70.  
  71.         public int SaveChanges()
  72.         {
  73.             return DBContext.SaveChanges();
  74.         }
  75.  
  76.         public void DeleteRange(IEnumerable<TEntity> itemsToRemove)
  77.         {
  78.             if (itemsToRemove.Any())
  79.             {
  80.                 Querable.RemoveRange(itemsToRemove);
  81.                 DBContext.SaveChanges();
  82.             }
  83.         }
  84.  
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement