/// <summary>
 /// Initializes a new instance of the <see cref="ColumnDefinition"/> class.
 /// </summary>
 /// <param name="name"></param>
 /// <param name="type"></param>
 /// <param name="nullable"></param>
 /// <param name="length"></param>
 /// <param name="scale"></param>
 /// <param name="precision"></param>
 public ColumnDefinition(string name, DbType? type, bool? nullable, int? length, int? scale, int? precision) : 
     this(name)
 {
     this.type = type;
     this.nullable = nullable;
     this.length = length;
     this.scale = scale;
     this.precision = precision;
 }
 /// <summary>
 ///     Initializes a new instance of the <see cref="DoubleTypeMapping" /> class.
 /// </summary>
 /// <param name="storeType"> The name of the database type. </param>
 /// <param name="dbType"> The <see cref="DbType" /> to be used. </param>
 public DoubleTypeMapping(
     [NotNull] string storeType,
     DbType?dbType = null)
     : base(storeType, typeof(double), dbType)
 {
 }
Example #3
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="UShortTypeMapping" /> class.
 /// </summary>
 /// <param name="storeType"> The name of the database type. </param>
 /// <param name="dbType"> The <see cref="DbType" /> to be used. </param>
 public UShortTypeMapping(
     [NotNull] string storeType,
     DbType?dbType = null)
     : this(storeType, null, null, null, dbType)
 {
 }
Example #4
0
        public QueryColumnE(QueryColumnSourceBase source, string outputColumnName, string sourceColumnName, DbType?columnDbType, bool?allowNull)
            : base(source)
        {
            if (string.IsNullOrEmpty(outputColumnName))
            {
                throw new ArgumentNullException(nameof(outputColumnName));
            }

            this.outputColumnName  = outputColumnName;
            this._sourceColumnName = sourceColumnName;
            this._columnDbType     = columnDbType;
            this._allowNull        = allowNull;
        }
Example #5
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="DateOnlyTypeMapping" /> class.
 /// </summary>
 /// <param name="storeType"> The name of the database type. </param>
 /// <param name="dbType"> The <see cref="DbType" /> to be used. </param>
 public DateOnlyTypeMapping(
     string storeType,
     DbType?dbType = null)
     : base(storeType, typeof(DateOnly), dbType)
 {
 }
Example #6
0
        /// <summary>
        /// Allows you to automatically populate a target property/field from output parameters. It actually
        /// creates an InputOutput parameter, so you can still pass data in.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="target">The object whose property/field you wish to populate.</param>
        /// <param name="expression">A MemberExpression targeting a property/field of the target (or descendant thereof.)</param>
        /// <param name="dbType"></param>
        /// <param name="size">The size to set on the parameter. Defaults to 0, or DbString.DefaultLength in case of strings.</param>
        /// <returns>The DynamicParameters instance</returns>
        public DynamicParameters Output <T>(T target, Expression <Func <T, object> > expression, DbType?dbType = null, int?size = null)
        {
            var failMessage = "Expression must be a property/field chain off of a(n) {0} instance";

            failMessage = string.Format(failMessage, typeof(T).Name);
            Action @throw = () => throw new InvalidOperationException(failMessage);

            // Is it even a MemberExpression?
            var lastMemberAccess = expression.Body as MemberExpression;

            if (lastMemberAccess == null ||
                (!(lastMemberAccess.Member is PropertyInfo) &&
                 !(lastMemberAccess.Member is FieldInfo)))
            {
                if (expression.Body.NodeType == ExpressionType.Convert &&
                    expression.Body.Type == typeof(object) &&
                    ((UnaryExpression)expression.Body).Operand is MemberExpression)
                {
                    // It's got to be unboxed
                    lastMemberAccess = (MemberExpression)((UnaryExpression)expression.Body).Operand;
                }
                else
                {
                    @throw();
                }
            }

            // Does the chain consist of MemberExpressions leading to a ParameterExpression of type T?
            MemberExpression diving = lastMemberAccess;
            // Retain a list of member names and the member expressions so we can rebuild the chain.
            List <string>           names = new List <string>();
            List <MemberExpression> chain = new List <MemberExpression>();

            do
            {
                // Insert the names in the right order so expression
                // "Post.Author.Name" becomes parameter "PostAuthorName"
                names.Insert(0, diving?.Member.Name);
                chain.Insert(0, diving);

                var constant = diving?.Expression as ParameterExpression;
                diving = diving?.Expression as MemberExpression;

                if (constant != null && constant.Type == typeof(T))
                {
                    break;
                }
                else if (diving == null ||
                         (!(diving.Member is PropertyInfo) &&
                          !(diving.Member is FieldInfo)))
                {
                    @throw();
                }
            }while (diving != null);

            var dynamicParamName = string.Concat(names.ToArray());

            // Before we get all emitty...
            var lookup = string.Join("|", names.ToArray());

            var cache  = CachedOutputSetters <T> .Cache;
            var setter = (Action <object, DynamicParameters>)cache[lookup];

            if (setter != null)
            {
                goto MAKECALLBACK;
            }

            // Come on let's build a method, let's build it, let's build it now!
            var dm = new DynamicMethod("ExpressionParam" + Guid.NewGuid().ToString(), null, new[] { typeof(object), GetType() }, true);
            var il = dm.GetILGenerator();

            il.Emit(OpCodes.Ldarg_0);              // [object]
            il.Emit(OpCodes.Castclass, typeof(T)); // [T]

            // Count - 1 to skip the last member access
            var i = 0;

            for (; i < (chain.Count - 1); i++)
            {
                var member = chain[0].Member;

                if (member is PropertyInfo)
                {
                    var get = ((PropertyInfo)member).GetGetMethod(true);
                    il.Emit(OpCodes.Callvirt, get); // [Member{i}]
                }
                else // Else it must be a field!
                {
                    il.Emit(OpCodes.Ldfld, (FieldInfo)member); // [Member{i}]
                }
            }

            var paramGetter = GetType().GetMethod("Get", new Type[] { typeof(string) }).MakeGenericMethod(lastMemberAccess.Type);

            il.Emit(OpCodes.Ldarg_1);                 // [target] [DynamicParameters]
            il.Emit(OpCodes.Ldstr, dynamicParamName); // [target] [DynamicParameters] [ParamName]
            il.Emit(OpCodes.Callvirt, paramGetter);   // [target] [value], it's already typed thanks to generic method

            // GET READY
            var lastMember = lastMemberAccess.Member;

            if (lastMember is PropertyInfo)
            {
                var set = ((PropertyInfo)lastMember).GetSetMethod(true);
                il.Emit(OpCodes.Callvirt, set); // SET
            }
            else
            {
                il.Emit(OpCodes.Stfld, (FieldInfo)lastMember); // SET
            }

            il.Emit(OpCodes.Ret); // GO

            setter = (Action <object, DynamicParameters>)dm.CreateDelegate(typeof(Action <object, DynamicParameters>));
            lock (cache)
            {
                cache[lookup] = setter;
            }

            // Queue the preparation to be fired off when adding parameters to the DbCommand
MAKECALLBACK:
            (outputCallbacks ?? (outputCallbacks = new List <Action>())).Add(() =>
            {
                // Finally, prep the parameter and attach the callback to it
                var targetMemberType = lastMemberAccess?.Type;
                int sizeToSet        = (!size.HasValue && targetMemberType == typeof(string)) ? DbString.DefaultLength : size ?? 0;

                if (parameters.TryGetValue(dynamicParamName, out ParamInfo parameter))
                {
                    parameter.ParameterDirection = parameter.AttachedParam.Direction = ParameterDirection.InputOutput;

                    if (parameter.AttachedParam.Size == 0)
                    {
                        parameter.Size = parameter.AttachedParam.Size = sizeToSet;
                    }
                }
                else
                {
                    dbType = (!dbType.HasValue)
#pragma warning disable 618
                    ? SqlMapper.LookupDbType(targetMemberType, targetMemberType?.Name, true, out SqlMapper.ITypeHandler handler)
#pragma warning restore 618
                    : dbType;

                    // CameFromTemplate property would not apply here because this new param
                    // Still needs to be added to the command
                    Add(dynamicParamName, expression.Compile().Invoke(target), null, ParameterDirection.InputOutput, sizeToSet);
                }

                parameter = parameters[dynamicParamName];
                parameter.OutputCallback = setter;
                parameter.OutputTarget   = target;
            });

            return(this);
        }
        /// <summary>
        /// Creates a new Output Parameter (can be Output, InputOutput, or ReturnValue) <br />
        /// and registers a callback action which (after command invocation) will populate back parameter output value into an instance property.
        /// </summary>
        /// <param name="name">The name of the parameter.</param>
        /// <param name="target">Target variable where output value will be set.</param>
        /// <param name="expression">Property where output value will be set. If it's InputOutput type this value will be passed.</param>
        /// <param name="dbType">The type of the parameter.</param>
        /// <param name="direction">The type of output of the parameter.</param>
        /// <param name="size">The size of the parameter.</param>
        /// <param name="precision">The precision of the parameter.</param>
        /// <param name="scale">The scale of the parameter.</param>
        public static ParameterInfo CreateOutputParameter <T, TP>(string name, T target, Expression <Func <T, TP> > expression, OutputParameterDirection direction = OutputParameterDirection.Output, DbType?dbType = null, int?size = null, byte?precision = null, byte?scale = null)
        {
            object value = null;

            // For InputOutput we send current value
            if (direction == OutputParameterDirection.InputOutput)
            {
                value = expression.Compile().Invoke(target);
            }

            ParameterInfo parameter = new ParameterInfo(name, value, dbType, (ParameterDirection)direction, size, precision, scale);

            var setter = GetSetter(expression);

            parameter.OutputCallback = new Action <object>(o =>
            {
                TP val;
                if (o is TP)
                {
                    val = (TP)o;
                }
                else
                {
                    try
                    {
                        val = (TP)Convert.ChangeType(o, typeof(TP));
                    }
                    catch (Exception ex)
                    {
                        throw new Exception($"Can't convert {parameter.Name} ({parameter.Value}) to type {typeof(TP).Name}", ex);
                    }
                }
                setter(target, val); // TP (property type) must match the return value
            });

            return(parameter);
        }
Example #8
0
 /// <summary>
 /// Reset DBType.
 /// </summary>
 public override void ResetDbType()
 {
     //type_info = NpgsqlTypesHelper.GetNativeTypeInfo(typeof(String));
     _dbType = null;
     _npgsqlDbType = null;
     Value = Value;
     ClearBind();
 }
 public KoraliumDateTimeTypeMapping(
     [NotNull] string storeType,
     DbType?dbType = null)
     : base(storeType, dbType)
 {
 }
        IDbCommand PrepareCommand(string cmdText, DbParam[] parameters, CommandType cmdType, out List <OutputParameter> outputParameters)
        {
            outputParameters = null;

            IDbCommand cmd = this._dbConnection.CreateCommand();

            cmd.CommandText    = cmdText;
            cmd.CommandType    = cmdType;
            cmd.CommandTimeout = this._commandTimeout;
            if (this.IsInTransaction)
            {
                cmd.Transaction = this._dbTransaction;
            }

            if (parameters != null)
            {
                for (int i = 0; i < parameters.Length; i++)
                {
                    DbParam param = parameters[i];
                    if (param == null)
                    {
                        continue;
                    }

                    if (param.ExplicitParameter != null)/* 如果存在创建好了的 IDbDataParameter,则直接用它。同时也忽视了 DbParam 的其他属性 */
                    {
                        cmd.Parameters.Add(param.ExplicitParameter);
                        continue;
                    }

                    IDbDataParameter parameter = cmd.CreateParameter();
                    parameter.ParameterName = param.Name;

                    Type parameterType;
                    if (param.Value == null || param.Value == DBNull.Value)
                    {
                        parameter.Value = DBNull.Value;
                        parameterType   = param.Type;
                    }
                    else
                    {
                        parameter.Value = param.Value;
                        parameterType   = param.Value.GetType();
                    }

                    if (param.Precision != null)
                    {
                        parameter.Precision = param.Precision.Value;
                    }

                    if (param.Scale != null)
                    {
                        parameter.Scale = param.Scale.Value;
                    }

                    if (param.Size != null)
                    {
                        parameter.Size = param.Size.Value;
                    }

                    if (param.DbType != null)
                    {
                        parameter.DbType = param.DbType.Value;
                    }
                    else
                    {
                        DbType?dbType = MappingTypeSystem.GetDbType(parameterType);
                        if (dbType != null)
                        {
                            parameter.DbType = dbType.Value;
                        }
                    }

                    const int defaultSizeOfStringOutputParameter = 4000;/* 当一个 string 类型输出参数未显示指定 Size 时使用的默认大小。如果有需要更大或者该值不足以满足需求,需显示指定 DbParam.Size 值 */

                    OutputParameter outputParameter = null;
                    if (param.Direction == ParamDirection.Input)
                    {
                        parameter.Direction = ParameterDirection.Input;
                    }
                    else if (param.Direction == ParamDirection.Output)
                    {
                        parameter.Direction = ParameterDirection.Output;
                        param.Value         = null;
                        if (param.Size == null && param.Type == UtilConstants.TypeOfString)
                        {
                            parameter.Size = defaultSizeOfStringOutputParameter;
                        }
                        outputParameter = new OutputParameter(param, parameter);
                    }
                    else if (param.Direction == ParamDirection.InputOutput)
                    {
                        parameter.Direction = ParameterDirection.InputOutput;
                        if (param.Size == null && param.Type == UtilConstants.TypeOfString)
                        {
                            parameter.Size = defaultSizeOfStringOutputParameter;
                        }
                        outputParameter = new OutputParameter(param, parameter);
                    }
                    else
                    {
                        throw new NotSupportedException(string.Format("ParamDirection '{0}' is not supported.", param.Direction));
                    }

                    cmd.Parameters.Add(parameter);

                    if (outputParameter != null)
                    {
                        if (outputParameters == null)
                        {
                            outputParameters = new List <OutputParameter>();
                        }
                        outputParameters.Add(outputParameter);
                    }
                }
            }

            return(cmd);
        }
Example #11
0
 public PropertyMap SetDbType(DbType?dbType)
 {
     DbType = dbType;
     return(this);
 }
 public KoraliumStringTypeMapping(string storeType, DbType?dbType = null, bool unicode = false, int?size = null) : base(storeType, dbType, unicode, size)
 {
 }
 /// <summary>
 ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
 ///     directly from your code. This API may change or be removed in future releases.
 /// </summary>
 public SqlServerTimeSpanTypeMapping([NotNull] string storeType, DbType?dbType = null)
     : this(storeType, null, null, dbType)
 {
 }
Example #14
0
 /// <summary>
 ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
 ///     directly from your code. This API may change or be removed in future releases.
 /// </summary>
 public SqliteDateTimeTypeMapping(
     [NotNull] string storeType,
     DbType?dbType = null)
     : this(storeType, null, null, null, dbType)
 {
 }
 /// <summary>
 /// 实例化
 /// </summary>
 /// <param name="storeType">存储类型</param>
 /// <param name="dbType">数据库类型</param>
 public OracleBoolTypeMapping([NotNull] string storeType, DbType?dbType = null)
     : this(new RelationalTypeMappingParameters(new CoreTypeMappingParameters(typeof(bool)), storeType, StoreTypePostfix.PrecisionAndScale, dbType))
 {
     //
 }
 /// <summary>
 /// Resets the data type associated with the parameter.
 /// </summary>
 public override void ResetDbType()
 {
     dbType = null;
     value = null;
     direction = ParameterDirection.Input;
     ParameterName = null;
 }
Example #17
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="LongTypeMapping" /> class.
 /// </summary>
 /// <param name="storeType"> The name of the database type. </param>
 /// <param name="dbType"> The <see cref="DbType" /> to be used. </param>
 public LongTypeMapping(
     [NotNull] string storeType,
     DbType?dbType = null)
     : base(storeType, typeof(long), dbType)
 {
 }
Example #18
0
 public IDbDataParameter CreateParam(string name, object value = null, ParameterDirection direction = ParameterDirection.Input, DbType?dbType = null)
 {
     return(q.CreateParam(name, value, direction, dbType));
 }
Example #19
0
 /// <summary>
 /// Конструктор
 /// </summary>
 /// <param name="dbType">Тип БД</param>
 /// <param name="length">Размерность</param>
 /// <param name="scale">Разрядность</param>
 public ColumnType(DbType dbType, int length, int scale)
 {
     this.dbType = dbType;
     this.length = length;
     this.scale = scale;
 }
        public static IDbCommand SetParameter(this IDbCommand command, string name, object value, DbType?parameterType = null)
        {
            Logger.LogTrace("Rebinding parameter '{0}' with value: {1}", name, value);
            var parameter = (IDataParameter)command.Parameters[name];

            parameter.Value = value;
            if (parameterType.HasValue)
            {
                parameter.DbType = parameterType.Value;
            }
            return(command);
        }
 /// <summary>
 /// New Parameter
 /// </summary>
 /// <param name="name">The name of the parameter.</param>
 /// <param name="value">The value of the parameter.</param>
 /// <param name="dbType">The type of the parameter.</param>
 /// <param name="direction">The in or out direction of the parameter.</param>
 /// <param name="size">The size of the parameter.</param>
 public ParameterInfo(string name, object value, DbType?dbType, ParameterDirection?direction, int?size) : this(name, value, dbType, direction, size, null, null)
 {
 }
Example #22
0
 /// <summary>
 ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
 ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
 ///     any release. You should only use it directly in your code with extreme caution and knowing that
 ///     doing so can result in application failures when updating to a new Entity Framework Core release.
 /// </summary>
 public SqliteDateTimeOffsetTypeMapping(
     [NotNull] string storeType,
     DbType?dbType = null)
     : base(storeType, dbType)
 {
 }
Example #23
0
        /// <summary>
        /// 添加参数对象,用于输入输出型参数。
        /// </summary>
        /// <param name="parameterName">参数名称。</param>
        /// <param name="sourceColumn">源列名称。</param>
        /// <param name="value">参数的值。</param>
        /// <param name="dbType">参数的值类型。</param>
        /// <param name="size">数据长度。</param>
        /// <param name="direction">参数的方向。</param>
        /// <returns>参数对象。</returns>
        private Parameter Add <T>(string parameterName, string sourceColumn, T value, DbType?dbType, int?size, ParameterDirection direction)
        {
            if (ContainsKey(parameterName))
            {
                return(null);
            }

            var parameter = new Parameter(parameterName)
            {
                SourceColumn = string.IsNullOrEmpty(sourceColumn) ? parameterName : sourceColumn
            };

            #region 计算参数的数据类型
            if (dbType != null)
            {
                parameter.DbType = (DbType)dbType;
            }
            else
            {
                parameter.DbType = typeof(T).GetDbType();
            }
            #endregion

            parameter.Value     = GetDBNullValue(value);
            parameter.Direction = direction;

            #region 计算参数的长度
            if (direction != ParameterDirection.Input)
            {
                if (size != null)
                {
                    parameter.Size = (int)size;
                }
                else if (value != null)
                {
                    parameter.Size = value.GetType().GetDbTypeSize(size);
                }
            }
            #endregion

            arrayList.Add(parameter);
            return(parameter);
        }
Example #24
0
 /// <summary>
 ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
 ///     directly from your code. This API may change or be removed in future releases.
 /// </summary>
 public SqlCeShortTypeMapping(
     [NotNull] string storeType,
     DbType?dbType = null)
     : base(storeType, dbType)
 {
 }
 /// <summary>
 ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
 ///     directly from your code. This API may change or be removed in future releases.
 /// </summary>
 public SqliteULongTypeMapping([NotNull] string storeType, DbType?dbType = null)
     : base(storeType, dbType)
 {
 }
 /// <summary>
 /// 实例化
 /// </summary>
 /// <param name="storeType">存储类型</param>
 /// <param name="dbType">数据库类型</param>
 public OracleDateTimeTypeMapping([NotNull] string storeType, [CanBeNull] DbType?dbType = null)
     : base(storeType, dbType)
 {
     //
 }
Example #27
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="DateTimeTypeMapping" /> class.
 /// </summary>
 /// <param name="storeType"> The name of the database type. </param>
 /// <param name="dbType"> The <see cref="DbType" /> to be used. </param>
 public LocalDateRelationalTypeMapping(
     string storeType,
     DbType?dbType = null)
     : base(storeType, typeof(LocalDate), dbType)
 {
 }
 /// <summary>
 /// 添加参数
 /// </summary>
 /// <param name="parameterName">参数名</param>
 /// <param name="value">参数值</param>
 /// <param name="dbType">数据库类型</param>
 /// <param name="size">长度</param>
 public void AddParameter(string parameterName, object value, DbType?dbType, int?size)
 {
     AddParameter(new DbParameterInfo(parameterName, value, dbType, size, null, scale: null));
 }
Example #29
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="BoolTypeMapping" /> class.
 /// </summary>
 /// <param name="storeType"> The name of the database type. </param>
 /// <param name="dbType"> The <see cref="DbType" /> to be used. </param>
 public BoolTypeMapping(
     [NotNull] string storeType,
     DbType?dbType = null)
     : base(storeType, typeof(bool), dbType)
 {
 }
 /// <summary>
 /// 添加参数
 /// </summary>
 /// <param name="parameterName">参数名</param>
 /// <param name="value">参数值</param>
 /// <param name="dbType">数据库类型</param>
 /// <param name="size">长度</param>
 /// <param name="direction">Dataset参数类型</param>
 /// <param name="scale">参数值的精度</param>
 public void AddParameter(string parameterName, object value, DbType?dbType, int?size, ParameterDirection?direction, byte?scale = null)
 {
     AddParameter(new DbParameterInfo(parameterName, value, dbType, size, direction, scale: scale));
 }
Example #31
0
 /// <summary>
 ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
 ///     directly from your code. This API may change or be removed in future releases.
 /// </summary>
 public MySqlTimeSpanTypeMapping([NotNull] string storeType, DbType?dbType = null)
     : base(storeType, dbType)
 {
 }
Example #32
0
 /// <summary>
 /// 创建命令参数
 /// </summary>
 /// <param name="name">参数名称</param>
 /// <param name="value">参数值</param>
 /// <param name="dbType">数据类型</param>
 /// <param name="size">参数大小</param>
 /// <param name="precision">精度</param>
 /// <param name="scale">小数位</param>
 /// <param name="direction">方向</param>
 /// <returns></returns>
 public virtual IDbDataParameter CreateParameter(string name, object value,
                                                 DbType?dbType = null, int?size = null, int?precision = null, int?scale = null, ParameterDirection?direction = null)
 {
     return(Database.CreateParameter(this.DbProviderFactory, name, value, dbType, size, precision, scale, direction));
 }
Example #33
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="parameterName"></param>
 /// <param name="parameterValue"></param>
 /// <param name="parameterDbType"></param>
 /// <param name="parameterSize"></param>
 public Parameter(string parameterName, object parameterValue, DbType? parameterDbType, int? parameterSize)
 {
     this.parameterName = parameterName;
     this.parameterValue = parameterValue;
     this.parameterDbType = parameterDbType;
     this.parameterSize = parameterSize;
 }
Example #34
0
        /// <summary>
        /// 创建命令参数
        /// </summary>
        /// <param name="name">参数名称</param>
        /// <param name="value">参数值</param>
        /// <param name="dbType">数据类型</param>
        /// <param name="size">参数大小</param>
        /// <param name="precision">精度</param>
        /// <param name="scale">小数位</param>
        /// <param name="direction">方向</param>
        /// <returns></returns>
        public static IDbDataParameter CreateParameter(DbProviderFactory dbProvider, string name, object value,
                                                       DbType?dbType = null, int?size = null, int?precision = null, int?scale = null, ParameterDirection?direction = null)
        {
            IDbDataParameter parameter = dbProvider.CreateParameter();

            parameter.ParameterName = name;
            parameter.Value         = value;

            if (dbType != null)
            {
                parameter.DbType = dbType.Value;
            }
            if (size != null && (size.Value > 0 || size.Value == -1))
            {
                parameter.Size = size.Value;
            }
            if (precision != null && precision.Value > 0)
            {
                parameter.Precision = (byte)precision.Value;
            }
            if (scale != null && scale.Value > 0)
            {
                parameter.Scale = (byte)scale.Value;
            }
            if (direction != null)
            {
                parameter.Direction = direction.Value;
            }
            else
            {
                parameter.Direction = ParameterDirection.Input;
            }

            // 补充字符串的长度
            if (value != null && value.GetType() == typeof(string) && size == null)
            {
                string s = value.ToString();
                if (dbType == null)
                {
                    parameter.DbType = DbType.String;
                }
                if (parameter.DbType == DbType.String || parameter.DbType == DbType.StringFixedLength ||
                    parameter.DbType == DbType.AnsiString || parameter.DbType == DbType.AnsiStringFixedLength)
                {
                    if (s.Length <= 256)
                    {
                        parameter.Size = 256;
                    }
                    else if (s.Length <= 512)
                    {
                        parameter.Size = 512;
                    }
                    else if (s.Length <= 1024)
                    {
                        parameter.Size = 1024;
                    }
                    else if (s.Length <= 4000)
                    {
                        parameter.Size = 4000;
                    }
                    else if (s.Length <= 8000)
                    {
                        parameter.Size = 8000;
                    }
                    else
                    {
                        parameter.Size = -1;
                    }
                }
            }

            ///返回创建的参数
            return(parameter);
        }
        /// <summary>
        /// Resets the DbType property to its original settings
        /// </summary>
        public override void ResetDbType()
        {
            if (_dbType != null
                || _edmType != null)
            {
                PropertyChanging();
            }

            _edmType = null;
            _dbType = null;
        }
 /// <summary>
 ///     Initializes a new instance of the <see cref="DecimalTypeMapping" /> class.
 /// </summary>
 /// <param name="storeType"> The name of the database type. </param>
 /// <param name="dbType"> The <see cref="DbType" /> to be used. </param>
 public DecimalTypeMapping(
     [NotNull] string storeType,
     DbType?dbType = null)
     : this(storeType, null, dbType)
 {
 }
 /// <summary>
 /// 初始化数据库列特性
 /// </summary>
 /// <param name="columnName">数据库列名</param>
 /// <param name="dbType">数据类型</param>
 public DatabaseColumnAtrribute(String columnName, DbType dbType)
 {
     this._columnName = columnName;
     this._dbType = dbType;
 }
 /// <summary>
 /// Initialises a new instance of the <see cref="ColumnAttribute" /> class.
 /// </summary>
 /// <param name="name">The name of the column in the database table that the property maps to.</param>
 /// <param name="dbType">The type of the column in the database table that the property maps to.</param>
 /// <param name="allowInsert">true if the column value can be inserted, otherwise false.</param>
 /// <param name="allowUpdate">true if the column value can be updated, otherwise false.</param>
 public ColumnAttribute(string name, DbType? dbType, bool allowInsert, bool allowUpdate)
 {
     this.name = name;
     this.dbType = dbType;
     this.allowInsert = allowInsert;
     this.allowUpdate = allowUpdate;
 }