/// <summary> /// 修改实 /// </summary> /// <param name="entity">修改对象</param> /// <param name="pNas">要修改的属性名称集体</param> /// <returns></returns> public int UpdateEntity(T entity, params string[] pNas) { try { using (DbContext _dbx = new Model.Entities()) { _dbx.Entry <T>(entity).State = EntityState.Detached;//oracle数据库没这一句不能修改 DbEntityEntry upEntity = _dbx.Entry <T>(entity); upEntity.State = System.Data.EntityState.Unchanged; foreach (string pNa in pNas) { upEntity.Property(pNa).IsModified = true; } return(_dbx.SaveChanges()); } } catch (Exception ex) { throw ex; } }
/// <summary> /// 新增实体 /// </summary> /// <param name="entity"></param> /// <returns></returns> public int AddEntity(T entity) { try { using (DbContext _dbx = new Model.Entities()) { _dbx.Set <T>().Add(entity); //_dbx.Configuration.ValidateOnSaveEnabled = false; int count = _dbx.SaveChanges(); //_dbx.Configuration.ValidateOnSaveEnabled = true; return(count); } //DbSet<T> dst = _db.Set<T>(); //dst.Add(entity); //return _db.SaveChanges(); } catch (Exception ex) { throw ex; } }