Exemple #1
0
        /// <summary> 更新对象 </summary>
        /// <param name="item">要更新的对象</param>
        public virtual void Modify(TEntity item)
        {
            if (item != (TEntity)null)
            {
                using (var rowsContext = new SGDCEntities())
                {
                    rowsContext.Entry <TEntity>(item).State = System.Data.EntityState.Modified;

                    // rowsContext.SaveChanges();
                    try
                    {
                        rowsContext.SaveChanges();
                    }
                    catch (DbEntityValidationException ex)
                    {
                        foreach (var entityValidationErrors in ex.EntityValidationErrors)
                        {
                            foreach (var validationError in entityValidationErrors.ValidationErrors)
                            {
                                String Response = "Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage;
                                string s        = "";
                            }
                        }
                    }
                }
            }
        }
Exemple #2
0
        /// <summary> 删除对象 </summary>
        /// <param name="item">要删除的对象</param>
        public virtual void Remove(TEntity item)
        {
            if (item != null)
            {
                using (var rowsContext = new SGDCEntities())
                {
                    rowsContext.Entry <TEntity>(item).State = System.Data.EntityState.Deleted;

                    rowsContext.SaveChanges();
                }
            }
        }
Exemple #3
0
        /// <summary> 批量新增,完成后返回保存的记录条数 </summary>
        /// <param name="items">要新增的对象List</param>
        /// <returns>保存的记录条数</returns>
        public virtual int BatchAdd(List <TEntity> items)
        {
            using (var rowsContext = new SGDCEntities())
            {
                foreach (var item in items)
                {
                    rowsContext.Set <TEntity>().Add(item);
                }

                return(rowsContext.SaveChanges());
            }
        }
Exemple #4
0
        /// <summary> 删除对象 </summary>
        /// <param name="keyValues">对象的主键值</param>
        public virtual void Remove(params object[] keyValues)
        {
            using (var rowsContext = new SGDCEntities())
            {
                var entity = rowsContext.Set <TEntity>().Find(keyValues);

                if (entity != null)
                {
                    rowsContext.Set <TEntity>().Remove(entity);

                    rowsContext.SaveChanges();
                }
            }
        }
Exemple #5
0
        /// <summary> 新增对象,完成后返回保存的记录条数 </summary>
        /// <param name="item">要新增的对象</param>
        /// <returns>保存的记录条数</returns>
        public virtual int Add2(TEntity item)
        {
            if (item != (TEntity)null)
            {
                using (var rowsContext = new SGDCEntities())
                {
                    rowsContext.Set <TEntity>().Add(item);

                    return(rowsContext.SaveChanges());
                }
            }

            return(0);
        }