Ejemplo n.º 1
0
        /// <summary>
        /// 批量修改满足满足条件的实体
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="whereLambda"></param>
        /// <param name="propertyNames"></param>
        /// <returns></returns>
        public virtual int Update(T entity, Expression <Func <T, bool> > whereLambda, params string[] propertyNames)
        {
            using (db = new OracleEntities(connString))
            {
                List <T> list = db.Set <T>().Where(whereLambda).ToList();

                Type t = typeof(T);

                List <PropertyInfo> propertyInfos = t.GetProperties(BindingFlags.Instance | BindingFlags.Public).ToList();

                Dictionary <string, PropertyInfo> dicPropertys = new Dictionary <string, PropertyInfo>();

                propertyInfos.ForEach(p =>
                {
                    if (propertyNames.Contains(p.Name))
                    {
                        dicPropertys.Add(p.Name, p);
                    }
                });

                if (propertyNames != null && propertyNames.Length > 0)
                {
                    foreach (var propertyName in propertyNames)
                    {
                        if (dicPropertys.ContainsKey(propertyName))
                        {
                            PropertyInfo propInfo = dicPropertys[propertyName];
                            object       newValue = propInfo.GetValue(entity, null);
                            foreach (T item in list)
                            {
                                propInfo.SetValue(item, newValue, null);
                                db.Entry(item).Property(propInfo.Name).IsModified = true;
                            }
                        }
                    }
                }
                else
                {
                    foreach (var property in propertyInfos)
                    {
                        if (property.Name.ToLower() != "id")
                        {
                            object newValue = property.GetValue(entity, null);
                            foreach (T item in list)
                            {
                                property.SetValue(item, newValue, null);
                                db.Entry(item).Property(property.Name).IsModified = true;
                            }
                        }
                    }
                }

                db.Configuration.ValidateOnSaveEnabled = false;



                return(db.SaveChanges());
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 修改一条数据
        /// </summary>
        /// <param name="entity">要修改的实体</param>
        /// <param name="propNames">需要修改的属性的属性名,不填则代表所有属性</param>
        /// <returns>受影响的行数</returns>
        public virtual int Update(T entity, params string[] propNames)
        {
            using (db = new OracleEntities(connString))
            {
                var entry = db.Entry(entity);

                if (propNames != null && propNames.Length > 0)
                {
                    entry.State = System.Data.Entity.EntityState.Unchanged;

                    foreach (string propName in propNames)
                    {
                        if (propName.ToLower() != "id")
                        {
                            entry.Property(propName).IsModified = true;
                        }
                    }
                }
                else
                {
                    entry.State = System.Data.Entity.EntityState.Modified;
                }

                db.Configuration.ValidateOnSaveEnabled = false;
                return(db.SaveChanges());
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 删除一条数据
        /// </summary>
        /// <param name="entity">需要删除的实体</param>
        /// <returns>受影响的行数</returns>
        public virtual int Delete(T entity)
        {
            using (db = new OracleEntities(connString))
            {
                db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;

                return(db.SaveChanges());
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 删除满足条件的所有实体
        /// </summary>
        /// <param name="whereLambda">条件</param>
        /// <returns>受影响的行数</returns>
        public virtual int Delete(Expression <Func <T, bool> > whereLambda)
        {
            using (db = new OracleEntities(connString))
            {
                List <T> list = db.Set <T>().Where(whereLambda).ToList();

                list.ForEach(t =>
                {
                    db.Entry(t).State = System.Data.Entity.EntityState.Deleted;
                });

                return(db.SaveChanges());
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 通过主键删除一条数据
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>受影响的行数</returns>
        public virtual int Delete(object id)
        {
            using (db = new OracleEntities(connString))
            {
                try
                {
                    var entity = db.Set <T>().Find(id);
                    if (entity != null)
                    {
                        db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;

                        return(db.SaveChanges());
                    }
                    else
                    {
                        return(0);
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
        }