// TODO: Issue #4440 For non-required FK, nullable property should be matched over any other
        // [Fact]
        public void Inverts_if_principal_entity_type_can_have_nullable_fk_property_for_non_required_relationship()
        {
            DependentType.Property(DependentEntity.PeEKaYProperty, ConfigurationSource.Convention);
            var fkProperty = PrincipalType.Property(PrincipalEntity.DependentEntityKayPeeProperty, ConfigurationSource.Convention).Metadata;

            var relationshipBuilder = DependentType.Relationship(PrincipalType, ConfigurationSource.Convention)
                .IsUnique(true, ConfigurationSource.Convention)
                .IsRequired(false, ConfigurationSource.DataAnnotation);

            var newRelationshipBuilder = new ForeignKeyPropertyDiscoveryConvention().Apply(relationshipBuilder);
            Assert.Same(relationshipBuilder, newRelationshipBuilder);
            Assert.Same(DependentType.Metadata, newRelationshipBuilder.Metadata.DeclaringEntityType);

            var fk = (IForeignKey)PrincipalType.Metadata.GetForeignKeys().Single();
            Assert.Same(fk, newRelationshipBuilder.Metadata);
            Assert.Same(fkProperty, fk.Properties.Single());
            Assert.Same(DependentType.Metadata.FindPrimaryKey(), fk.PrincipalKey);
            Assert.True(fk.IsUnique);
            Assert.False(fk.IsRequired);
            Assert.Empty(DependentType.Metadata.GetForeignKeys());
        }
        public void Matches_navigation_plus_PK_name_property()
        {
            var fkProperty = DependentType.Property(DependentEntity.SomeNavPeEKaYProperty, ConfigurationSource.Convention).Metadata;
            DependentType.Property(DependentEntity.SomeNavIDProperty, ConfigurationSource.Convention);
            DependentType.Property(DependentEntity.PrincipalEntityIDProperty, ConfigurationSource.Convention);
            DependentType.Property(DependentEntity.PrincipalEntityPeEKaYProperty, ConfigurationSource.Convention);
            DependentType.Property(DependentEntity.PeEKaYProperty, ConfigurationSource.Convention);

            var relationshipBuilder = DependentType.Relationship(
                PrincipalType,
                "SomeNav",
                null,
                ConfigurationSource.Convention);

            var newRelationshipBuilder = new ForeignKeyPropertyDiscoveryConvention().Apply(relationshipBuilder);
            Assert.NotSame(relationshipBuilder, newRelationshipBuilder);

            var fk = (IForeignKey)DependentType.Metadata.GetForeignKeys().Single();
            Assert.Same(fk, newRelationshipBuilder.Metadata);
            Assert.Same(fkProperty, fk.Properties.Single());
            Assert.Same(PrimaryKey, fk.PrincipalKey.Properties.Single());
            Assert.False(fk.IsUnique);
            Assert.True(fk.IsRequired);
        }
        public void Does_not_invert_if_dependent_entity_type_can_have_non_pk_fk_property()
        {
            var fkProperty = DependentType.Property(DependentEntity.PeEKaYProperty, ConfigurationSource.Convention).Metadata;

            var relationshipBuilder = DependentType.Relationship(PrincipalType, ConfigurationSource.Convention)
                .IsUnique(true, ConfigurationSource.Convention);

            var newRelationshipBuilder = new ForeignKeyPropertyDiscoveryConvention().Apply(relationshipBuilder);
            Assert.NotSame(relationshipBuilder, newRelationshipBuilder);
            Assert.Same(DependentType.Metadata, newRelationshipBuilder.Metadata.DeclaringEntityType);

            newRelationshipBuilder = new ForeignKeyPropertyDiscoveryConvention().Apply(newRelationshipBuilder);

            var fk = (IForeignKey)DependentType.Metadata.GetForeignKeys().Single();
            Assert.Same(fk, newRelationshipBuilder.Metadata);
            Assert.Same(fkProperty, fk.Properties.Single());
            Assert.Same(PrimaryKey, fk.PrincipalKey.Properties.Single());
            Assert.True(fk.IsUnique);
            Assert.True(fk.IsRequired);
        }
        public void Does_not_invert_if_both_entity_types_can_have_non_pk_fk_property()
        {
            DependentType.Property(DependentEntity.PeEKaYProperty, ConfigurationSource.Convention);
            PrincipalType.Property(PrincipalEntity.DependentEntityKayPeeProperty, ConfigurationSource.Convention);

            var relationshipBuilder = DependentType.Relationship(PrincipalType, ConfigurationSource.Convention)
                .IsUnique(true, ConfigurationSource.Convention);

            var newRelationshipBuilder = new ForeignKeyPropertyDiscoveryConvention().Apply(relationshipBuilder);
            Assert.Same(relationshipBuilder, newRelationshipBuilder);

            var fk = (IForeignKey)newRelationshipBuilder.Metadata;
            Assert.Same(DependentType.Metadata, fk.DeclaringEntityType);
            Assert.Same(PrincipalType.Metadata.FindPrimaryKey(), fk.PrincipalKey);
            Assert.True(fk.IsUnique);
            Assert.False(fk.IsRequired);
        }
        public void Does_not_match_composite_dependent_PK_for_unique_FK_on_derived_type()
        {
            var modelBuilder = new InternalModelBuilder(new Model());

            var principalTypeWithCompositeKey = modelBuilder.Entity(typeof(PrincipalEntityWithCompositeKey), ConfigurationSource.Explicit);
            principalTypeWithCompositeKey.PrimaryKey(new[] { PrincipalEntityWithCompositeKey.IdProperty, PrincipalEntityWithCompositeKey.NameProperty }, ConfigurationSource.Explicit);
            principalTypeWithCompositeKey.Property(PrincipalEntityWithCompositeKey.NameProperty, ConfigurationSource.Explicit).IsRequired(true, ConfigurationSource.Explicit);

            var dependentTypeWithCompositeKeyBase = modelBuilder.Entity(typeof(DependentCompositeBase), ConfigurationSource.Explicit);
            var dependentTypeWithCompositeKey = modelBuilder.Entity(typeof(DependentEntityWithCompositeKey), ConfigurationSource.Explicit);
            dependentTypeWithCompositeKey.HasBaseType(dependentTypeWithCompositeKeyBase.Metadata, ConfigurationSource.Explicit);
            dependentTypeWithCompositeKeyBase.PrimaryKey(new[] { nameof(DependentEntityWithCompositeKey.NotId), nameof(DependentEntityWithCompositeKey.NotName) }, ConfigurationSource.Explicit);

            var relationshipBuilder = dependentTypeWithCompositeKey.Relationship(
                principalTypeWithCompositeKey,
                "NavProp",
                "InverseReferenceNav", ConfigurationSource.Convention)
                .IsUnique(true, ConfigurationSource.DataAnnotation)
                .DependentEntityType(dependentTypeWithCompositeKey, ConfigurationSource.DataAnnotation);

            var newRelationshipBuilder = new ForeignKeyPropertyDiscoveryConvention().Apply(relationshipBuilder);
            Assert.Same(relationshipBuilder, newRelationshipBuilder);

            var fk = (IForeignKey)dependentTypeWithCompositeKey.Metadata.GetForeignKeys().Single();
            Assert.Same(fk, newRelationshipBuilder.Metadata);
            Assert.NotEqual(dependentTypeWithCompositeKey.Metadata.FindPrimaryKey().Properties[0], fk.Properties[0]);
            Assert.NotEqual(dependentTypeWithCompositeKey.Metadata.FindPrimaryKey().Properties[1], fk.Properties[1]);
            Assert.Equal("NavProp" + CompositePrimaryKey[0].Name + "1", fk.Properties[0].Name);
            Assert.Equal("NavProp" + CompositePrimaryKey[1].Name + "1", fk.Properties[1].Name);
            Assert.Same(principalTypeWithCompositeKey.Metadata.FindPrimaryKey(), fk.PrincipalKey);
            Assert.True(fk.IsUnique);
        }
        public void Matches_composite_dependent_PK_for_unique_FK()
        {
            var fkProperty1 = DependentTypeWithCompositeKey.Metadata.FindPrimaryKey().Properties[0];
            var fkProperty2 = DependentTypeWithCompositeKey.Metadata.FindPrimaryKey().Properties[1];

            var relationshipBuilder = DependentTypeWithCompositeKey.Relationship(
                PrincipalTypeWithCompositeKey,
                "NavProp",
                "InverseReferenceNav", ConfigurationSource.Convention)
                .IsUnique(true, ConfigurationSource.DataAnnotation)
                .DependentEntityType(DependentTypeWithCompositeKey, ConfigurationSource.DataAnnotation);

            var newRelationshipBuilder = new ForeignKeyPropertyDiscoveryConvention().Apply(relationshipBuilder);
            Assert.NotSame(relationshipBuilder, newRelationshipBuilder);

            var fk = (IForeignKey)DependentTypeWithCompositeKey.Metadata.GetForeignKeys().Single();
            Assert.Same(fk, newRelationshipBuilder.Metadata);
            Assert.Same(fkProperty1, fk.Properties[0]);
            Assert.Same(fkProperty2, fk.Properties[1]);
            Assert.Same(PrincipalTypeWithCompositeKey.Metadata.FindPrimaryKey(), fk.PrincipalKey);
            Assert.True(fk.IsUnique);
            Assert.True(fk.IsRequired);
        }
        public void Matches_dependent_PK_for_unique_FK_set_by_higher_source_than_convention()
        {
            var fkProperty = DependentType.Metadata.FindPrimaryKey().Properties.Single();
            var relationshipBuilder = DependentType.Relationship(
                PrincipalType,
                "SomeNav",
                "InverseReferenceNav",
                ConfigurationSource.Convention)
                .IsUnique(true, ConfigurationSource.DataAnnotation)
                .DependentEntityType(DependentType, ConfigurationSource.DataAnnotation);

            var newRelationshipBuilder = new ForeignKeyPropertyDiscoveryConvention().Apply(relationshipBuilder);
            Assert.NotSame(relationshipBuilder, newRelationshipBuilder);

            var fk = (IForeignKey)DependentType.Metadata.GetForeignKeys().Single();
            Assert.Same(fk, newRelationshipBuilder.Metadata);
            Assert.Same(fkProperty, fk.Properties.Single());
            Assert.Same(PrimaryKey, fk.PrincipalKey.Properties.Single());
            Assert.True(fk.IsUnique);
            Assert.True(fk.IsRequired);
        }
        public void Matches_PK_name_properties()
        {
            var fkProperty1 = DependentTypeWithCompositeKey.Property(DependentEntityWithCompositeKey.IdProperty, ConfigurationSource.Convention);
            var fkProperty2 = DependentTypeWithCompositeKey.Property(DependentEntityWithCompositeKey.NameProperty, ConfigurationSource.Convention);
            fkProperty2.IsRequired(true, ConfigurationSource.Convention);

            var relationshipBuilder = DependentTypeWithCompositeKey.Relationship(
                PrincipalTypeWithCompositeKey,
                "NavProp",
                null,
                ConfigurationSource.Convention);

            var newRelationshipBuilder = new ForeignKeyPropertyDiscoveryConvention().Apply(relationshipBuilder);
            Assert.NotSame(relationshipBuilder, newRelationshipBuilder);

            var fk = (IForeignKey)DependentTypeWithCompositeKey.Metadata.GetForeignKeys().Single();
            Assert.Same(fk, newRelationshipBuilder.Metadata);
            Assert.Same(fkProperty1.Metadata, fk.Properties[0]);
            Assert.Same(fkProperty2.Metadata, fk.Properties[1]);
            Assert.Same(CompositePrimaryKey[0], fk.PrincipalKey.Properties[0]);
            Assert.Same(CompositePrimaryKey[1], fk.PrincipalKey.Properties[1]);
        }
        public void Matches_Id_property()
        {
            var fkProperty = DependentType.Property(DependentEntity.IDProperty, ConfigurationSource.Convention).Metadata;

            var relationshipBuilder = DependentType.Relationship(
                PrincipalType,
                "SomeNav",
                null,
                ConfigurationSource.Convention);

            var newRelationshipBuilder = new ForeignKeyPropertyDiscoveryConvention().Apply(relationshipBuilder);
            Assert.NotSame(relationshipBuilder, newRelationshipBuilder);

            var fk = (IForeignKey)DependentType.Metadata.GetForeignKeys().Single();
            Assert.Same(fk, newRelationshipBuilder.Metadata);
            Assert.Same(fkProperty, fk.Properties.Single());
            Assert.Same(PrimaryKey, fk.PrincipalKey.Properties.Single());
        }