コード例 #1
0
 public IList <TEntity> GetAll <TEntity>() where TEntity : class
 {
     using (var context = new RedCarpetDBContext())
     {
         return(context.Set <TEntity>().ToList());
     }
 }
コード例 #2
0
 public async Task <TEntity> Find <TEntity>(int id) where TEntity : class
 {
     using (var context = new RedCarpetDBContext())
     {
         return(await context.Set <TEntity>().FindAsync(new object[] { id }));
     }
 }
コード例 #3
0
 public void Create <TEntity>(TEntity entity) where TEntity : class
 {
     using (var context = new RedCarpetDBContext())
     {
         context.Set <TEntity>().Add(entity);
         context.SaveChanges();
     }
 }
コード例 #4
0
 public void Update <TEntity>(TEntity entity) where TEntity : class
 {
     using (var context = new RedCarpetDBContext())
     {
         context.Set <TEntity>().Attach(entity);
         context.Entry(entity).State = EntityState.Modified;
         context.SaveChanges();
     }
 }
コード例 #5
0
 public void CreateList <TEntity>(IList <TEntity> entities) where TEntity : class
 {
     using (var context = new RedCarpetDBContext())
     {
         foreach (var entity in entities)
         {
             context.SaveChanges();
             context.Set <TEntity>().Add(entity);
         }
         context.SaveChanges();
     }
 }
コード例 #6
0
        public void UpdateList <TEntity>(IList <TEntity> entities) where TEntity : class
        {
            using (var context = new RedCarpetDBContext())
            {
                foreach (var entity in entities)
                {
                    context.Set <TEntity>().Attach(entity);
                    context.Entry(entity).State = EntityState.Modified;
                }

                context.SaveChanges();
            }
        }
コード例 #7
0
        public virtual async Task <TEntity> GetFirstAsync <TEntity>(
            Expression <Func <TEntity, bool> > filter = null)
            where TEntity : class
        {
            using (var context = new RedCarpetDBContext())
            {
                IQueryable <TEntity> query = context.Set <TEntity>();

                if (filter != null)
                {
                    return(await query.Where(filter).AsNoTracking().FirstOrDefaultAsync());
                }

                return(null);
            }
        }