Exemplo n.º 1
0
        void IMetadataBuilder.BuildColumnMetadata(ColumnMetadata metadata, PropertyBuilder propertyBuilder)
        {
            if (!metadata.IsColumn)
            {
                // for non-column mapping ignore any other metadata properties
                propertyBuilder.AddAttribute(WellKnownTypes.LinqToDB.Mapping.NotColumnAttribute);
                return;
            }

            // compared to old T4 implementation we use only ColumnAttribute
            // T4 used separate attributes for primary key, nullability and identity
            // but they just duplicate ColumnAttribute functionality
            var attr = propertyBuilder.AddAttribute(WellKnownTypes.LinqToDB.Mapping.ColumnAttribute);

            // always generate column name when it is provided, even if it match property name
            // otherwise generated code will be refactoring-unfriendly (will break on property rename)
            // note that rename could happen not only as user's action in generated code, but also during code
            // generation to resolve naming conflicts with other members/types
            if (metadata.Name != null)
            {
                attr.Parameter(_builder.Constant(metadata.Name, true));
            }

            // generate only "CanBeNull = false" only for non-default cases (where linq2db already infer nullability from type):
            // - for reference type is is true by default
            // - for value type it is false
            // - for nullable value type it is true
            if ((!propertyBuilder.Property.Type.Type.IsValueType && !metadata.CanBeNull) ||
                (propertyBuilder.Property.Type.Type.IsValueType && metadata.CanBeNull != propertyBuilder.Property.Type.Type.IsNullable))
            {
                attr.Parameter(WellKnownTypes.LinqToDB.Mapping.ColumnAttribute_CanBeNull, _builder.Constant(metadata.CanBeNull, true));
            }

            if (metadata.Configuration != null)
            {
                attr.Parameter(WellKnownTypes.LinqToDB.Mapping.ColumnAttribute_Configuration, _builder.Constant(metadata.Configuration, true));
            }
            if (metadata.DataType != null)
            {
                attr.Parameter(WellKnownTypes.LinqToDB.Mapping.ColumnAttribute_DataType, _builder.Constant(metadata.DataType.Value, true));
            }

            // generate database type attributes
            if (metadata.DbType?.Name != null)
            {
                attr.Parameter(WellKnownTypes.LinqToDB.Mapping.ColumnAttribute_DbType, _builder.Constant(metadata.DbType.Name, true));
            }
            if (metadata.DbType?.Length != null)
            {
                attr.Parameter(WellKnownTypes.LinqToDB.Mapping.ColumnAttribute_Length, _builder.Constant(metadata.DbType.Length.Value, true));
            }
            if (metadata.DbType?.Precision != null)
            {
                attr.Parameter(WellKnownTypes.LinqToDB.Mapping.ColumnAttribute_Precision, _builder.Constant(metadata.DbType.Precision.Value, true));
            }
            if (metadata.DbType?.Scale != null)
            {
                attr.Parameter(WellKnownTypes.LinqToDB.Mapping.ColumnAttribute_Scale, _builder.Constant(metadata.DbType.Scale.Value, true));
            }

            if (metadata.IsPrimaryKey)
            {
                attr.Parameter(WellKnownTypes.LinqToDB.Mapping.ColumnAttribute_IsPrimaryKey, _builder.Constant(true, true));
                if (metadata.PrimaryKeyOrder != null)
                {
                    attr.Parameter(WellKnownTypes.LinqToDB.Mapping.ColumnAttribute_PrimaryKeyOrder, _builder.Constant(metadata.PrimaryKeyOrder.Value, true));
                }
            }

            if (metadata.IsIdentity)
            {
                attr.Parameter(WellKnownTypes.LinqToDB.Mapping.ColumnAttribute_IsIdentity, _builder.Constant(true, true));
            }
            if (metadata.SkipOnInsert)
            {
                attr.Parameter(WellKnownTypes.LinqToDB.Mapping.ColumnAttribute_SkipOnInsert, _builder.Constant(true, true));
            }
            if (metadata.SkipOnUpdate)
            {
                attr.Parameter(WellKnownTypes.LinqToDB.Mapping.ColumnAttribute_SkipOnUpdate, _builder.Constant(true, true));
            }
            if (metadata.SkipOnEntityFetch)
            {
                attr.Parameter(WellKnownTypes.LinqToDB.Mapping.ColumnAttribute_SkipOnEntityFetch, _builder.Constant(true, true));
            }
            if (metadata.MemberName != null)
            {
                attr.Parameter(WellKnownTypes.LinqToDB.Mapping.ColumnAttribute_MemberName, _builder.Constant(metadata.MemberName, true));
            }
            if (metadata.Storage != null)
            {
                attr.Parameter(WellKnownTypes.LinqToDB.Mapping.ColumnAttribute_Storage, _builder.Constant(metadata.Storage, true));
            }
            if (metadata.CreateFormat != null)
            {
                attr.Parameter(WellKnownTypes.LinqToDB.Mapping.ColumnAttribute_CreateFormat, _builder.Constant(metadata.CreateFormat, true));
            }
            if (metadata.IsDiscriminator)
            {
                attr.Parameter(WellKnownTypes.LinqToDB.Mapping.ColumnAttribute_IsDiscriminator, _builder.Constant(true, true));
            }
            if (metadata.Order != null)
            {
                attr.Parameter(WellKnownTypes.LinqToDB.Mapping.ColumnAttribute_Order, _builder.Constant(metadata.Order.Value, true));
            }
        }
Exemplo n.º 2
0
 public static void AddAttribute(this PropertyBuilder Builder, CustomAttributeData CustomAttribute)
 => Builder.AddAttribute(CustomAttribute.ToCustomAttributeBuilder());
Exemplo n.º 3
0
 void IMetadataBuilder.BuildAssociationMetadata(AssociationMetadata metadata, PropertyBuilder propertyBuilder)
 {
     BuildAssociationAttribute(metadata, propertyBuilder.AddAttribute(WellKnownTypes.LinqToDB.Mapping.AssociationAttribute));
 }