Ejemplo n.º 1
0
        public virtual void Detects_duplicate_column_names_within_hierarchy_with_different_value_generation_strategy(bool obsolete)
        {
            var modelBuilder = CreateConventionalModelBuilder();

            modelBuilder.Entity <Animal>();
            modelBuilder.Entity <Cat>(
                cb =>
            {
                if (obsolete)
                {
#pragma warning disable 618
                    cb.Property(c => c.Identity).UseMySqlIdentityColumn();
#pragma warning restore 618
                }
                else
                {
                    cb.Property(c => c.Identity).UseIdentityColumn();
                }
                cb.Property(c => c.Identity).HasColumnName(nameof(Cat.Identity));
            });
            modelBuilder.Entity <Dog>(
                db =>
            {
                db.Property(d => d.Identity).ValueGeneratedNever();
                db.Property(c => c.Identity).HasColumnName(nameof(Dog.Identity));
            });

            VerifyError(
                MySqlStrings.DuplicateColumnNameValueGenerationStrategyMismatch(
                    nameof(Cat), nameof(Cat.Identity), nameof(Dog), nameof(Dog.Identity), nameof(Cat.Identity), nameof(Animal)),
                modelBuilder.Model);
        }
Ejemplo n.º 2
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 override void ValidateSharedColumnsCompatibility(IReadOnlyList <IEntityType> mappedTypes, string tableName)
        {
            base.ValidateSharedColumnsCompatibility(mappedTypes, tableName);

            var identityColumns  = new List <IProperty>();
            var propertyMappings = new Dictionary <string, IProperty>();

            foreach (var property in mappedTypes.SelectMany(et => et.GetDeclaredProperties()))
            {
                var propertyAnnotations = property.Relational();
                var columnName          = propertyAnnotations.ColumnName;
                if (propertyMappings.TryGetValue(columnName, out var duplicateProperty))
                {
                    var propertyStrategy          = property.MySql().ValueGenerationStrategy;
                    var duplicatePropertyStrategy = duplicateProperty.MySql().ValueGenerationStrategy;
                    if (propertyStrategy != duplicatePropertyStrategy &&
                        (propertyStrategy == MySqlValueGenerationStrategy.IdentityColumn ||
                         duplicatePropertyStrategy == MySqlValueGenerationStrategy.IdentityColumn))
                    {
                        throw new InvalidOperationException(
                                  MySqlStrings.DuplicateColumnNameValueGenerationStrategyMismatch(
                                      duplicateProperty.DeclaringEntityType.DisplayName(),
                                      duplicateProperty.Name,
                                      property.DeclaringEntityType.DisplayName(),
                                      property.Name,
                                      columnName,
                                      tableName));
                    }
                }
                else
                {
                    propertyMappings[columnName] = property;
                    if (property.MySql().ValueGenerationStrategy == MySqlValueGenerationStrategy.IdentityColumn)
                    {
                        identityColumns.Add(property);
                    }
                }
            }

            if (identityColumns.Count > 1)
            {
                var sb = new StringBuilder()
                         .AppendJoin(identityColumns.Select(p => "'" + p.DeclaringEntityType.DisplayName() + "." + p.Name + "'"));
                throw new InvalidOperationException(MySqlStrings.MultipleIdentityColumns(sb, tableName));
            }
        }