public int Update(TEntity entity)
        {
            int result = 0;

            try
            {
                RemoveHoldingEntityInContext(entity);
                dbcontext.Set <TEntity>().Attach(entity);
                PropertyInfo[] props = entity.GetType().GetProperties().Where(pi => !Attribute.IsDefined(pi, typeof(NotMappedAttribute))).ToArray();
                foreach (PropertyInfo prop in props)
                {
                    if (prop.GetValue(entity, null) != null)
                    {
                        if (prop.GetValue(entity, null).ToString() == "&nbsp;")
                        {
                            dbcontext.Entry(entity).Property(prop.Name).CurrentValue = null;
                        }
                        dbcontext.Entry(entity).Property(prop.Name).IsModified = true;
                    }
                }

                result = dbcontext.SaveChanges();
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
            }

            return(result);
        }
Example #2
0
 public int Update(TEntity entity)
 {
     dbcontext.Set <TEntity>().Attach(entity);
     PropertyInfo[] props = entity.GetType().GetProperties();
     foreach (PropertyInfo prop in props)
     {
         if (prop.GetValue(entity, null) != null)
         {
             if (prop.GetValue(entity, null).ToString() == "&nbsp;")
             {
                 dbcontext.Entry(entity).Property(prop.Name).CurrentValue = null;
             }
             dbcontext.Entry(entity).Property(prop.Name).IsModified = true;
         }
     }
     return(dbcontext.SaveChanges());
 }
Example #3
0
 /// <summary>
 /// update
 /// </summary>
 /// <typeparam name="TEntity"></typeparam>
 /// <param name="entity">entity</param>
 /// <returns></returns>
 public int Update <TEntity>(TEntity entity) where TEntity : class
 {
     dbcontext.Set <TEntity>().Attach(entity);
     PropertyInfo[] props = entity.GetType().GetProperties();
     foreach (PropertyInfo prop in props)
     {
         if (prop.GetValue(entity, null) != null)
         {
             if (prop.GetValue(entity, null).ToString() == "&nbsp;")
             {
                 dbcontext.Entry(entity).Property(prop.Name).CurrentValue = null;
             }
             dbcontext.Entry(entity).Property(prop.Name).IsModified = true;
         }
     }
     return(dbTransaction == null?this.Commit() : 0);
 }
Example #4
0
 public int Update(TEntity entity)
 {
     dbcontext.Set <TEntity>().Attach(entity); //附加上实体
     PropertyInfo[] props = entity.GetType().GetProperties();
     foreach (PropertyInfo prop in props)
     {
         if (prop.GetValue(entity, null) != null)                                 //如果参数值不为空
         {
             if (prop.GetValue(entity, null).ToString() == "&nbsp;")              //如果参数值转为字符串后为空格?!
             {
                 dbcontext.Entry(entity).Property(prop.Name).CurrentValue = null; //转为空引用
             }
             dbcontext.Entry(entity).Property(prop.Name).IsModified = true;       //将此属性设置为已修改
         }
     }
     return(dbcontext.SaveChanges()); //提交保存
 }
Example #5
0
 public int Delete <TEntity>(TEntity entity) where TEntity : class
 {
     context.Set <TEntity>().Attach(entity);
     context.Entry <TEntity>(entity).State = System.Data.Entity.EntityState.Deleted;
     return(trans == null?this.Commit() : 0);
 }
 public int Delete(TEntity entity)
 {
     context.Set <TEntity>().Attach(entity);
     context.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
     return(context.SaveChanges());
 }