public virtual void AddColumnNameAndTypeConfiguration(
            [NotNull] PropertyConfiguration propertyConfiguration)
        {
            Check.NotNull(propertyConfiguration, nameof(propertyConfiguration));


            var delimitedColumnName =
                ExtensionsProvider.For(propertyConfiguration.Property).ColumnName != null &&
                ExtensionsProvider.For(propertyConfiguration.Property).ColumnName != propertyConfiguration.Property.Name
                ? CSharpUtilities.DelimitString(
                    ExtensionsProvider.For(propertyConfiguration.Property).ColumnName)
                : null;

            var delimitedColumnTypeName =
                ExtensionsProvider.For(propertyConfiguration.Property).ColumnType != null
                ? CSharpUtilities.DelimitString(
                    ExtensionsProvider.For(propertyConfiguration.Property).ColumnType)
                : null;

            if (delimitedColumnName != null)
            {
                propertyConfiguration.FluentApiConfigurations.Add(
                    _configurationFactory.CreateFluentApiConfiguration(
                        /* hasAttributeEquivalent */ true,
                        nameof(RelationalPropertyBuilderExtensions.HasColumnName),
                        delimitedColumnName));

                if (delimitedColumnTypeName == null)
                {
                    propertyConfiguration.AttributeConfigurations.Add(
                        _configurationFactory.CreateAttributeConfiguration(nameof(ColumnAttribute), delimitedColumnName));
                }
                else
                {
                    propertyConfiguration.FluentApiConfigurations.Add(
                        _configurationFactory.CreateFluentApiConfiguration(
                            /* hasAttributeEquivalent */ true,
                            nameof(RelationalPropertyBuilderExtensions.HasColumnType),
                            delimitedColumnTypeName));
                    propertyConfiguration.AttributeConfigurations.Add(
                        _configurationFactory.CreateAttributeConfiguration(
                            nameof(ColumnAttribute),
                            new[] {
                        delimitedColumnName,
                        nameof(ColumnAttribute.TypeName) + " = " + delimitedColumnTypeName
                    }));
                }
            }
            else if (delimitedColumnTypeName != null)
            {
                propertyConfiguration.FluentApiConfigurations.Add(
                    _configurationFactory.CreateFluentApiConfiguration(
                        /* hasAttributeEquivalent */ false,
                        nameof(RelationalPropertyBuilderExtensions.HasColumnType),
                        delimitedColumnTypeName));
            }
        }
        public virtual void AddPropertyConfiguration([NotNull] PropertyConfiguration propertyConfiguration)
        {
            Check.NotNull(propertyConfiguration, nameof(propertyConfiguration));

            AddRequiredConfiguration(propertyConfiguration);
            AddMaxLengthConfiguration(propertyConfiguration);
            AddColumnNameAndTypeConfiguration(propertyConfiguration);
            AddDefaultValueConfiguration(propertyConfiguration);
            AddDefaultExpressionConfiguration(propertyConfiguration);
            AddValueGeneratedConfiguration(propertyConfiguration);
        }
        public virtual void AddValueGeneratedConfiguration(
            [NotNull] PropertyConfiguration propertyConfiguration)
        {
            Check.NotNull(propertyConfiguration, nameof(propertyConfiguration));

            if (!((Property)propertyConfiguration.Property).GetValueGeneratedConfigurationSource().HasValue)
            {
                return;
            }

            var valueGenerated = propertyConfiguration.Property.ValueGenerated;

            switch (valueGenerated)
            {
            case ValueGenerated.OnAdd:
                // If this property is the single integer primary key on the EntityType then
                // KeyConvention assumes ValueGeneratedOnAdd() so there is no need to add it.
                if (_keyConvention.FindValueGeneratedOnAddProperty(
                        new List <Property> {
                    (Property)propertyConfiguration.Property
                },
                        (EntityType)propertyConfiguration.EntityConfiguration.EntityType) == null &&
                    ExtensionsProvider.For(propertyConfiguration.Property).GeneratedValueSql == null)
                {
                    propertyConfiguration.FluentApiConfigurations.Add(
                        _configurationFactory.CreateFluentApiConfiguration(
                            /* hasAttributeEquivalent */ false,
                            nameof(PropertyBuilder.ValueGeneratedOnAdd)));
                }

                break;

            case ValueGenerated.OnAddOrUpdate:
                propertyConfiguration.FluentApiConfigurations.Add(
                    _configurationFactory.CreateFluentApiConfiguration(
                        /* hasAttributeEquivalent */ false,
                        nameof(PropertyBuilder.ValueGeneratedOnAddOrUpdate)));
                break;

            case ValueGenerated.Never:
                propertyConfiguration.FluentApiConfigurations.Add(
                    _configurationFactory.CreateFluentApiConfiguration(
                        /* hasAttributeEquivalent */ false,
                        nameof(PropertyBuilder.ValueGeneratedNever)));
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
        public virtual PropertyConfiguration GetOrAddPropertyConfiguration(
            [NotNull] EntityConfiguration entityConfiguration, [NotNull] Property property)
        {
            Check.NotNull(entityConfiguration, nameof(entityConfiguration));
            Check.NotNull(property, nameof(property));

            var propertyConfiguration = FindPropertyConfiguration(property);
            if (propertyConfiguration == null)
            {
                propertyConfiguration = new PropertyConfiguration(entityConfiguration, property);
                PropertyConfigurations.Add(propertyConfiguration);
            }

            return propertyConfiguration;
        }
        public virtual void AddDefaultExpressionConfiguration(
            [NotNull] PropertyConfiguration propertyConfiguration)
        {
            Check.NotNull(propertyConfiguration, nameof(propertyConfiguration));

            if (ExtensionsProvider.For(propertyConfiguration.Property).GeneratedValueSql != null)
            {
                propertyConfiguration.FluentApiConfigurations.Add(
                    _configurationFactory.CreateFluentApiConfiguration(
                        /* hasAttributeEquivalent */ false,
                        nameof(RelationalPropertyBuilderExtensions.HasDefaultValueSql),
                        CSharpUtilities.DelimitString(
                            ExtensionsProvider.For(propertyConfiguration.Property).GeneratedValueSql)));
            }
        }
        public virtual void AddDefaultValueConfiguration(
            [NotNull] PropertyConfiguration propertyConfiguration)
        {
            Check.NotNull(propertyConfiguration, nameof(propertyConfiguration));

            if (ExtensionsProvider.For(propertyConfiguration.Property).DefaultValue != null)
            {
                propertyConfiguration.FluentApiConfigurations.Add(
                    _configurationFactory.CreateFluentApiConfiguration(
                        /* hasAttributeEquivalent */ false,
                        nameof(RelationalPropertyBuilderExtensions.HasDefaultValue),
                        CSharpUtilities.GenerateLiteral(
                            (dynamic)ExtensionsProvider.For(propertyConfiguration.Property).DefaultValue)));
            }
        }
Пример #7
0
        public virtual PropertyConfiguration GetOrAddPropertyConfiguration(
            [NotNull] EntityConfiguration entityConfiguration, [NotNull] Property property)
        {
            Check.NotNull(entityConfiguration, nameof(entityConfiguration));
            Check.NotNull(property, nameof(property));

            var propertyConfiguration = FindPropertyConfiguration(property);

            if (propertyConfiguration == null)
            {
                propertyConfiguration = new PropertyConfiguration(entityConfiguration, property);
                PropertyConfigurations.Add(propertyConfiguration);
            }

            return(propertyConfiguration);
        }
        public virtual void AddMaxLengthConfiguration(
            [NotNull] PropertyConfiguration propertyConfiguration)
        {
            Check.NotNull(propertyConfiguration, nameof(propertyConfiguration));

            if (((Property)propertyConfiguration.Property).GetMaxLength().HasValue)
            {
                var maxLengthLiteral =
                    CSharpUtilities.GenerateLiteral(
                        ((Property)propertyConfiguration.Property).GetMaxLength().Value);
                propertyConfiguration.FluentApiConfigurations.Add(
                    _configurationFactory.CreateFluentApiConfiguration(
                        /* hasAttributeEquivalent */ true,
                        nameof(PropertyBuilder.HasMaxLength), maxLengthLiteral));
                propertyConfiguration.AttributeConfigurations.Add(
                    _configurationFactory.CreateAttributeConfiguration(nameof(MaxLengthAttribute), maxLengthLiteral));
            }
        }
        public virtual void AddRequiredConfiguration(
            [NotNull] PropertyConfiguration propertyConfiguration)
        {
            Check.NotNull(propertyConfiguration, nameof(propertyConfiguration));

            if (!propertyConfiguration.Property.IsNullable &&
                propertyConfiguration.Property.ClrType.IsNullableType())
            {
                var entityKeyProperties =
                    ((EntityType)propertyConfiguration.EntityConfiguration.EntityType)
                    .FindPrimaryKey()?.Properties
                    ?? Enumerable.Empty <Property>();
                if (!entityKeyProperties.Contains(propertyConfiguration.Property))
                {
                    propertyConfiguration.FluentApiConfigurations.Add(
                        _configurationFactory.CreateFluentApiConfiguration(
                            /* hasAttributeEquivalent */ true,
                            nameof(PropertyBuilder.IsRequired)));
                    propertyConfiguration.AttributeConfigurations.Add(
                        _configurationFactory.CreateAttributeConfiguration(nameof(RequiredAttribute)));
                }
            }
        }