Exemplo n.º 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);
                }
            }
        }
Exemplo n.º 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;
         }
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// 根据条件查找
        /// </summary>
        /// <param cookieName="exp">lambda查询条件where</param>
        /// <returns></returns>
        public virtual T GetEntity(Func <T, bool> exp)
        {
            using (KS_StuInfoEntities Entities = new KS_StuInfoEntities())
            {
                var entity = Entities.Set <T>().Where(exp).SingleOrDefault();
                if (entity != null)
                {
                    Entities.Entry <T>(entity).State = System.Data.Entity.EntityState.Detached;
                }

                return(entity);
            }
        }
Exemplo n.º 4
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);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// 根据条件查找
        /// </summary>
        /// <param cookieName="CommandText">Sql语句</param>
        /// <param cookieName="objParams">可变参数</param>
        /// <returns></returns>
        public virtual T GetEntity(string CommandText)
        {
            using (KS_StuInfoEntities Entities = new KS_StuInfoEntities())
            {
                var entity = Entities.Database.SqlQuery <T>("select * from " + typeof(T).Name + " where " + CommandText)
                             .SingleOrDefault();
                if (entity != null)
                {
                    Entities.Entry <T>(entity).State = System.Data.Entity.EntityState.Detached;
                }

                return(entity);
            }
        }
Exemplo n.º 6
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;
         }
     }
 }
Exemplo n.º 7
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());
                }
            }
        }
Exemplo n.º 8
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());
                }
            }
        }
Exemplo n.º 9
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;
         }
     }
 }