protected T GetAttribute <T>(Type type, MemberInfo memberInfo) where T : Attribute
        {
            var attribute = Types.GetOrAdd((type, memberInfo), t =>
            {
                var tableExpr = Expressions.GetOrAdd(type, entityType => _migrationManager.GetCreateTableExpression(entityType));

                if (typeof(T) == typeof(TableAttribute))
                {
                    return new TableAttribute(tableExpr.TableName)
                    {
                        Schema = tableExpr.SchemaName
                    }
                }
                ;

                if (typeof(T) != typeof(ColumnAttribute))
                {
                    return(null);
                }

                var column = tableExpr.Columns.SingleOrDefault(cd => cd.Name.Equals(NameCompatibilityManager.GetColumnName(type, memberInfo.Name), StringComparison.OrdinalIgnoreCase));

                if (column is null)
                {
                    return(null);
                }

                var columnSystemType = (memberInfo as PropertyInfo)?.PropertyType ?? typeof(string);

                return(new ColumnAttribute
                {
                    Name = column.Name,
                    IsPrimaryKey = column.IsPrimaryKey,
                    IsColumn = true,
                    CanBeNull = column.IsNullable ?? false,
                    Length = column.Size ?? 0,
                    Precision = column.Precision ?? 0,
                    IsIdentity = column.IsIdentity,
                    DataType = SqlDataType.GetDataType(columnSystemType).Type.DataType
                });
            });

            return((T)attribute);
        }
示例#2
0
        protected T GetAttribute <T>(Type type, MemberInfo memberInfo) where T : Attribute
        {
            var attribute = Types.GetOrAdd((type, memberInfo), _ =>
            {
                var entityDescriptor = _mappingEntityAccessor.GetEntityDescriptor(type);

                if (typeof(T) == typeof(TableAttribute))
                {
                    return(new TableAttribute(entityDescriptor.EntityName));
                }

                if (typeof(T) != typeof(ColumnAttribute))
                {
                    return(null);
                }

                var entityField = entityDescriptor.Fields.SingleOrDefault(cd => cd.Name.Equals(NameCompatibilityManager.GetColumnName(type, memberInfo.Name), StringComparison.OrdinalIgnoreCase));

                if (entityField is null)
                {
                    return(null);
                }

                var columnSystemType = (memberInfo as PropertyInfo)?.PropertyType ?? typeof(string);

                return(new ColumnAttribute
                {
                    Name = entityField.Name,
                    IsPrimaryKey = entityField.IsPrimaryKey,
                    IsColumn = true,
                    CanBeNull = entityField.IsNullable ?? false,
                    Length = entityField.Size ?? 0,
                    Precision = entityField.Precision ?? 0,
                    IsIdentity = entityField.IsIdentity,
                    DataType = SqlDataType.GetDataType(columnSystemType).Type.DataType
                });
            });

            return((T)attribute);
        }