예제 #1
0
        /// <summary>
        /// 自定义删除扩展
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="context"></param>
        /// <param name="predicate">删除条件</param>
        /// <returns></returns>
        public static bool MangoRemove <TEntity>(this DbContext context, Expression <Func <TEntity, bool> > predicate) where TEntity : class, new()
        {
            TSqlAssembledResult result = TSqlAssembled.Delete <TEntity>(predicate);

            context.Database.ExecuteSqlCommand(result.SqlStr);
            return(context.SaveChanges() > 0 ? true : false);
        }
예제 #2
0
        /// <summary>
        /// 更新语句组装
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="fields"></param>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public static TSqlAssembledResult Update <TEntity>(Expression <Func <TEntity, bool> > fields, Expression <Func <TEntity, bool> > predicate) where TEntity : class, new()
        {
            try
            {
                StringBuilder strBuilder = new StringBuilder();
                strBuilder.Append("update ");
                strBuilder.Append(typeof(TEntity).Name);
                strBuilder.Append(" set ");
                //解析需要更新的字段值
                UpdateFieldBuilder updateFieldBuilder = new UpdateFieldBuilder();
                strBuilder.Append(updateFieldBuilder.Translate(fields));
                //解析条件
                ConditionBuilder conditionBuilder = new ConditionBuilder();
                strBuilder.Append(" where ");
                strBuilder.Append(conditionBuilder.Translate(predicate));
                //处理结果返回
                TSqlAssembledResult result = new TSqlAssembledResult();
                result.SqlParameters = null;
                result.SqlStr        = strBuilder.ToString();
                return(result);
            }
            catch (Exception ex)
            {
                return(null);

                throw ex;
            }
        }
예제 #3
0
        /// <summary>
        /// 更新语句组装
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="entity"></param>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public static TSqlAssembledResult Update <TEntity>(TEntity entity, Expression <Func <TEntity, bool> > predicate) where TEntity : class, new()
        {
            try
            {
                StringBuilder strBuilder = new StringBuilder();
                strBuilder.Append("update ");
                //
                Type type = entity.GetType();
                strBuilder.Append(type.Name);
                strBuilder.Append(" set ");
                //处理实体类属性
                PropertyInfo[]      properties   = type.GetProperties();
                int                 index        = 0;
                List <SqlParameter> sqlParameter = new List <SqlParameter>();
                foreach (var property in properties)
                {
                    object value = property.GetValue(entity, null);
                    if (value != null)
                    {
                        if (index != 0)
                        {
                            strBuilder.Append(",");
                        }
                        strBuilder.Append(property.Name);
                        strBuilder.Append("=@");
                        strBuilder.Append(property.Name);

                        sqlParameter.Add(new SqlParameter(property.Name, value));
                        index++;
                    }
                }
                //编译条件
                ConditionBuilder conditionBuilder = new ConditionBuilder();
                strBuilder.Append(" where ");
                strBuilder.Append(conditionBuilder.Translate(predicate));
                //处理结果返回
                TSqlAssembledResult result = new TSqlAssembledResult();
                result.SqlParameters = sqlParameter.ToArray();
                result.SqlStr        = strBuilder.ToString();
                return(result);
            }
            catch (Exception ex)
            {
                return(null);

                throw ex;
            }
        }
예제 #4
0
 /// <summary>
 /// 删除语句组装
 /// </summary>
 /// <typeparam name="TEntity"></typeparam>
 /// <param name="predicate"></param>
 /// <returns></returns>
 public static TSqlAssembledResult Delete <TEntity>(Expression <Func <TEntity, bool> > predicate) where TEntity : class, new()
 {
     try
     {
         string tableName = typeof(TEntity).Name;
         //条件编译
         ConditionBuilder conditionBuilder = new ConditionBuilder();
         string           conditionStr     = conditionBuilder.Translate(predicate);
         StringBuilder    strBuilder       = new StringBuilder();
         strBuilder.Append("delete from ");
         strBuilder.Append(tableName);
         strBuilder.Append(" where ");
         strBuilder.Append(conditionStr);
         //处理结果返回
         TSqlAssembledResult result = new TSqlAssembledResult();
         result.SqlParameters = null;
         result.SqlStr        = strBuilder.ToString();
         return(result);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }