示例#1
0
 public void Delete(int key)
 {
     using (var dbContext = new AARPDbContext())
     {
         var entity = dbContext.Set <TEntity>().Find(key);
         if (entity != null)
         {
             dbContext.Set <TEntity>().Remove(entity);
             dbContext.SaveChanges();
         }
     }
 }
示例#2
0
        public TEntity[] AddRange(TEntity[] entities)
        {
            using (var dbContext = new AARPDbContext())
            {
                try
                {
                    dbContext.Set <TEntity>().AddRange(entities);
                    dbContext.SaveChanges();
                }
                catch (System.Data.Entity.Validation.DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                          eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                              ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    throw;
                }
            }

            return(entities);
        }
示例#3
0
 public TEntity Get(int key)
 {
     using (var dbContext = new AARPDbContext())
     {
         return(dbContext.Set <TEntity>().Find(key));
     }
 }
示例#4
0
 public TEntity[] GetList(Expression <Func <TEntity, bool> > predicate)
 {
     using (var dbContext = new AARPDbContext())
     {
         return(dbContext.Set <TEntity>().Where(predicate).ToArray());
     }
 }
示例#5
0
 public TEntity[] GetList()
 {
     using (var dbContext = new AARPDbContext())
     {
         return(dbContext.Set <TEntity>().ToArray());
     }
 }
示例#6
0
        public TEntity Add(TEntity entity)
        {
            using (var dbContext = new AARPDbContext())
            {
                dbContext.Set <TEntity>().Add(entity);
                dbContext.SaveChanges();
            }

            return(entity);
        }
示例#7
0
        public TEntity Update(TEntity entity)
        {
            using (var dbContext = new AARPDbContext())
            {
                dbContext.Set <TEntity>().Attach(entity);
                dbContext.Entry(entity).State = EntityState.Modified;
                dbContext.SaveChanges();
            }

            return(entity);
        }