/// <summary>
 /// 添加
 /// </summary>
 /// <param name="entity">数据实体</param>
 /// <returns>实体</returns>
 public T InsertReturnEntity <T>(T entity) where T : class
 {
     using (CommonEntities db = new CommonEntities())
     {
         db.Entry <T>(entity).State = System.Data.Entity.EntityState.Added;
         db.SaveChanges();
         return(entity);
     }
 }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">实体数据</param>
        /// <returns>布尔值(修改结果)</returns>
        public bool Update <T>(T entity) where T : class
        {
            bool result = false;

            using (CommonEntities db = new CommonEntities())
            {
                db.Entry <T>(entity).State = System.Data.Entity.EntityState.Modified;
                try
                {
                    db.SaveChanges();
                    result = true;
                }
                catch (Exception)
                {
                    result = false;
                }
            }
            return(result);
        }
Example #3
0
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>布尔值</returns>
        public bool Insert(T entity)
        {
            bool result = false;

            using (CommonEntities db = new CommonEntities())
            {
                db.Entry <T>(entity).State = System.Data.Entity.EntityState.Added;
                try
                {
                    db.SaveChanges();
                    result = true;
                }
                catch (Exception)
                {
                    result = false;
                }
                return(result);
            }
        }
        /// <summary>
        /// 添加集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool InsertIQueryable <T>(List <T> lstentity) where T : class
        {
            bool result = false;

            using (CommonEntities db = new CommonEntities())
            {
                foreach (var item in lstentity)
                {
                    db.Entry <T>(item).State = System.Data.Entity.EntityState.Added;
                }
                try
                {
                    db.SaveChanges();
                    result = true;
                }
                catch (Exception)
                {
                    result = false;
                }
                return(result);
            }
        }
Example #5
0
        /// <summary>
        /// 删除集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool DeleteIQueryable(List <T> entity)
        {
            bool result = false;

            using (CommonEntities db = new CommonEntities())
            {
                foreach (var item in entity)
                {
                    db.Entry <T>(item).State = System.Data.Entity.EntityState.Deleted;
                }
                try
                {
                    db.SaveChanges();
                    result = true;
                }
                catch (Exception)
                {
                    result = false;
                }
                return(result);
            }
        }