Пример #1
0
        public virtual void Index_removed_when_covered_by_an_inherited_foreign_key()
        {
            var modelBuilder = CreateModelBuilder();

            modelBuilder.Ignore <CustomerDetails>();
            modelBuilder.Ignore <OrderDetails>();
            modelBuilder.Ignore <BackOrder>();
            modelBuilder.Ignore <SpecialCustomer>();
            modelBuilder.Ignore <Product>();

            var principalEntityBuilder        = modelBuilder.Entity <Customer>();
            var derivedPrincipalEntityBuilder = modelBuilder.Entity <OtherCustomer>();
            var dependentEntityBuilder        = modelBuilder.Entity <Order>();
            var derivedDependentEntityBuilder = modelBuilder.Entity <BackOrder>();

            var dependentEntityType        = dependentEntityBuilder.Metadata;
            var derivedDependentEntityType = derivedDependentEntityBuilder.Metadata;

            Assert.Empty(derivedDependentEntityType.GetDeclaredIndexes());

            principalEntityBuilder.HasMany(c => c.Orders).WithOne(o => o.Customer)
            .HasForeignKey(
                o => new { o.CustomerId, o.AnotherCustomerId })
            .HasPrincipalKey(
                c => new { c.Id, c.AlternateKey });

            Assert.Empty(derivedDependentEntityType.GetDeclaredIndexes());

            derivedPrincipalEntityBuilder.HasMany <BackOrder>().WithOne()
            .HasForeignKey(
                o => new { o.CustomerId })
            .HasPrincipalKey(
                c => new { c.Id });

            var fk = dependentEntityType.GetForeignKeys().Single();

            Assert.Single(dependentEntityType.GetIndexes());
            Assert.False(dependentEntityType.FindIndex(fk.Properties).IsUnique);
            Assert.False(derivedDependentEntityType.GetDeclaredForeignKeys().Single().IsUnique);
            Assert.Empty(derivedDependentEntityType.GetDeclaredIndexes());

            var backOrderClone     = modelBuilder.Model.Clone().FindEntityType(derivedDependentEntityType.Name);
            var initialProperties  = backOrderClone.GetProperties().ToList();
            var initialKeys        = backOrderClone.GetKeys().ToList();
            var initialIndexes     = backOrderClone.GetIndexes().ToList();
            var initialForeignKeys = backOrderClone.GetForeignKeys().ToList();

            derivedDependentEntityBuilder.HasBaseType(null);

            var derivedFk = derivedDependentEntityType.GetForeignKeys()
                            .Single(foreignKey => foreignKey.PrincipalEntityType == derivedPrincipalEntityBuilder.Metadata);

            Assert.Equal(2, derivedDependentEntityType.GetIndexes().Count());
            Assert.False(derivedDependentEntityType.FindIndex(derivedFk.Properties).IsUnique);

            derivedDependentEntityBuilder.HasBaseType <Order>();

            fk = dependentEntityType.GetForeignKeys().Single();
            Assert.Single(dependentEntityType.GetIndexes());
            Assert.False(dependentEntityType.FindIndex(fk.Properties).IsUnique);

            AssertEqual(initialProperties, derivedDependentEntityType.GetProperties(), new PropertyComparer(compareAnnotations: false));
            AssertEqual(initialKeys, derivedDependentEntityType.GetKeys());
            AssertEqual(initialIndexes, derivedDependentEntityType.GetIndexes());
            AssertEqual(initialForeignKeys, derivedDependentEntityType.GetForeignKeys());

            principalEntityBuilder.HasOne <Order>().WithOne()
            .HasPrincipalKey <Customer>(
                c => new { c.Id })
            .HasForeignKey <Order>(
                o => new { o.CustomerId });

            modelBuilder.FinalizeModel();

            var(Level, _, Message, _, _) = modelBuilder.ModelLoggerFactory.Log.Single(e => e.Id == CoreEventId.RedundantIndexRemoved);
            Assert.Equal(LogLevel.Debug, Level);
            Assert.Equal(
                CoreResources.LogRedundantIndexRemoved(new TestLogger <TestLoggingDefinitions>()).GenerateMessage(
                    "{'CustomerId'}", nameof(Order), "{'CustomerId', 'AnotherCustomerId'}"), Message);

            fk = dependentEntityType.GetForeignKeys().Single(foreignKey => foreignKey.DependentToPrincipal == null);
            Assert.Equal(2, dependentEntityType.GetIndexes().Count());
            Assert.True(dependentEntityType.FindIndex(fk.Properties).IsUnique);
            Assert.Empty(derivedDependentEntityType.GetDeclaredIndexes());
        }
Пример #2
0
            public virtual void Index_removed_when_covered_by_an_inherited_index()
            {
                var modelBuilder = CreateModelBuilder();

                modelBuilder.Ignore <CustomerDetails>();
                modelBuilder.Ignore <OrderDetails>();
                modelBuilder.Ignore <BackOrder>();
                modelBuilder.Ignore <SpecialCustomer>();

                modelBuilder.Entity <Customer>();
                var derivedPrincipalEntityBuilder = modelBuilder.Entity <OtherCustomer>();
                var dependentEntityBuilder        = modelBuilder.Entity <Order>();
                var derivedDependentEntityBuilder = modelBuilder.Entity <BackOrder>();

                dependentEntityBuilder.HasIndex(
                    o => new
                {
                    o.CustomerId,
                    o.AnotherCustomerId
                })
                .IsUnique();

                derivedPrincipalEntityBuilder.HasMany <BackOrder>().WithOne()
                .HasPrincipalKey(
                    c => new
                {
                    c.Id
                })
                .HasForeignKey(
                    o => new
                {
                    o.CustomerId
                });

                var dependentEntityType        = dependentEntityBuilder.Metadata;
                var derivedDependentEntityType = derivedDependentEntityBuilder.Metadata;
                var index = dependentEntityType.FindIndex(dependentEntityType.GetForeignKeys().Single().Properties);

                Assert.False(index.IsUnique);
                Assert.True(dependentEntityType.GetIndexes().Single(i => i != index).IsUnique);
                Assert.False(derivedDependentEntityType.GetDeclaredForeignKeys().Single().IsUnique);
                Assert.Empty(derivedDependentEntityType.GetDeclaredIndexes());

                var backOrderClone     = modelBuilder.Model.Clone().FindEntityType(derivedDependentEntityType.Name);
                var initialProperties  = backOrderClone.GetProperties().ToList();
                var initialKeys        = backOrderClone.GetKeys().ToList();
                var initialIndexes     = backOrderClone.GetIndexes().ToList();
                var initialForeignKeys = backOrderClone.GetForeignKeys().ToList();

                derivedDependentEntityBuilder.HasBaseType(null);

                var derivedFk = derivedDependentEntityType.GetForeignKeys()
                                .Single(foreignKey => foreignKey.PrincipalEntityType == derivedPrincipalEntityBuilder.Metadata);

                Assert.Equal(2, derivedDependentEntityType.GetIndexes().Count());
                Assert.False(derivedDependentEntityType.FindIndex(derivedFk.Properties).IsUnique);

                derivedDependentEntityBuilder.HasBaseType <Order>();

                modelBuilder.FinalizeModel();

                var indexRemoveMessage =
                    CoreResources.LogRedundantIndexRemoved(new TestLogger <TestLoggingDefinitions>()).GenerateMessage(
                        "{'CustomerId'}", nameof(Order), "{'CustomerId', 'AnotherCustomerId'}");

                Assert.Equal(1, modelBuilder.ModelLoggerFactory.Log.Count(l => l.Message == indexRemoveMessage));

                var baseFK    = dependentEntityType.GetForeignKeys().Single();
                var baseIndex = dependentEntityType.FindIndex(baseFK.Properties);

                Assert.False(baseIndex.IsUnique);
                Assert.True(dependentEntityType.GetIndexes().Single(i => i != baseIndex).IsUnique);
                Assert.False(derivedDependentEntityType.GetDeclaredForeignKeys().Single().IsUnique);
                Assert.Empty(derivedDependentEntityType.GetDeclaredIndexes());

                AssertEqual(initialProperties, derivedDependentEntityType.GetProperties(), new PropertyComparer(false));
                AssertEqual(initialKeys, derivedDependentEntityType.GetKeys());
                AssertEqual(initialIndexes, derivedDependentEntityType.GetIndexes());
                AssertEqual(initialForeignKeys, derivedDependentEntityType.GetForeignKeys());
            }