private PrimitivePropertyConfiguration MergeWithExistingConfiguration(
            EdmProperty property, Func <string, Exception> getConflictException, bool inCSpace, bool fillFromExistingConfiguration)
        {
            var existingConfiguration = property.GetConfiguration() as PrimitivePropertyConfiguration;

            if (existingConfiguration != null)
            {
                var space = inCSpace ? OverridableConfigurationParts.OverridableInCSpace : OverridableConfigurationParts.OverridableInSSpace;
                if (existingConfiguration.OverridableConfigurationParts.HasFlag(space) ||
                    fillFromExistingConfiguration)
                {
                    return(existingConfiguration.OverrideFrom(this, inCSpace));
                }

                string errorMessage;
                if (OverridableConfigurationParts.HasFlag(space) ||
                    existingConfiguration.IsCompatible(this, inCSpace, errorMessage: out errorMessage))
                {
                    return(OverrideFrom(existingConfiguration, inCSpace));
                }

                throw getConflictException(errorMessage);
            }

            return(this);
        }
Ejemplo n.º 2
0
        private PrimitivePropertyConfiguration MergeWithExistingConfiguration(
            EdmProperty property,
            Func <string, Exception> getConflictException,
            bool inCSpace,
            bool fillFromExistingConfiguration)
        {
            PrimitivePropertyConfiguration configuration = property.GetConfiguration() as PrimitivePropertyConfiguration;

            if (configuration == null)
            {
                return(this);
            }
            OverridableConfigurationParts configurationParts = inCSpace ? OverridableConfigurationParts.OverridableInCSpace : OverridableConfigurationParts.OverridableInSSpace;

            if (configuration.OverridableConfigurationParts.HasFlag((Enum)configurationParts) || fillFromExistingConfiguration)
            {
                return(configuration.OverrideFrom(this, inCSpace));
            }
            string errorMessage;

            if (this.OverridableConfigurationParts.HasFlag((Enum)configurationParts) || configuration.IsCompatible(this, inCSpace, out errorMessage))
            {
                return(this.OverrideFrom(configuration, inCSpace));
            }
            throw getConflictException(errorMessage);
        }
        internal virtual void Configure(
            EdmProperty column, EntityType table, DbProviderManifest providerManifest,
            bool allowOverride = false,
            bool fillFromExistingConfiguration = false)
        {
            DebugCheck.NotNull(column);
            DebugCheck.NotNull(table);
            DebugCheck.NotNull(providerManifest);

            var existingConfiguration = column.GetConfiguration() as PrimitivePropertyConfiguration;

            if (existingConfiguration != null)
            {
                var overridable = column.GetAllowOverride();

                string errorMessage;
                if ((existingConfiguration.OverridableConfigurationParts
                     & OverridableConfigurationParts.OverridableInSSpace)
                    != OverridableConfigurationParts.OverridableInSSpace &&
                    !overridable &&
                    !allowOverride &&
                    !fillFromExistingConfiguration &&
                    !existingConfiguration.IsCompatible(this, inCSpace: false, errorMessage: out errorMessage))
                {
                    if (OverridableConfigurationParts.HasFlag(OverridableConfigurationParts.OverridableInSSpace))
                    {
                        OverrideFrom(existingConfiguration);
                    }
                    else
                    {
                        throw Error.ConflictingColumnConfiguration(column.Name, table.Name, errorMessage);
                    }
                }

                FillFrom(existingConfiguration, inCSpace: false);
            }

            ConfigureColumnName(column, table);

            ConfigureAnnotations(column);

            if (!string.IsNullOrWhiteSpace(ColumnType))
            {
                column.PrimitiveType = providerManifest.GetStoreTypeFromName(ColumnType);
            }

            if (ColumnOrder != null)
            {
                column.SetOrder(ColumnOrder.Value);
            }

            var storeType
                = providerManifest.GetStoreTypes()
                  .SingleOrDefault(t => t.Name.Equals(column.TypeName, StringComparison.OrdinalIgnoreCase));

            if (storeType != null)
            {
                storeType.FacetDescriptions.Each(f => Configure(column, f));
            }

            column.SetConfiguration(this);
            column.SetAllowOverride(allowOverride);
        }
        internal virtual void Configure(EdmProperty property)
        {
            DebugCheck.NotNull(property);
            Debug.Assert(property.TypeUsage != null);

            var existingConfiguration = property.GetConfiguration() as PrimitivePropertyConfiguration;

            if (existingConfiguration != null)
            {
                string errorMessage;
                if ((existingConfiguration.OverridableConfigurationParts
                     & OverridableConfigurationParts.OverridableInCSpace)
                    != OverridableConfigurationParts.OverridableInCSpace &&
                    !existingConfiguration.IsCompatible(this, inCSpace: true, errorMessage: out errorMessage))
                {
                    if (OverridableConfigurationParts.HasFlag(OverridableConfigurationParts.OverridableInCSpace))
                    {
                        OverrideFrom(existingConfiguration);
                    }
                    else
                    {
                        var propertyInfo      = property.GetClrPropertyInfo();
                        var declaringTypeName = propertyInfo == null
                            ? string.Empty
                            : ObjectContextTypeCache.GetObjectType(propertyInfo.DeclaringType).
                                                FullNameWithNesting();
                        throw Error.ConflictingPropertyConfiguration(property.Name, declaringTypeName, errorMessage);
                    }
                }

                // Choose the more derived type for the merged configuration
                PrimitivePropertyConfiguration mergedConfiguration;
                if (existingConfiguration.GetType().IsAssignableFrom(GetType()))
                {
                    mergedConfiguration = Clone();
                }
                else
                {
                    mergedConfiguration = existingConfiguration.Clone();
                    mergedConfiguration.CopyFrom(this);
                }
                mergedConfiguration.FillFrom(existingConfiguration, inCSpace: true);
                property.SetConfiguration(mergedConfiguration);
                ConfigureAnnotations(property);
            }
            else
            {
                property.SetConfiguration(this);
            }

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

            if (ConcurrencyMode != null)
            {
                property.ConcurrencyMode = ConcurrencyMode.Value;
            }

            if (DatabaseGeneratedOption != null)
            {
                property.SetStoreGeneratedPattern((StoreGeneratedPattern)DatabaseGeneratedOption.Value);

                if (DatabaseGeneratedOption.Value
                    == ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)
                {
                    property.Nullable = false;
                }
            }
        }
        public virtual void Key(
            PropertyInfo propertyInfo, OverridableConfigurationParts? overridableConfigurationParts = null)
        {
            Contract.Requires(propertyInfo != null);

            if (!propertyInfo.IsValidEdmScalarProperty())
            {
                throw Error.ModelBuilder_KeyPropertiesMustBePrimitive(propertyInfo.Name, ClrType);
            }

            if (!_isKeyConfigured
                &&
                // DevDiv #324763 (DbModelBuilder.Build is not idempotent):  If build is called twice when keys are configured via attributes 
                // _isKeyConfigured is not set, thus we need to check whether the key has already been included.
                !_keyProperties.ContainsSame(propertyInfo))
            {
                _keyProperties.Add(propertyInfo);

                Property(new PropertyPath(propertyInfo), overridableConfigurationParts);
            }
        }
        internal virtual void Key(PropertyInfo propertyInfo, OverridableConfigurationParts? overridableConfigurationParts)
        {
            DebugCheck.NotNull(propertyInfo);

            if (!propertyInfo.IsValidEdmScalarProperty())
            {
                throw Error.ModelBuilder_KeyPropertiesMustBePrimitive(propertyInfo.Name, ClrType);
            }

            if (!_isKeyConfigured
                && !_keyProperties.ContainsSame(propertyInfo))
            {
                _keyProperties.Add(propertyInfo);

                Property(new PropertyPath(propertyInfo), overridableConfigurationParts);
            }
        }
        internal virtual void Key(
            PropertyInfo propertyInfo, OverridableConfigurationParts? overridableConfigurationParts, bool configuredByAttribute)
        {
            DebugCheck.NotNull(propertyInfo);

            if (!propertyInfo.IsValidEdmScalarProperty())
            {
                throw Error.ModelBuilder_KeyPropertiesMustBePrimitive(propertyInfo.Name, ClrType);
            }

            if (!_isKeyConfigured
                &&
                // DevDiv #324763 (DbModelBuilder.Build is not idempotent):  If build is called twice when keys are configured via attributes 
                // _isKeyConfigured is not set, thus we need to check whether the key has already been included.
                !_keyProperties.ContainsSame(propertyInfo)
                && (configuredByAttribute || !_isKeyConfiguredByAttributes))
            {
                _isKeyConfiguredByAttributes |= configuredByAttribute;

                _keyProperties.Add(propertyInfo);

                Property(new PropertyPath(propertyInfo), overridableConfigurationParts);
            }
        }