Example #1
0
        internal Delete(XContext context, T entity)
            : this(context)
        {
            if (entity == null)
            {
                throw Error.ArgumentNullException(nameof(entity));
            }

            var condition = MapperConfig.GetKeysExpression <T>(entity);
            if (condition != null)
            {
                this.Where(condition);
            }
        }
Example #2
0
        /// <summary>
        /// 根据主键值获取实体对象
        /// </summary>
        /// <typeparam name="T">查询结果实体类型</typeparam>
        /// <param name="keys">主键值</param>
        /// <returns>返回主键等于主键值的实体对象,没有返回null</returns>
        public T GetByKey <T>(params object[] keys)
        {
            if (keys.IsNullOrEmpty())
            {
                return(default(T));
            }

            var keyCondition = MapperConfig.GetKeysExpression <T>(keys);

            if (keyCondition != null)
            {
                return(GetFirstOrDefault(keyCondition));
            }

            return(default(T));
            //var keyMeta = MapperConfig.GetKeyMetas<T>();
            //if (keyMeta == null)
            //{
            //    throw Error.Exception("没有为模型" + typeof(T).FullName + "指定主键。");
            //}

            //if (keys.Length != keyMeta.Length)
            //{
            //    return default(T);
            //}

            //LambdaExpression body = null;
            //ParameterExpression parameter = null;
            //for (var i = 0; i < keyMeta.Length; i++)
            //{
            //    var meta = keyMeta[i];
            //    if (meta.Expression is LambdaExpression exp)
            //    {
            //        parameter = exp.Parameters.FirstOrDefault();
            //        var keyExp = Expression.Constant(keys[i]);
            //        var mem = exp.Body.ChangeType(keyExp.Type);
            //        var condition = Expression.Equal(keyExp, mem);
            //        var innerLambda = Expression.Lambda(condition, parameter);

            //        body = body.AndAlso(innerLambda);
            //    }
            //}

            //if (body == null)
            //{
            //    return default(T);
            //}
            //var lambda = Expression.Lambda<Func<T, bool>>(body.Body, parameter);
            //return GetFirstOrDefault(lambda);
        }
Example #3
0
        internal Delete(XContext context, params object[] keys)
            : this(context)
        {
            if (keys.IsNullOrEmpty())
            {
                throw Error.ArgumentNullException(nameof(keys));
            }

            var condition = MapperConfig.GetKeysExpression <T>(keys);
            if (condition != null)
            {
                this.Where(condition);
            }
        }
Example #4
0
        private void AddConditionByKey(T oldEntity)
        {
            var condition = MapperConfig.GetKeysExpression <T>(oldEntity);

            if (condition != null)
            {
                this.Where(condition);
                this.hasKeyWhere = true;
            }
            //if (exp != null)
            //{
            //    var val = exp.Compile().DynamicInvoke(oldEntity);
            //    var keyExp = Expression.Constant(val);
            //    var mem = exp.Body.ChangeType(keyExp.Type);
            //    var newExp = Expression.Equal(mem, keyExp);
            //    var lambda = Expression.Lambda<Func<T, bool>>(newExp, exp.Parameters);
            //    this.Where(lambda);
            //    this.hasKeyWhere = true;
            //}
            //else
            //{
            //    throw Error.Exception("没有为类型 " + tableMeta.Type.Name + " 指定主键。");
            //}
        }