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,
                DependentType,
                null,
                null,
                null,
                null,
                ConfigurationSource.Convention,
                isUnique: true);

            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_override_explicit_foreign_key_created_using_given_property()
        {
            DependentType.Property(DependentEntity.SomeNavIDProperty, ConfigurationSource.Convention);
            DependentType.Property(DependentEntity.SomeNavPeEKaYProperty, ConfigurationSource.Convention);
            DependentType.Property(DependentEntity.PrincipalEntityIDProperty, ConfigurationSource.Convention);
            DependentType.Property(DependentEntity.PrincipalEntityPeEKaYProperty, ConfigurationSource.Convention);
            DependentType.Property(DependentEntity.IDProperty, ConfigurationSource.Convention);
            DependentType.Property(DependentEntity.PeEKaYProperty, ConfigurationSource.Convention);
            var fkProperty = DependentType.Property("No!No!", typeof(int), ConfigurationSource.Convention).Metadata;

            var relationshipBuilder = DependentType.Relationship(
                PrincipalType,
                DependentType,
                "SomeNav",
                null,
                ConfigurationSource.Explicit,
                isUnique: false)
                                      .ForeignKey(new[] { fkProperty }, ConfigurationSource.Explicit);

            Assert.Same(relationshipBuilder, new ForeignKeyPropertyDiscoveryConvention().Apply(relationshipBuilder));

            var fk = (IForeignKey)relationshipBuilder.Metadata;

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

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

            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_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 dependentEntityTypeBuilder = DependentType;
            var relationshipBuilder        = dependentEntityTypeBuilder.Relationship(
                PrincipalType,
                dependentEntityTypeBuilder,
                "SomeNav",
                null,
                null,
                null,
                ConfigurationSource.Convention,
                isUnique: false);

            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);
        }
Ejemplo n.º 5
0
        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,
                DependentType,
                null,
                null,
                null,
                null,
                ConfigurationSource.Convention,
                isUnique: true,
                isRequired: false);

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

            Assert.NotSame(relationshipBuilder, newRelationshipBuilder);
            Assert.Same(PrincipalType.Metadata, newRelationshipBuilder.Metadata.DeclaringEntityType);

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

            var fk = (IForeignKey)PrincipalType.Metadata.GetForeignKeys().Single();

            Assert.Same(fk, newRelationshipBuilder.Metadata);
            Assert.Same(fkProperty, fk.Properties.Single());
            Assert.Same(DependentType.Metadata.GetPrimaryKey(), fk.PrincipalKey);
            Assert.True(fk.IsUnique);
            Assert.False(fk.IsRequired);
            Assert.Empty(DependentType.Metadata.GetForeignKeys());
        }
Ejemplo n.º 6
0
        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(
                DependentType,
                PrincipalType,
                null,
                null,
                null,
                null,
                ConfigurationSource.Convention,
                isUnique: true);

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

            Assert.NotSame(relationshipBuilder, newRelationshipBuilder);
            Assert.Same(PrincipalType.Metadata, newRelationshipBuilder.Metadata.DeclaringEntityType);

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

            var fk = (IForeignKey)newRelationshipBuilder.Metadata;

            Assert.Same(DependentType.Metadata.GetPrimaryKey(), fk.PrincipalKey);
            Assert.True(fk.IsUnique);
            Assert.False(fk.IsRequired);
        }
        public void Inverts_and_sets_foreign_key_if_matching_non_shadow_property_added_on_principal_type()
        {
            var relationshipBuilder = PrincipalType
                                      .Relationship(DependentType, "InverseReferenceNav", "SomeNav", ConfigurationSource.Convention)
                                      .IsUnique(true, ConfigurationSource.Convention);

            var fk = (IForeignKey)relationshipBuilder.Metadata;

            Assert.Same(fk, PrincipalType.Metadata.GetForeignKeys().Single());
            Assert.Equal("InverseReferenceNavKayPee", fk.Properties.Single().Name);
            Assert.Same(fk.DeclaringEntityType, PrincipalType.Metadata);
            Assert.Same(fk.PrincipalEntityType, DependentType.Metadata);
            Assert.True(fk.IsUnique);

            var property = DependentType.Property(DependentEntity.SomeNavIDProperty, ConfigurationSource.Convention);

            Assert.Same(property, new ForeignKeyPropertyDiscoveryConvention().Apply(property));
            Assert.Same(property, new ForeignKeyPropertyDiscoveryConvention().Apply(property));

            var newFk = DependentType.Metadata.GetForeignKeys().Single();

            Assert.NotSame(fk, newFk);
            Assert.Equal(property.Metadata, newFk.Properties.Single());
            Assert.Same(newFk.DeclaringEntityType, fk.PrincipalEntityType);
            Assert.Same(newFk.PrincipalEntityType, fk.DeclaringEntityType);
            Assert.True(newFk.IsUnique);
        }
        public void Does_nothing_if_a_foreign_key_on_the_best_candidate_property_already_exists()
        {
            var fkProperty = DependentType.Property(DependentEntity.SomeNavPeEKaYProperty, ConfigurationSource.Convention).Metadata;

            DependentType.Property(DependentEntity.PrincipalEntityIDProperty, ConfigurationSource.Convention);
            DependentType.Property(DependentEntity.PrincipalEntityPeEKaYProperty, ConfigurationSource.Convention);
            DependentType.Property(DependentEntity.PeEKaYProperty, ConfigurationSource.Convention);

            var relationshipBuilder = DependentType.HasForeignKey(
                PrincipalType, new[] { fkProperty }, ConfigurationSource.Convention);

            var fk = (IForeignKey)relationshipBuilder.Metadata;

            Assert.Same(fkProperty, fk.Properties.Single());
            Assert.False(fk.IsUnique);
            Assert.True(fk.IsRequired);

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

            Assert.Same(relationshipBuilder, new ForeignKeyPropertyDiscoveryConvention().Apply(relationshipBuilder));

            var newFk = (IForeignKey)relationshipBuilder.Metadata;

            Assert.Equal(2, DependentType.Metadata.GetForeignKeys().Count());
            Assert.NotEqual(fkProperty.Name, newFk.Properties.Single().Name);
        }
        public void Does_nothing_if_matching_shadow_property_added()
        {
            var relationshipBuilder = DependentType.Relationship(PrincipalType, "SomeNav", null, ConfigurationSource.Convention);

            Assert.Same(relationshipBuilder, new ForeignKeyPropertyDiscoveryConvention().Apply(relationshipBuilder));

            var fk = (IForeignKey)relationshipBuilder.Metadata;

            Assert.Same(fk, DependentType.Metadata.GetForeignKeys().Single());
            Assert.Equal("SomeNav" + PrimaryKey.Name, fk.Properties.Single().Name);
            Assert.False(fk.IsUnique);

            var property = DependentType.Property("SomeNavId", typeof(int?), ConfigurationSource.Convention);

            Assert.Same(property, new ForeignKeyPropertyDiscoveryConvention().Apply(property));
            Assert.Same(fk, DependentType.Metadata.GetForeignKeys().Single());
            Assert.Equal("SomeNav" + PrimaryKey.Name, fk.Properties.Single().Name);
            Assert.False(fk.IsUnique);
        }
        public void Sets_foreign_key_if_matching_non_shadow_property_added()
        {
            var relationshipBuilder = DependentType.Relationship(PrincipalType, "SomeNav", null, ConfigurationSource.Convention);

            Assert.Same(relationshipBuilder, new ForeignKeyPropertyDiscoveryConvention().Apply(relationshipBuilder));

            var fk = (IForeignKey)relationshipBuilder.Metadata;

            Assert.Same(fk, DependentType.Metadata.GetForeignKeys().Single());
            Assert.Equal("SomeNav" + PrimaryKey.Name, fk.Properties.Single().Name);
            Assert.False(fk.IsUnique);

            var property = DependentType.Property(DependentEntity.SomeNavIDProperty, ConfigurationSource.Convention);

            Assert.Same(property, new ForeignKeyPropertyDiscoveryConvention().Apply(property));

            var newFk = DependentType.Metadata.GetForeignKeys().Single();

            Assert.NotSame(fk, newFk);
            Assert.Equal(property.Metadata, newFk.Properties.Single());
        }
        public void Throws_on_ambiguous_relationship()
        {
            DependentType.Property(DependentEntity.PrincipalEntityPeEKaYProperty, ConfigurationSource.Convention);
            DependentType.Property(DependentEntity.PrincipalEntityIDProperty, ConfigurationSource.Convention);
            DependentType.Property(DependentEntity.PeEKaYProperty, ConfigurationSource.Convention);

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

            var convention             = new ForeignKeyPropertyDiscoveryConvention();
            var newRelationshipBuilder = convention.Apply(relationshipBuilder);

            Assert.NotSame(relationshipBuilder, newRelationshipBuilder);

            var otherRelationshipBuilder = DependentType.Relationship(
                PrincipalType,
                (string)null,
                null,
                ConfigurationSource.Convention);

            var otherNewRelationshipBuilder = convention.Apply(otherRelationshipBuilder);

            Assert.Same(otherRelationshipBuilder, otherNewRelationshipBuilder);

            Assert.Equal(
                CoreStrings.AmbiguousForeignKeyPropertyCandidates(
                    nameof(DependentEntity) + ".SomeNav",
                    nameof(PrincipalEntity),
                    nameof(DependentEntity),
                    nameof(PrincipalEntity),
                    "{'" + nameof(DependentEntity.PrincipalEntityPeEKaY) + "'}"),
                Assert.Throws <InvalidOperationException>(() => convention.Apply(DependentType.Metadata.Model.Builder)).Message);

            newRelationshipBuilder.Metadata.UpdateForeignKeyPropertiesConfigurationSource(ConfigurationSource.Explicit);

            convention.Apply(DependentType.Metadata.Model.Builder);
        }
        public void Matches_principal_type_plus_PK_name_property()
        {
            var fkProperty = DependentType.Property(DependentEntity.PrincipalEntityPeEKaYProperty, ConfigurationSource.Convention).Metadata;

            DependentType.Property(DependentEntity.PrincipalEntityIDProperty, 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());
        }