예제 #1
0
        public virtual bool UpdateBatch(List <T> entitys)
        {
            using (KS_StuInfoEntities Entities = new KS_StuInfoEntities())
            {
                try
                {
                    foreach (var entity in entitys)
                    {
                        Entities.Entry <T>(entity).State = System.Data.Entity.EntityState.Modified;
                        var result1 = Entities.SaveChanges();
                        if (result1 <= 0)
                        {
                            Entities.ChangeTracker.DetectChanges();
                            return(false);
                        }
                    }

                    return(true);
                }
                catch (DbEntityValidationException)
                {
                    //throw ee;
                    return(false);
                }
                catch (Exception)
                {
                    //LogHelper.WriteLog("", e);
                    //throw e;
                    return(false);
                }
            }
        }
예제 #2
0
 /// <summary>
 /// 插入Entity
 /// </summary>
 /// <param cookieName="model"></param>
 /// <returns></returns>
 public virtual T InsertReturn(T entity)
 {
     using (KS_StuInfoEntities Entities = new KS_StuInfoEntities())
     {
         try
         {
             Entities.Entry <T>(entity).State = System.Data.Entity.EntityState.Added;
             if (Entities.SaveChanges() > 0)
             {
                 return(entity);
             }
             else
             {
                 return(null);
             }
         }
         catch (DbEntityValidationException dbEx)
         {
             //LogHelper.WriteLog("", dbEx);
             System.Diagnostics.Debug.WriteLine(dbEx.Message);
             throw dbEx;
         }
         catch (Exception ee)
         {
             //LogHelper.WriteLog("", ee);
             System.Diagnostics.Debug.WriteLine(ee.Message);
             throw ee;
         }
     }
 }
예제 #3
0
        /// <summary>
        /// 删除Entity
        /// </summary>
        /// <param cookieName="entity"></param>
        /// <returns></returns>
        public virtual bool Delete(T entity)
        {
            using (KS_StuInfoEntities Entities = new KS_StuInfoEntities())
            {
                if (entity != null)
                {
                    Entities.Entry <T>(entity).State = System.Data.Entity.EntityState.Deleted;
                    return(Entities.SaveChanges() > 0);
                }

                return(false);
            }
        }
예제 #4
0
 /// <summary>
 /// 更新Entity(注意这里使用的傻瓜式更新,可能性能略低)
 /// </summary>
 /// <param cookieName="model"></param>
 /// <returns></returns>
 public virtual bool Update(T entity)
 {
     using (KS_StuInfoEntities Entities = new KS_StuInfoEntities())
     {
         try
         {
             Entities.Entry <T>(entity).State = System.Data.Entity.EntityState.Modified;
             var result = Entities.SaveChanges();
             return(result > 0);
         }
         catch (DbEntityValidationException ee)
         {
             throw ee;
         }
         catch (Exception e)
         {
             throw e;
         }
     }
 }
예제 #5
0
        /// <summary>
        /// 批量插入
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public bool InsertBatch(IEnumerable <T> entities)
        {
            using (KS_StuInfoEntities Entities = new KS_StuInfoEntities())
            {
                try
                {
                    foreach (var entity in entities)
                    {
                        Entities.Entry <T>(entity).State = System.Data.Entity.EntityState.Added;
                    }

                    return(Entities.SaveChanges() > 0);
                }

                catch (DbEntityValidationException ee)
                {
                    //LogHelper.WriteLog("", ee);
                    throw new Exception(ee.EntityValidationErrors.ElementAt(0).ValidationErrors.ElementAt(0)
                                        .ErrorMessage.ToString());
                }
            }
        }
예제 #6
0
        /// <summary>
        /// 插入Entity
        /// </summary>
        /// <param cookieName="model"></param>
        /// <returns></returns>
        public virtual bool Insert(T entity)
        {
            using (KS_StuInfoEntities Entities = new KS_StuInfoEntities())
            {
                try
                {
                    //var obj = Entities.Set<T>();
                    //obj.Attach(entity);
                    Entities.Entry <T>(entity).State = System.Data.Entity.EntityState.Added;
                    return(Entities.SaveChanges() > 0);
                }

                catch (DbEntityValidationException ee)
                {
                    //LogHelper.WriteLog("", ee);
                    System.Diagnostics.Debug.WriteLine(ee.EntityValidationErrors.ElementAt(0).ValidationErrors
                                                       .ElementAt(0).ErrorMessage);
                    throw new Exception(ee.EntityValidationErrors.ElementAt(0).ValidationErrors.ElementAt(0)
                                        .ErrorMessage.ToString());
                }
            }
        }
예제 #7
0
 /// <summary>
 /// 更新Entity(注意这里使用的傻瓜式更新,可能性能略低)
 /// </summary>
 /// <param cookieName="model"></param>
 /// <returns></returns>
 public virtual T UpdateReturn(T entity)
 {
     using (KS_StuInfoEntities Entities = new KS_StuInfoEntities())
     {
         Entities.Entry <T>(entity).State = System.Data.Entity.EntityState.Modified;
         try
         {
             if (Entities.SaveChanges() > 0)
             {
                 return(entity);
             }
             else
             {
                 return(null);
             }
         }
         catch (Exception e)
         {
             //LogHelper.WriteLog("", e);
             throw e;
         }
     }
 }