Exemplo n.º 1
0
        internal void Configure(
            DbDatabaseMapping databaseMapping,
            MappingFragment fragment,
            EntityType entityType)
        {
            IEnumerable <EdmPropertyPath> edmPropertyPath = EntityMappingConfiguration.PropertyPathToEdmPropertyPath(this.PropertyPath, entityType);

            if (edmPropertyPath.Count <EdmPropertyPath>() > 1)
            {
                throw Error.InvalidNotNullCondition((object)this.PropertyPath.ToString(), (object)entityType.Name);
            }
            EdmProperty column = fragment.ColumnMappings.Where <ColumnMappingBuilder>((Func <ColumnMappingBuilder, bool>)(pm => pm.PropertyPath.SequenceEqual <EdmProperty>((IEnumerable <EdmProperty>)edmPropertyPath.Single <EdmPropertyPath>()))).Select <ColumnMappingBuilder, EdmProperty>((Func <ColumnMappingBuilder, EdmProperty>)(pm => pm.ColumnProperty)).SingleOrDefault <EdmProperty>();

            if (column == null || !fragment.Table.Properties.Contains(column))
            {
                throw Error.InvalidNotNullCondition((object)this.PropertyPath.ToString(), (object)entityType.Name);
            }
            if (ValueConditionConfiguration.AnyBaseTypeToTableWithoutColumnCondition(databaseMapping, entityType, fragment.Table, column))
            {
                column.Nullable = true;
            }
            new System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration()
            {
                IsNullable = new bool?(false),
                OverridableConfigurationParts = OverridableConfigurationParts.OverridableInSSpace
            }.Configure(edmPropertyPath.Single <EdmPropertyPath>().Last <EdmProperty>());
            fragment.AddNullabilityCondition(column, false);
        }
Exemplo n.º 2
0
        internal void Configure(
            DbDatabaseMapping databaseMapping,
            MappingFragment fragment,
            EntityType entityType,
            DbProviderManifest providerManifest)
        {
            EdmProperty edmProperty = fragment.Table.Properties.SingleOrDefault <EdmProperty>((Func <EdmProperty, bool>)(c => string.Equals(c.Name, this.Discriminator, StringComparison.Ordinal)));

            if (edmProperty != null && ValueConditionConfiguration.GetMappingFragmentsWithColumnAsDefaultDiscriminator(databaseMapping, fragment.Table, edmProperty).Any <MappingFragment>())
            {
                edmProperty.Name = fragment.Table.Properties.Select <EdmProperty, string>((Func <EdmProperty, string>)(p => p.Name)).Uniquify(edmProperty.Name);
                edmProperty      = (EdmProperty)null;
            }
            if (edmProperty == null)
            {
                edmProperty = new EdmProperty(this.Discriminator, providerManifest.GetStoreType(DatabaseMappingGenerator.DiscriminatorTypeUsage))
                {
                    Nullable = false
                };
                TablePrimitiveOperations.AddColumn(fragment.Table, edmProperty);
            }
            if (ValueConditionConfiguration.AnyBaseTypeToTableWithoutColumnCondition(databaseMapping, entityType, fragment.Table, edmProperty))
            {
                edmProperty.Nullable = true;
            }
            System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration configuration = edmProperty.GetConfiguration() as System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration;
            if (this.Value != null)
            {
                this.ConfigureColumnType(providerManifest, configuration, edmProperty);
                fragment.AddDiscriminatorCondition(edmProperty, this.Value);
            }
            else
            {
                if (string.IsNullOrWhiteSpace(edmProperty.TypeName))
                {
                    TypeUsage storeType = providerManifest.GetStoreType(DatabaseMappingGenerator.DiscriminatorTypeUsage);
                    edmProperty.PrimitiveType = (PrimitiveType)storeType.EdmType;
                    edmProperty.MaxLength     = new int?(128);
                    edmProperty.Nullable      = false;
                }
                this.GetOrCreateConfiguration <System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration>().IsNullable = new bool?(true);
                fragment.AddNullabilityCondition(edmProperty, true);
            }
            if (this._configuration == null)
            {
                return;
            }
            string errorMessage;

            if (configuration != null && (configuration.OverridableConfigurationParts & OverridableConfigurationParts.OverridableInCSpace) != OverridableConfigurationParts.OverridableInCSpace && !configuration.IsCompatible(this._configuration, true, out errorMessage))
            {
                throw Error.ConflictingColumnConfiguration((object)edmProperty, (object)fragment.Table, (object)errorMessage);
            }
            if (this._configuration.IsNullable.HasValue)
            {
                edmProperty.Nullable = this._configuration.IsNullable.Value;
            }
            this._configuration.Configure(edmProperty, fragment.Table, providerManifest, false, false);
        }
        internal void Configure(
            DbDatabaseMapping databaseMapping, MappingFragment fragment, EntityType entityType)
        {
            DebugCheck.NotNull(fragment);

            var edmPropertyPath = EntityMappingConfiguration.PropertyPathToEdmPropertyPath(PropertyPath, entityType);

            if (edmPropertyPath.Count() > 1)
            {
                throw Error.InvalidNotNullCondition(PropertyPath.ToString(), entityType.Name);
            }

            var column
                = fragment.ColumnMappings
                  .Where(pm => pm.PropertyPath.SequenceEqual(edmPropertyPath.Single()))
                  .Select(pm => pm.ColumnProperty)
                  .SingleOrDefault();

            if (column == null ||
                !fragment.Table.Properties.Contains(column))
            {
                throw Error.InvalidNotNullCondition(PropertyPath.ToString(), entityType.Name);
            }

            if (ValueConditionConfiguration.AnyBaseTypeToTableWithoutColumnCondition(
                    databaseMapping, entityType, fragment.Table, column))
            {
                column.Nullable = true;
            }

            // Make the property required
            var newConfiguration = new Properties.Primitive.PrimitivePropertyConfiguration
            {
                IsNullable = false,
                OverridableConfigurationParts =
                    OverridableConfigurationParts.OverridableInSSpace
            };

            newConfiguration.Configure(edmPropertyPath.Single().Last());

            fragment.AddNullabilityCondition(column, isNull: false);
        }
        internal void Configure(
            DbDatabaseMapping databaseMapping,
            MappingFragment fragment,
            EntityType entityType,
            DbProviderManifest providerManifest)
        {
            DebugCheck.NotNull(fragment);
            DebugCheck.NotNull(providerManifest);

            var discriminatorColumn
                = fragment.Table.Properties
                  .SingleOrDefault(c => string.Equals(c.Name, Discriminator, StringComparison.Ordinal));

            if (discriminatorColumn != null)
            {
                if (GetMappingFragmentsWithColumnAsDefaultDiscriminator(databaseMapping, fragment.Table, discriminatorColumn).Any())
                {
                    // There is at least one fragment that uses this column as the default discriminator
                    // so to avoid conflict with the new discriminator column it needs to be renamed
                    discriminatorColumn.Name = fragment.Table.Properties.Select(p => p.Name).Uniquify(discriminatorColumn.Name);
                    discriminatorColumn      = null;
                }
            }

            if (discriminatorColumn == null)
            {
                var typeUsage
                    = providerManifest.GetStoreType(DatabaseMappingGenerator.DiscriminatorTypeUsage);

                discriminatorColumn
                    = new EdmProperty(Discriminator, typeUsage)
                    {
                    Nullable = false
                    };

                TablePrimitiveOperations.AddColumn(fragment.Table, discriminatorColumn);
            }

            if (AnyBaseTypeToTableWithoutColumnCondition(
                    databaseMapping, entityType, fragment.Table, discriminatorColumn))
            {
                discriminatorColumn.Nullable = true;
            }

            var existingConfiguration
                = discriminatorColumn.GetConfiguration() as Properties.Primitive.PrimitivePropertyConfiguration;

            if (Value != null)
            {
                ConfigureColumnType(providerManifest, existingConfiguration, discriminatorColumn);

                fragment.AddDiscriminatorCondition(discriminatorColumn, Value);
            }
            else
            {
                if (string.IsNullOrWhiteSpace(discriminatorColumn.TypeName))
                {
                    var typeUsage
                        = providerManifest.GetStoreType(DatabaseMappingGenerator.DiscriminatorTypeUsage);

                    discriminatorColumn.PrimitiveType = (PrimitiveType)typeUsage.EdmType;
                    discriminatorColumn.MaxLength     = DatabaseMappingGenerator.DiscriminatorMaxLength;
                    discriminatorColumn.Nullable      = false;
                }

                GetOrCreateConfiguration <Properties.Primitive.PrimitivePropertyConfiguration>().IsNullable = true;

                fragment.AddNullabilityCondition(discriminatorColumn, true);
            }

            if (_configuration == null)
            {
                return;
            }

            if (existingConfiguration != null)
            {
                string errorMessage;
                if ((existingConfiguration.OverridableConfigurationParts &
                     OverridableConfigurationParts.OverridableInCSpace) !=
                    OverridableConfigurationParts.OverridableInCSpace &&
                    !existingConfiguration.IsCompatible(
                        _configuration, inCSpace: true, errorMessage: out errorMessage))
                {
                    throw Error.ConflictingColumnConfiguration(discriminatorColumn, fragment.Table, errorMessage);
                }
            }

            if (_configuration.IsNullable != null)
            {
                discriminatorColumn.Nullable = _configuration.IsNullable.Value;
            }

            _configuration.Configure(discriminatorColumn, fragment.Table, providerManifest);
        }