Пример #1
0
        public async Task <TEntity> Delete(long id)
        {
            var entity = await _context.Set <TEntity>().FindAsync(id);

            if (entity == null)
            {
                throw new EntityNotFoundException($"Cannot find object of type \"{typeof(TEntity)}\" with id {id}", $"Cannot delete {typeof(TEntity)}. Object is missing.");
            }

            _context.Set <TEntity>().Remove(entity);
            await _context.SaveChangesAsync();

            return(entity);
        }
 public static T Get <T>(Expression <Func <T, bool> > expression) where T : BaseEntity
 {
     using (var context = new ArticleDbContext())
     {
         return(context.Set <T>().Where(expression).FirstOrDefault());
     }
 }
 public static T Get <T>(int id) where T : BaseEntity
 {
     using (var context = new ArticleDbContext())
     {
         return(context.Set <T>().Where(x => x.Id == id).FirstOrDefault());
     }
 }
 public static List <T> GetList <T>(Expression <Func <T, bool> > expression) where T : BaseEntity, new()
 {
     using (var context = new ArticleDbContext())
     {
         return(context.Set <T>().Where(expression).ToList());
     }
 }
 public static BaseEntity Update(BaseEntity entity)
 {
     using (var context = new ArticleDbContext())
     {
         context.Set <BaseEntity>().Update(entity);
         context.SaveChanges();
         return(entity);
     }
 }