Esempio n. 1
0
        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        protected virtual void EnsureSharedColumnsCompatibility([NotNull] IModel model)
        {
            foreach (var rootEntityType in model.GetRootEntityTypes())
            {
                var annotations          = RelationalExtensions.For(rootEntityType);
                var table                = Format(annotations.Schema, annotations.TableName);
                var properties           = rootEntityType.GetDerivedTypesInclusive().SelectMany(et => et.GetDeclaredProperties());
                var propertyTypeMappings = new Dictionary <string, IProperty>(StringComparer.OrdinalIgnoreCase);

                foreach (var property in properties)
                {
                    var       propertyAnnotations = RelationalExtensions.For(property);
                    var       columnName          = propertyAnnotations.ColumnName;
                    IProperty duplicateProperty;
                    if (propertyTypeMappings.TryGetValue(columnName, out duplicateProperty))
                    {
                        var previousAnnotations = RelationalExtensions.For(duplicateProperty);
                        var currentTypeString   = propertyAnnotations.ColumnType
                                                  ?? TypeMapper.GetMapping(property).StoreType;
                        var previousTypeString = previousAnnotations.ColumnType
                                                 ?? TypeMapper.GetMapping(duplicateProperty).StoreType;
                        if (!currentTypeString.Equals(previousTypeString, StringComparison.OrdinalIgnoreCase))
                        {
                            ShowError(RelationalStrings.DuplicateColumnNameDataTypeMismatch(
                                          duplicateProperty.DeclaringEntityType.DisplayName(),
                                          duplicateProperty.Name,
                                          property.DeclaringEntityType.DisplayName(),
                                          property.Name,
                                          columnName,
                                          table,
                                          previousTypeString,
                                          currentTypeString));
                        }

                        if (property.IsColumnNullable() != duplicateProperty.IsColumnNullable())
                        {
                            ShowError(RelationalStrings.DuplicateColumnNameNullabilityMismatch(
                                          duplicateProperty.DeclaringEntityType.DisplayName(),
                                          duplicateProperty.Name,
                                          property.DeclaringEntityType.DisplayName(),
                                          property.Name,
                                          columnName,
                                          table));
                        }

                        var currentComputedColumnSql  = propertyAnnotations.ComputedColumnSql ?? "";
                        var previousComputedColumnSql = previousAnnotations.ComputedColumnSql ?? "";
                        if (!currentComputedColumnSql.Equals(previousComputedColumnSql, StringComparison.OrdinalIgnoreCase))
                        {
                            ShowError(RelationalStrings.DuplicateColumnNameComputedSqlMismatch(
                                          duplicateProperty.DeclaringEntityType.DisplayName(),
                                          duplicateProperty.Name,
                                          property.DeclaringEntityType.DisplayName(),
                                          property.Name,
                                          columnName,
                                          table,
                                          previousComputedColumnSql,
                                          currentComputedColumnSql));
                        }

                        var currentDefaultValue  = propertyAnnotations.DefaultValue;
                        var previousDefaultValue = previousAnnotations.DefaultValue;
                        if (!Equals(currentDefaultValue, previousDefaultValue))
                        {
                            ShowError(RelationalStrings.DuplicateColumnNameDefaultSqlMismatch(
                                          duplicateProperty.DeclaringEntityType.DisplayName(),
                                          duplicateProperty.Name,
                                          property.DeclaringEntityType.DisplayName(),
                                          property.Name,
                                          columnName,
                                          table,
                                          previousDefaultValue ?? "NULL",
                                          currentDefaultValue ?? "NULL"));
                        }

                        var currentDefaultValueSql  = propertyAnnotations.DefaultValueSql ?? "";
                        var previousDefaultValueSql = previousAnnotations.DefaultValueSql ?? "";
                        if (!currentDefaultValueSql.Equals(previousDefaultValueSql, StringComparison.OrdinalIgnoreCase))
                        {
                            ShowError(RelationalStrings.DuplicateColumnNameDefaultSqlMismatch(
                                          duplicateProperty.DeclaringEntityType.DisplayName(),
                                          duplicateProperty.Name,
                                          property.DeclaringEntityType.DisplayName(),
                                          property.Name,
                                          columnName,
                                          table,
                                          previousDefaultValueSql,
                                          currentDefaultValueSql));
                        }
                    }
                    else
                    {
                        propertyTypeMappings[columnName] = property;
                    }
                }
            }
        }
        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        protected virtual void EnsureSharedColumnsCompatibility([NotNull] IModel model)
        {
            var groupedEntityTypes = new Dictionary <string, List <IEntityType> >();

            foreach (var entityType in model.GetEntityTypes())
            {
                var annotations = RelationalExtensions.For(entityType);
                var tableName   = annotations.Schema + "." + annotations.TableName;
                if (!groupedEntityTypes.ContainsKey(tableName))
                {
                    groupedEntityTypes[tableName] = new List <IEntityType>();
                }
                groupedEntityTypes[tableName].Add(entityType);
            }

            foreach (var table in groupedEntityTypes.Keys)
            {
                var properties           = groupedEntityTypes[table].SelectMany(et => et.GetDeclaredProperties());
                var propertyTypeMappings = new Dictionary <string, IProperty>();

                foreach (var property in properties)
                {
                    var       propertyAnnotations = RelationalExtensions.For(property);
                    var       columnName          = propertyAnnotations.ColumnName;
                    IProperty duplicateProperty;
                    if (propertyTypeMappings.TryGetValue(columnName, out duplicateProperty))
                    {
                        var previousAnnotations = RelationalExtensions.For(duplicateProperty);
                        var currentTypeString   = propertyAnnotations.ColumnType
                                                  ?? TypeMapper.GetMapping(property).StoreType;
                        var previousTypeString = previousAnnotations.ColumnType
                                                 ?? TypeMapper.GetMapping(duplicateProperty).StoreType;
                        if (!currentTypeString.Equals(previousTypeString, StringComparison.OrdinalIgnoreCase))
                        {
                            ShowError(RelationalStrings.DuplicateColumnName(
                                          duplicateProperty.DeclaringEntityType.DisplayName(),
                                          duplicateProperty.Name,
                                          property.DeclaringEntityType.DisplayName(),
                                          property.Name,
                                          columnName,
                                          table,
                                          previousTypeString,
                                          currentTypeString));
                        }

                        if (property.IsColumnNullable() != duplicateProperty.IsColumnNullable())
                        {
                            ShowError(RelationalStrings.DuplicateColumnNameNullabilityMismatch(
                                          duplicateProperty.DeclaringEntityType.DisplayName(),
                                          duplicateProperty.Name,
                                          property.DeclaringEntityType.DisplayName(),
                                          property.Name,
                                          columnName,
                                          table));
                        }

                        var currentComputedColumnSql  = propertyAnnotations.ComputedColumnSql ?? "";
                        var previousComputedColumnSql = previousAnnotations.ComputedColumnSql ?? "";
                        if (!currentComputedColumnSql.Equals(previousComputedColumnSql, StringComparison.OrdinalIgnoreCase))
                        {
                            ShowError(RelationalStrings.DuplicateColumnNameComputedSqlMismatch(
                                          duplicateProperty.DeclaringEntityType.DisplayName(),
                                          duplicateProperty.Name,
                                          property.DeclaringEntityType.DisplayName(),
                                          property.Name,
                                          columnName,
                                          table,
                                          previousComputedColumnSql,
                                          currentComputedColumnSql));
                        }

                        var currentDefaultValue  = propertyAnnotations.DefaultValue;
                        var previousDefaultValue = previousAnnotations.DefaultValue;
                        if (currentDefaultValue != previousDefaultValue)
                        {
                            ShowError(RelationalStrings.DuplicateColumnNameDefaultSqlMismatch(
                                          duplicateProperty.DeclaringEntityType.DisplayName(),
                                          duplicateProperty.Name,
                                          property.DeclaringEntityType.DisplayName(),
                                          property.Name,
                                          columnName,
                                          table,
                                          previousDefaultValue ?? "NULL",
                                          currentDefaultValue ?? "NULL"));
                        }

                        var currentDefaultValueSql  = propertyAnnotations.DefaultValueSql ?? "";
                        var previousDefaultValueSql = previousAnnotations.DefaultValueSql ?? "";
                        if (!currentDefaultValueSql.Equals(previousDefaultValueSql, StringComparison.OrdinalIgnoreCase))
                        {
                            ShowError(RelationalStrings.DuplicateColumnNameDefaultSqlMismatch(
                                          duplicateProperty.DeclaringEntityType.DisplayName(),
                                          duplicateProperty.Name,
                                          property.DeclaringEntityType.DisplayName(),
                                          property.Name,
                                          columnName,
                                          table,
                                          previousDefaultValueSql,
                                          currentDefaultValueSql));
                        }
                    }
                    else
                    {
                        propertyTypeMappings[columnName] = property;
                    }
                }
            }
        }