Ejemplo n.º 1
0
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Insert(T model, string incrementFieldName = null, string sameValuePropertyName = null)
        {
            if (!string.IsNullOrEmpty(sameValuePropertyName))
            {
                PropertyInfo property = model.GetType().GetProperty(sameValuePropertyName);
                string       check    = new ConditionHelper().And(sameValuePropertyName, property.GetValue(model, null), CompareType.Equal).ToString();

                DynamicParameters p = new DynamicParameters();
                p.Add($"@{sameValuePropertyName}", property.GetValue(model, null));

                if (_provider.GetItem <T>(check, p) != null)
                {
                    throw new SameValueException();
                }
            }
            List <string> fieldsBuilder = new List <string>();
            List <string> valuesBuilder = new List <string>();

            if (string.IsNullOrEmpty(incrementFieldName))
            {
                Type           type      = typeof(T);
                PropertyInfo[] propertys = type.GetProperties();

                foreach (var property in propertys)
                {
                    fieldsBuilder.Add($"{property.Name}");
                    valuesBuilder.Add($"@{property.Name}");
                }
            }
            else
            {
                Type           type      = typeof(T);
                PropertyInfo[] propertys = type.GetProperties();
                foreach (var property in propertys)
                {
                    if (property.Name != incrementFieldName)
                    {
                        fieldsBuilder.Add($"{property.Name}");
                        valuesBuilder.Add($"@{property.Name}");
                    }
                }
            }
            return(_provider.Insert(string.Join(",", fieldsBuilder), string.Join(",", valuesBuilder), model));
        }