public void Does_not_match_composite_dependent_PK_for_unique_FK_if_order_mismatched()
        {
            var fkProperty1 = DependentTypeWithCompositeKey.Metadata.FindPrimaryKey().Properties[0];
            var fkProperty2 = DependentTypeWithCompositeKey.Metadata.FindPrimaryKey().Properties[1];

            DependentTypeWithCompositeKey.PrimaryKey(new[] { fkProperty2.Name, fkProperty1.Name }, ConfigurationSource.Explicit);

            var relationshipBuilder = DependentTypeWithCompositeKey.Relationship(
                PrincipalTypeWithCompositeKey, "NavProp", "InverseReferenceNav", ConfigurationSource.Convention)
                                      .HasPrincipalKey(PrincipalTypeWithCompositeKey.Metadata.FindPrimaryKey().Properties, ConfigurationSource.DataAnnotation)
                                      .IsUnique(true, ConfigurationSource.DataAnnotation);

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

            var fk = relationshipBuilder.Metadata;

            Assert.Same(fk, DependentTypeWithCompositeKey.Metadata.GetForeignKeys().Single());
            Assert.Null(fk.GetForeignKeyPropertiesConfigurationSource());
            Assert.Equal(2, fk.Properties.Count);
            Assert.NotSame(fkProperty1, fk.Properties[0]);
            Assert.NotSame(fkProperty2, fk.Properties[1]);
            Assert.NotSame(fkProperty1, fk.Properties[1]);
            Assert.NotSame(fkProperty2, fk.Properties[0]);
            Assert.Same(CompositePrimaryKey[0], fk.PrincipalKey.Properties[0]);
            Assert.Same(CompositePrimaryKey[1], fk.PrincipalKey.Properties[1]);
            Assert.True(fk.IsUnique);
            Assert.False(fk.IsRequired);
        }
        public void Does_not_match_composite_dependent_PK_for_non_unique_FK()
        {
            DependentTypeWithCompositeKey.PrimaryKey(
                new[] { DependentEntityWithCompositeKey.NavPropIdProperty, DependentEntityWithCompositeKey.NavPropNameProperty },
                ConfigurationSource.Explicit);

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

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

            var fk = relationshipBuilder.Metadata;

            Assert.Same(fk, DependentTypeWithCompositeKey.Metadata.GetForeignKeys().Single());
            Assert.Null(fk.GetForeignKeyPropertiesConfigurationSource());
            Assert.Equal("NavProp" + CompositePrimaryKey[0].Name + "1", fk.Properties[0].Name);
            Assert.Equal("NavProp" + CompositePrimaryKey[1].Name + "1", fk.Properties[1].Name);
            Assert.Same(CompositePrimaryKey[0], fk.PrincipalKey.Properties[0]);
            Assert.Same(CompositePrimaryKey[1], fk.PrincipalKey.Properties[1]);
            Assert.False(fk.IsUnique);
            Assert.False(fk.IsRequired);
        }
        public void Matches_dependent_PK_for_unique_FK()
        {
            var fkProperty = DependentType.Metadata.GetPrimaryKey().Properties.Single();

            var relationshipBuilder = DependentType.Relationship(
                PrincipalType,
                DependentType,
                "SomeNav",
                "InverseReferenceNav",
                null,
                PrincipalType.Metadata.GetPrimaryKey().Properties,
                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_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_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 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());
        }
        public void Does_not_invert_if_both_are_referenced()
        {
            DependentType.Relationship(
                PrincipalType, DependentType, null, null, null, null, ConfigurationSource.Convention);
            DependentType.Relationship(
                DependentType, PrincipalType, null, null, null, null, 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.True(fk.IsRequired);
        }
        public void Matches_navigation_plus_Id_property()
        {
            var fkProperty = DependentType.Property(DependentEntity.SomeNavIDProperty, ConfigurationSource.Convention).Metadata;

            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_both_entity_types_have_navigations()
        {
            var relationshipBuilder = DependentType.Relationship(
                DependentType,
                PrincipalType,
                "InverseReferenceNav",
                "SomeNav",
                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)PrincipalType.Metadata.GetForeignKeys().Single();

            Assert.Same(fk, newRelationshipBuilder.Metadata);
            Assert.Same(DependentType.Metadata.GetPrimaryKey(), fk.PrincipalKey);
            Assert.True(fk.IsUnique);
            Assert.True(fk.IsRequired);
            Assert.Empty(DependentType.Metadata.GetForeignKeys());
        }
        public void Inverts_if_dependent_entity_type_is_referenced()
        {
            DependentType.Relationship(
                PrincipalType, DependentType, null, null, null, null, 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(DependentType.Metadata, newRelationshipBuilder.Metadata.DeclaringEntityType);

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

            var fk = (IForeignKey)newRelationshipBuilder.Metadata;

            Assert.Same(PrimaryKey, fk.PrincipalKey.Properties.Single());
            Assert.True(fk.IsUnique);
            Assert.True(fk.IsRequired);
            Assert.Empty(PrincipalType.Metadata.GetForeignKeys());
        }
        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 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);

            var fk = dependentTypeWithCompositeKey.Metadata.GetForeignKeys().Single();

            Assert.Same(fk, newRelationshipBuilder.Metadata);
            Assert.True(fk.IsUnique);
            Assert.Null(fk.GetForeignKeyPropertiesConfigurationSource());
            Assert.Same(principalTypeWithCompositeKey.Metadata.FindPrimaryKey(), fk.PrincipalKey);
            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);
        }
        public void Does_not_match_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.Equal(nameof(PrincipalEntity) + nameof(PrincipalEntity.PeeKay), relationshipBuilder.Metadata.Properties.Single().Name);

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

            var newFk = newRelationshipBuilder.Metadata;

            Assert.Equal(2, DependentType.Metadata.GetForeignKeys().Count());
            Assert.Equal("SomeNav" + nameof(PrincipalEntity.PeeKay), newFk.Properties.Single().Name);
            Assert.Null(newFk.GetForeignKeyPropertiesConfigurationSource());
        }
        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);
        }
示例#15
0
        public virtual ConventionSet CreateConventionSet()
        {
            var conventionSet = new ConventionSet();

            var relationshipDiscoveryConvention = new RelationshipDiscoveryConvention();

            conventionSet.EntityTypeAddedConventions.Add(new NotMappedEntityTypeAttributeConvention());
            conventionSet.EntityTypeAddedConventions.Add(new NotMappedMemberAttributeConvention());
            conventionSet.EntityTypeAddedConventions.Add(new PropertyDiscoveryConvention());
            conventionSet.EntityTypeAddedConventions.Add(new KeyDiscoveryConvention());
            conventionSet.EntityTypeAddedConventions.Add(new InversePropertyAttributeConvention());
            conventionSet.EntityTypeAddedConventions.Add(relationshipDiscoveryConvention);

            // An ambiguity might have been resolved
            conventionSet.EntityTypeMemberIgnoredConventions.Add(relationshipDiscoveryConvention);

            conventionSet.PropertyAddedConventions.Add(new ConcurrencyCheckAttributeConvention());
            conventionSet.PropertyAddedConventions.Add(new DatabaseGeneratedAttributeConvention());
            conventionSet.PropertyAddedConventions.Add(new RequiredPropertyAttributeConvention());
            conventionSet.PropertyAddedConventions.Add(new MaxLengthAttributeConvention());
            conventionSet.PropertyAddedConventions.Add(new StringLengthAttributeConvention());
            conventionSet.PropertyAddedConventions.Add(new TimestampAttributeConvention());

            var keyAttributeConvention = new KeyAttributeConvention();

            conventionSet.PropertyAddedConventions.Add(keyAttributeConvention);

            var keyConvention = new KeyConvention();

            conventionSet.KeyAddedConventions.Add(keyConvention);

            conventionSet.PrimaryKeySetConventions.Add(keyConvention);

            var foreignKeyPropertyDiscoveryConvention = new ForeignKeyPropertyDiscoveryConvention();

            conventionSet.ForeignKeyAddedConventions.Add(new ForeignKeyAttributeConvention());
            conventionSet.ForeignKeyAddedConventions.Add(foreignKeyPropertyDiscoveryConvention);

            conventionSet.ForeignKeyRemovedConventions.Add(keyConvention);

            conventionSet.ModelBuiltConventions.Add(new ModelCleanupConvention());
            conventionSet.ModelBuiltConventions.Add(keyAttributeConvention);

            conventionSet.NavigationAddedConventions.Add(new RequiredNavigationAttributeConvention());
            conventionSet.NavigationAddedConventions.Add(foreignKeyPropertyDiscoveryConvention);

            conventionSet.NavigationRemovedConventions.Add(relationshipDiscoveryConvention);

            return(conventionSet);
        }
        public virtual ConventionSet CreateConventionSet()
        {
            var conventionSet = new ConventionSet();

            var relationshipDiscoveryConvention = new RelationshipDiscoveryConvention();
            conventionSet.EntityTypeAddedConventions.Add(new NotMappedEntityTypeAttributeConvention());
            conventionSet.EntityTypeAddedConventions.Add(new NotMappedMemberAttributeConvention());
            conventionSet.EntityTypeAddedConventions.Add(new PropertyDiscoveryConvention());
            conventionSet.EntityTypeAddedConventions.Add(new KeyDiscoveryConvention());
            conventionSet.EntityTypeAddedConventions.Add(new InversePropertyAttributeConvention());
            conventionSet.EntityTypeAddedConventions.Add(relationshipDiscoveryConvention);

            // An ambiguity might have been resolved
            conventionSet.EntityTypeMemberIgnoredConventions.Add(relationshipDiscoveryConvention);

            conventionSet.PropertyAddedConventions.Add(new ConcurrencyCheckAttributeConvention());
            conventionSet.PropertyAddedConventions.Add(new DatabaseGeneratedAttributeConvention());
            conventionSet.PropertyAddedConventions.Add(new RequiredPropertyAttributeConvention());
            conventionSet.PropertyAddedConventions.Add(new MaxLengthAttributeConvention());
            conventionSet.PropertyAddedConventions.Add(new StringLengthAttributeConvention());
            conventionSet.PropertyAddedConventions.Add(new TimestampAttributeConvention());

            var keyAttributeConvention = new KeyAttributeConvention();
            conventionSet.PropertyAddedConventions.Add(keyAttributeConvention);

            var keyConvention = new KeyConvention();
            conventionSet.KeyAddedConventions.Add(keyConvention);

            conventionSet.PrimaryKeySetConventions.Add(keyConvention);
            
            var foreignKeyPropertyDiscoveryConvention = new ForeignKeyPropertyDiscoveryConvention();
            conventionSet.ForeignKeyAddedConventions.Add(new ForeignKeyAttributeConvention());
            conventionSet.ForeignKeyAddedConventions.Add(foreignKeyPropertyDiscoveryConvention);

            conventionSet.ForeignKeyRemovedConventions.Add(keyConvention);

            conventionSet.ModelBuiltConventions.Add(new ModelCleanupConvention());
            conventionSet.ModelBuiltConventions.Add(keyAttributeConvention);

            conventionSet.NavigationAddedConventions.Add(new RequiredNavigationAttributeConvention());
            conventionSet.NavigationAddedConventions.Add(foreignKeyPropertyDiscoveryConvention);
            
            conventionSet.NavigationRemovedConventions.Add(relationshipDiscoveryConvention);

            return conventionSet;
        }
        public void Returns_same_builder_if_no_matching_clr_properties_found()
        {
            var relationshipBuilder = DependentType.Relationship(
                PrincipalType,
                "SomeNav",
                null,
                ConfigurationSource.Convention);

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

            var fk = relationshipBuilder.Metadata;

            Assert.Same(fk, DependentType.Metadata.GetForeignKeys().Single());
            Assert.Null(fk.GetForeignKeyPropertiesConfigurationSource());
            Assert.Equal("SomeNav" + PrimaryKey.Name, fk.Properties.Single().Name);
            Assert.False(fk.IsUnique);
        }
        public void Does_not_match_properties_with_different_base_names()
        {
            DependentTypeWithCompositeKey.Property(DependentEntityWithCompositeKey.NavPropIdProperty, ConfigurationSource.Convention);
            DependentTypeWithCompositeKey.Property(DependentEntityWithCompositeKey.PrincipalEntityWithCompositeKeyNameProperty, ConfigurationSource.Convention);
            DependentTypeWithCompositeKey.Property(DependentEntityWithCompositeKey.IdProperty, ConfigurationSource.Convention);

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

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

            var fk = relationshipBuilder.Metadata;

            Assert.Same(fk, DependentTypeWithCompositeKey.Metadata.GetForeignKeys().Single());
            Assert.Null(fk.GetForeignKeyPropertiesConfigurationSource());
            Assert.Same(CompositePrimaryKey[0], fk.PrincipalKey.Properties[0]);
            Assert.Same(CompositePrimaryKey[1], fk.PrincipalKey.Properties[1]);
        }
        public void Does_nothing_if_matching_shadow_property_added()
        {
            var relationshipBuilder = DependentType.Relationship(PrincipalType, "SomeNav", null, ConfigurationSource.Convention);

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

            var fk = relationshipBuilder.Metadata;

            Assert.Same(fk, DependentType.Metadata.GetForeignKeys().Single());
            Assert.Null(fk.GetForeignKeyPropertiesConfigurationSource());
            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);

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

            var fk = relationshipBuilder.Metadata;

            Assert.Same(fk, DependentType.Metadata.GetForeignKeys().Single());
            Assert.Null(fk.GetForeignKeyPropertiesConfigurationSource());
            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 Does_not_invert_if_principal_entity_type_is_defining_the_dependent_entity_type()
        {
            PrincipalType.Property(nameof(PrincipalEntity.DependentEntityKayPee), ConfigurationSource.Convention);
            PrincipalType.Metadata.Model.RemoveEntityType(typeof(DependentEntity));
            var relationshipBuilder = PrincipalType.Owns(
                typeof(DependentEntity), nameof(PrincipalEntity.InverseReferenceNav), ConfigurationSource.Convention);
            var dependentTypeBuilder = relationshipBuilder.Metadata.DeclaringEntityType.Builder;

            dependentTypeBuilder.PrimaryKey(new[] { nameof(DependentEntity.KayPee) }, ConfigurationSource.Convention);

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

            Assert.Same(relationshipBuilder, newRelationshipBuilder);
            Assert.Same(dependentTypeBuilder.Metadata, newRelationshipBuilder.Metadata.DeclaringEntityType);

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

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

            Assert.Same(fk, newRelationshipBuilder.Metadata);
            Assert.Same(PrimaryKey, fk.PrincipalKey.Properties.Single());
            Assert.True(fk.IsUnique);
        }
        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);
        }
    /// <summary>
    ///     Builds and returns the convention set for the current database provider.
    /// </summary>
    /// <returns>The convention set for the current database provider.</returns>
    public virtual ConventionSet CreateConventionSet()
    {
        var conventionSet = new ConventionSet();

        var propertyDiscoveryConvention        = new PropertyDiscoveryConvention(Dependencies);
        var keyDiscoveryConvention             = new KeyDiscoveryConvention(Dependencies);
        var inversePropertyAttributeConvention = new InversePropertyAttributeConvention(Dependencies);
        var foreignKeyAttributeConvention      = new ForeignKeyAttributeConvention(Dependencies);
        var relationshipDiscoveryConvention    = new RelationshipDiscoveryConvention(Dependencies);
        var servicePropertyDiscoveryConvention = new ServicePropertyDiscoveryConvention(Dependencies);
        var indexAttributeConvention           = new IndexAttributeConvention(Dependencies);
        var baseTypeDiscoveryConvention        = new BaseTypeDiscoveryConvention(Dependencies);

        conventionSet.EntityTypeAddedConventions.Add(new NotMappedEntityTypeAttributeConvention(Dependencies));
        conventionSet.EntityTypeAddedConventions.Add(new OwnedEntityTypeAttributeConvention(Dependencies));
        conventionSet.EntityTypeAddedConventions.Add(new KeylessEntityTypeAttributeConvention(Dependencies));
        conventionSet.EntityTypeAddedConventions.Add(new EntityTypeConfigurationEntityTypeAttributeConvention(Dependencies));
        conventionSet.EntityTypeAddedConventions.Add(new NotMappedMemberAttributeConvention(Dependencies));
        conventionSet.EntityTypeAddedConventions.Add(baseTypeDiscoveryConvention);
        conventionSet.EntityTypeAddedConventions.Add(propertyDiscoveryConvention);
        conventionSet.EntityTypeAddedConventions.Add(servicePropertyDiscoveryConvention);
        conventionSet.EntityTypeAddedConventions.Add(keyDiscoveryConvention);
        conventionSet.EntityTypeAddedConventions.Add(indexAttributeConvention);
        conventionSet.EntityTypeAddedConventions.Add(inversePropertyAttributeConvention);
        conventionSet.EntityTypeAddedConventions.Add(foreignKeyAttributeConvention);
        conventionSet.EntityTypeAddedConventions.Add(relationshipDiscoveryConvention);

        conventionSet.EntityTypeIgnoredConventions.Add(relationshipDiscoveryConvention);

        var discriminatorConvention = new DiscriminatorConvention(Dependencies);

        conventionSet.EntityTypeRemovedConventions.Add(inversePropertyAttributeConvention);
        conventionSet.EntityTypeRemovedConventions.Add(discriminatorConvention);

        var foreignKeyIndexConvention = new ForeignKeyIndexConvention(Dependencies);
        var valueGeneratorConvention  = new ValueGenerationConvention(Dependencies);

        conventionSet.EntityTypeBaseTypeChangedConventions.Add(propertyDiscoveryConvention);
        conventionSet.EntityTypeBaseTypeChangedConventions.Add(servicePropertyDiscoveryConvention);
        conventionSet.EntityTypeBaseTypeChangedConventions.Add(keyDiscoveryConvention);
        conventionSet.EntityTypeBaseTypeChangedConventions.Add(indexAttributeConvention);
        conventionSet.EntityTypeBaseTypeChangedConventions.Add(inversePropertyAttributeConvention);
        conventionSet.EntityTypeBaseTypeChangedConventions.Add(relationshipDiscoveryConvention);
        conventionSet.EntityTypeBaseTypeChangedConventions.Add(foreignKeyIndexConvention);
        conventionSet.EntityTypeBaseTypeChangedConventions.Add(valueGeneratorConvention);
        conventionSet.EntityTypeBaseTypeChangedConventions.Add(discriminatorConvention);

        var foreignKeyPropertyDiscoveryConvention = new ForeignKeyPropertyDiscoveryConvention(Dependencies);

        conventionSet.EntityTypeMemberIgnoredConventions.Add(inversePropertyAttributeConvention);
        conventionSet.EntityTypeMemberIgnoredConventions.Add(relationshipDiscoveryConvention);
        conventionSet.EntityTypeMemberIgnoredConventions.Add(keyDiscoveryConvention);
        conventionSet.EntityTypeMemberIgnoredConventions.Add(foreignKeyPropertyDiscoveryConvention);

        var keyAttributeConvention = new KeyAttributeConvention(Dependencies);
        var backingFieldConvention = new BackingFieldConvention(Dependencies);
        var concurrencyCheckAttributeConvention    = new ConcurrencyCheckAttributeConvention(Dependencies);
        var databaseGeneratedAttributeConvention   = new DatabaseGeneratedAttributeConvention(Dependencies);
        var requiredPropertyAttributeConvention    = new RequiredPropertyAttributeConvention(Dependencies);
        var nonNullableReferencePropertyConvention = new NonNullableReferencePropertyConvention(Dependencies);
        var maxLengthAttributeConvention           = new MaxLengthAttributeConvention(Dependencies);
        var stringLengthAttributeConvention        = new StringLengthAttributeConvention(Dependencies);
        var timestampAttributeConvention           = new TimestampAttributeConvention(Dependencies);
        var backingFieldAttributeConvention        = new BackingFieldAttributeConvention(Dependencies);
        var unicodeAttributeConvention             = new UnicodeAttributeConvention(Dependencies);
        var precisionAttributeConvention           = new PrecisionAttributeConvention(Dependencies);

        conventionSet.PropertyAddedConventions.Add(backingFieldAttributeConvention);
        conventionSet.PropertyAddedConventions.Add(backingFieldConvention);
        conventionSet.PropertyAddedConventions.Add(concurrencyCheckAttributeConvention);
        conventionSet.PropertyAddedConventions.Add(databaseGeneratedAttributeConvention);
        conventionSet.PropertyAddedConventions.Add(requiredPropertyAttributeConvention);
        conventionSet.PropertyAddedConventions.Add(nonNullableReferencePropertyConvention);
        conventionSet.PropertyAddedConventions.Add(maxLengthAttributeConvention);
        conventionSet.PropertyAddedConventions.Add(stringLengthAttributeConvention);
        conventionSet.PropertyAddedConventions.Add(timestampAttributeConvention);
        conventionSet.PropertyAddedConventions.Add(keyAttributeConvention);
        conventionSet.PropertyAddedConventions.Add(keyDiscoveryConvention);
        conventionSet.PropertyAddedConventions.Add(foreignKeyPropertyDiscoveryConvention);
        conventionSet.PropertyAddedConventions.Add(unicodeAttributeConvention);
        conventionSet.PropertyAddedConventions.Add(precisionAttributeConvention);

        conventionSet.EntityTypePrimaryKeyChangedConventions.Add(foreignKeyPropertyDiscoveryConvention);
        conventionSet.EntityTypePrimaryKeyChangedConventions.Add(valueGeneratorConvention);

        conventionSet.KeyAddedConventions.Add(foreignKeyPropertyDiscoveryConvention);
        conventionSet.KeyAddedConventions.Add(foreignKeyIndexConvention);

        conventionSet.KeyRemovedConventions.Add(foreignKeyPropertyDiscoveryConvention);
        conventionSet.KeyRemovedConventions.Add(foreignKeyIndexConvention);
        conventionSet.KeyRemovedConventions.Add(keyDiscoveryConvention);

        var cascadeDeleteConvention = new CascadeDeleteConvention(Dependencies);

        conventionSet.ForeignKeyAddedConventions.Add(foreignKeyAttributeConvention);
        conventionSet.ForeignKeyAddedConventions.Add(foreignKeyPropertyDiscoveryConvention);
        conventionSet.ForeignKeyAddedConventions.Add(keyDiscoveryConvention);
        conventionSet.ForeignKeyAddedConventions.Add(valueGeneratorConvention);
        conventionSet.ForeignKeyAddedConventions.Add(cascadeDeleteConvention);
        conventionSet.ForeignKeyAddedConventions.Add(foreignKeyIndexConvention);

        conventionSet.ForeignKeyRemovedConventions.Add(baseTypeDiscoveryConvention);
        conventionSet.ForeignKeyRemovedConventions.Add(relationshipDiscoveryConvention);
        conventionSet.ForeignKeyRemovedConventions.Add(keyDiscoveryConvention);
        conventionSet.ForeignKeyRemovedConventions.Add(valueGeneratorConvention);
        conventionSet.ForeignKeyRemovedConventions.Add(foreignKeyIndexConvention);

        conventionSet.ForeignKeyPropertiesChangedConventions.Add(foreignKeyPropertyDiscoveryConvention);
        conventionSet.ForeignKeyPropertiesChangedConventions.Add(keyDiscoveryConvention);
        conventionSet.ForeignKeyPropertiesChangedConventions.Add(valueGeneratorConvention);
        conventionSet.ForeignKeyPropertiesChangedConventions.Add(foreignKeyIndexConvention);

        conventionSet.ForeignKeyUniquenessChangedConventions.Add(foreignKeyPropertyDiscoveryConvention);
        conventionSet.ForeignKeyUniquenessChangedConventions.Add(keyDiscoveryConvention);
        conventionSet.ForeignKeyUniquenessChangedConventions.Add(foreignKeyIndexConvention);

        conventionSet.ForeignKeyRequirednessChangedConventions.Add(cascadeDeleteConvention);
        conventionSet.ForeignKeyRequirednessChangedConventions.Add(foreignKeyPropertyDiscoveryConvention);

        conventionSet.ForeignKeyOwnershipChangedConventions.Add(new NavigationEagerLoadingConvention(Dependencies));
        conventionSet.ForeignKeyOwnershipChangedConventions.Add(relationshipDiscoveryConvention);
        conventionSet.ForeignKeyOwnershipChangedConventions.Add(keyDiscoveryConvention);
        conventionSet.ForeignKeyOwnershipChangedConventions.Add(valueGeneratorConvention);

        conventionSet.ForeignKeyNullNavigationSetConventions.Add(relationshipDiscoveryConvention);

        var requiredNavigationAttributeConvention = new RequiredNavigationAttributeConvention(Dependencies);
        var nonNullableNavigationConvention       = new NonNullableNavigationConvention(Dependencies);

        conventionSet.NavigationAddedConventions.Add(new NavigationBackingFieldAttributeConvention(Dependencies));
        conventionSet.NavigationAddedConventions.Add(backingFieldConvention);
        conventionSet.NavigationAddedConventions.Add(requiredNavigationAttributeConvention);
        conventionSet.NavigationAddedConventions.Add(nonNullableNavigationConvention);
        conventionSet.NavigationAddedConventions.Add(inversePropertyAttributeConvention);
        conventionSet.NavigationAddedConventions.Add(foreignKeyPropertyDiscoveryConvention);
        conventionSet.NavigationAddedConventions.Add(relationshipDiscoveryConvention);
        conventionSet.NavigationAddedConventions.Add(foreignKeyAttributeConvention);

        var manyToManyJoinEntityTypeConvention = new ManyToManyJoinEntityTypeConvention(Dependencies);

        conventionSet.SkipNavigationAddedConventions.Add(new NavigationBackingFieldAttributeConvention(Dependencies));
        conventionSet.SkipNavigationAddedConventions.Add(backingFieldConvention);
        conventionSet.SkipNavigationAddedConventions.Add(manyToManyJoinEntityTypeConvention);

        conventionSet.SkipNavigationRemovedConventions.Add(manyToManyJoinEntityTypeConvention);

        conventionSet.SkipNavigationInverseChangedConventions.Add(manyToManyJoinEntityTypeConvention);
        conventionSet.SkipNavigationInverseChangedConventions.Add(foreignKeyPropertyDiscoveryConvention);

        conventionSet.SkipNavigationForeignKeyChangedConventions.Add(manyToManyJoinEntityTypeConvention);
        conventionSet.SkipNavigationForeignKeyChangedConventions.Add(foreignKeyAttributeConvention);
        conventionSet.SkipNavigationForeignKeyChangedConventions.Add(keyDiscoveryConvention);
        conventionSet.SkipNavigationForeignKeyChangedConventions.Add(foreignKeyPropertyDiscoveryConvention);

        conventionSet.NavigationRemovedConventions.Add(relationshipDiscoveryConvention);

        conventionSet.IndexAddedConventions.Add(foreignKeyIndexConvention);

        conventionSet.IndexRemovedConventions.Add(foreignKeyIndexConvention);

        conventionSet.IndexUniquenessChangedConventions.Add(foreignKeyIndexConvention);

        conventionSet.ForeignKeyPrincipalEndChangedConventions.Add(foreignKeyPropertyDiscoveryConvention);
        conventionSet.ForeignKeyPrincipalEndChangedConventions.Add(requiredNavigationAttributeConvention);
        conventionSet.ForeignKeyPrincipalEndChangedConventions.Add(nonNullableNavigationConvention);

        conventionSet.PropertyNullabilityChangedConventions.Add(foreignKeyPropertyDiscoveryConvention);

        conventionSet.PropertyFieldChangedConventions.Add(foreignKeyPropertyDiscoveryConvention);
        conventionSet.PropertyFieldChangedConventions.Add(keyAttributeConvention);
        conventionSet.PropertyFieldChangedConventions.Add(concurrencyCheckAttributeConvention);
        conventionSet.PropertyFieldChangedConventions.Add(databaseGeneratedAttributeConvention);
        conventionSet.PropertyFieldChangedConventions.Add(requiredPropertyAttributeConvention);
        conventionSet.PropertyFieldChangedConventions.Add(nonNullableReferencePropertyConvention);
        conventionSet.PropertyFieldChangedConventions.Add(maxLengthAttributeConvention);
        conventionSet.PropertyFieldChangedConventions.Add(stringLengthAttributeConvention);
        conventionSet.PropertyFieldChangedConventions.Add(timestampAttributeConvention);

        conventionSet.ModelInitializedConventions.Add(new DbSetFindingConvention(Dependencies));

        conventionSet.ModelFinalizingConventions.Add(new ModelCleanupConvention(Dependencies));
        conventionSet.ModelFinalizingConventions.Add(keyAttributeConvention);
        conventionSet.ModelFinalizingConventions.Add(indexAttributeConvention);
        conventionSet.ModelFinalizingConventions.Add(foreignKeyAttributeConvention);
        conventionSet.ModelFinalizingConventions.Add(new ChangeTrackingStrategyConvention(Dependencies));
        conventionSet.ModelFinalizingConventions.Add(new ConstructorBindingConvention(Dependencies));
        conventionSet.ModelFinalizingConventions.Add(foreignKeyIndexConvention);
        conventionSet.ModelFinalizingConventions.Add(foreignKeyPropertyDiscoveryConvention);
        conventionSet.ModelFinalizingConventions.Add(nonNullableReferencePropertyConvention);
        conventionSet.ModelFinalizingConventions.Add(nonNullableNavigationConvention);
        conventionSet.ModelFinalizingConventions.Add(new QueryFilterRewritingConvention(Dependencies));
        conventionSet.ModelFinalizingConventions.Add(inversePropertyAttributeConvention);
        conventionSet.ModelFinalizingConventions.Add(backingFieldConvention);

        conventionSet.ModelFinalizedConventions.Add(new RuntimeModelConvention(Dependencies));

        return(conventionSet);
    }
示例#24
0
        /// <summary>
        ///     Builds and returns the convention set for the current database provider.
        /// </summary>
        /// <returns> The convention set for the current database provider. </returns>
        public virtual ConventionSet CreateConventionSet()
        {
            var conventionSet = new ConventionSet();
            var logger        = Dependencies.Logger;

            var propertyDiscoveryConvention
                = new PropertyDiscoveryConvention(
                      Dependencies.TypeMappingSource, logger);

            var keyDiscoveryConvention
                = new KeyDiscoveryConvention(logger);

            var inversePropertyAttributeConvention
                = new InversePropertyAttributeConvention(Dependencies.MemberClassifier, logger);

            var relationshipDiscoveryConvention
                = new RelationshipDiscoveryConvention(Dependencies.MemberClassifier, logger);

            var servicePropertyDiscoveryConvention
                = new ServicePropertyDiscoveryConvention(Dependencies.TypeMappingSource, Dependencies.ParameterBindingFactories, logger);

            conventionSet.EntityTypeAddedConventions.Add(new NotMappedEntityTypeAttributeConvention(logger));
            conventionSet.EntityTypeAddedConventions.Add(new OwnedEntityTypeAttributeConvention(logger));
            conventionSet.EntityTypeAddedConventions.Add(new NotMappedMemberAttributeConvention(logger));
            conventionSet.EntityTypeAddedConventions.Add(new BaseTypeDiscoveryConvention(logger));
            conventionSet.EntityTypeAddedConventions.Add(propertyDiscoveryConvention);
            conventionSet.EntityTypeAddedConventions.Add(servicePropertyDiscoveryConvention);
            conventionSet.EntityTypeAddedConventions.Add(keyDiscoveryConvention);
            conventionSet.EntityTypeAddedConventions.Add(inversePropertyAttributeConvention);
            conventionSet.EntityTypeAddedConventions.Add(relationshipDiscoveryConvention);
            conventionSet.EntityTypeAddedConventions.Add(new DerivedTypeDiscoveryConvention(logger));

            conventionSet.EntityTypeIgnoredConventions.Add(inversePropertyAttributeConvention);

            var discriminatorConvention = new DiscriminatorConvention(logger);

            conventionSet.EntityTypeRemovedConventions.Add(new OwnedTypesConvention(logger));
            conventionSet.EntityTypeRemovedConventions.Add(discriminatorConvention);

            var foreignKeyIndexConvention = new ForeignKeyIndexConvention(logger);
            var valueGeneratorConvention  = new ValueGeneratorConvention(logger);

            conventionSet.EntityTypeBaseTypeChangedConventions.Add(propertyDiscoveryConvention);
            conventionSet.EntityTypeBaseTypeChangedConventions.Add(servicePropertyDiscoveryConvention);
            conventionSet.EntityTypeBaseTypeChangedConventions.Add(keyDiscoveryConvention);
            conventionSet.EntityTypeBaseTypeChangedConventions.Add(inversePropertyAttributeConvention);
            conventionSet.EntityTypeBaseTypeChangedConventions.Add(relationshipDiscoveryConvention);
            conventionSet.EntityTypeBaseTypeChangedConventions.Add(foreignKeyIndexConvention);
            conventionSet.EntityTypeBaseTypeChangedConventions.Add(valueGeneratorConvention);
            conventionSet.EntityTypeBaseTypeChangedConventions.Add(discriminatorConvention);

            var foreignKeyPropertyDiscoveryConvention = new ForeignKeyPropertyDiscoveryConvention(logger);

            conventionSet.EntityTypeMemberIgnoredConventions.Add(inversePropertyAttributeConvention);
            conventionSet.EntityTypeMemberIgnoredConventions.Add(relationshipDiscoveryConvention);
            conventionSet.EntityTypeMemberIgnoredConventions.Add(foreignKeyPropertyDiscoveryConvention);
            conventionSet.EntityTypeMemberIgnoredConventions.Add(servicePropertyDiscoveryConvention);

            var keyAttributeConvention = new KeyAttributeConvention(logger);
            var backingFieldConvention = new BackingFieldConvention(logger);
            var concurrencyCheckAttributeConvention    = new ConcurrencyCheckAttributeConvention(logger);
            var databaseGeneratedAttributeConvention   = new DatabaseGeneratedAttributeConvention(logger);
            var requiredPropertyAttributeConvention    = new RequiredPropertyAttributeConvention(logger);
            var nonNullableReferencePropertyConvention = new NonNullableReferencePropertyConvention(logger);
            var maxLengthAttributeConvention           = new MaxLengthAttributeConvention(logger);
            var stringLengthAttributeConvention        = new StringLengthAttributeConvention(logger);
            var timestampAttributeConvention           = new TimestampAttributeConvention(logger);

            conventionSet.PropertyAddedConventions.Add(backingFieldConvention);
            conventionSet.PropertyAddedConventions.Add(concurrencyCheckAttributeConvention);
            conventionSet.PropertyAddedConventions.Add(databaseGeneratedAttributeConvention);
            conventionSet.PropertyAddedConventions.Add(requiredPropertyAttributeConvention);
            conventionSet.PropertyAddedConventions.Add(nonNullableReferencePropertyConvention);
            conventionSet.PropertyAddedConventions.Add(maxLengthAttributeConvention);
            conventionSet.PropertyAddedConventions.Add(stringLengthAttributeConvention);
            conventionSet.PropertyAddedConventions.Add(timestampAttributeConvention);
            conventionSet.PropertyAddedConventions.Add(keyAttributeConvention);
            conventionSet.PropertyAddedConventions.Add(keyDiscoveryConvention);
            conventionSet.PropertyAddedConventions.Add(foreignKeyPropertyDiscoveryConvention);

            conventionSet.EntityTypePrimaryKeyChangedConventions.Add(foreignKeyPropertyDiscoveryConvention);
            conventionSet.EntityTypePrimaryKeyChangedConventions.Add(valueGeneratorConvention);

            conventionSet.KeyAddedConventions.Add(foreignKeyPropertyDiscoveryConvention);
            conventionSet.KeyAddedConventions.Add(foreignKeyIndexConvention);

            conventionSet.KeyRemovedConventions.Add(foreignKeyPropertyDiscoveryConvention);
            conventionSet.KeyRemovedConventions.Add(foreignKeyIndexConvention);
            conventionSet.KeyRemovedConventions.Add(keyDiscoveryConvention);

            var cascadeDeleteConvention       = new CascadeDeleteConvention(logger);
            var foreignKeyAttributeConvention = new ForeignKeyAttributeConvention(Dependencies.MemberClassifier, logger);

            conventionSet.ForeignKeyAddedConventions.Add(foreignKeyAttributeConvention);
            conventionSet.ForeignKeyAddedConventions.Add(foreignKeyPropertyDiscoveryConvention);
            conventionSet.ForeignKeyAddedConventions.Add(keyDiscoveryConvention);
            conventionSet.ForeignKeyAddedConventions.Add(valueGeneratorConvention);
            conventionSet.ForeignKeyAddedConventions.Add(cascadeDeleteConvention);
            conventionSet.ForeignKeyAddedConventions.Add(foreignKeyIndexConvention);

            conventionSet.ForeignKeyRemovedConventions.Add(keyDiscoveryConvention);
            conventionSet.ForeignKeyRemovedConventions.Add(valueGeneratorConvention);
            conventionSet.ForeignKeyRemovedConventions.Add(foreignKeyIndexConvention);

            conventionSet.ForeignKeyPropertiesChangedConventions.Add(foreignKeyPropertyDiscoveryConvention);
            conventionSet.ForeignKeyPropertiesChangedConventions.Add(keyDiscoveryConvention);
            conventionSet.ForeignKeyPropertiesChangedConventions.Add(valueGeneratorConvention);
            conventionSet.ForeignKeyPropertiesChangedConventions.Add(foreignKeyIndexConvention);

            conventionSet.ForeignKeyUniquenessChangedConventions.Add(foreignKeyPropertyDiscoveryConvention);
            conventionSet.ForeignKeyUniquenessChangedConventions.Add(keyDiscoveryConvention);
            conventionSet.ForeignKeyUniquenessChangedConventions.Add(foreignKeyIndexConvention);

            conventionSet.ForeignKeyRequirednessChangedConventions.Add(cascadeDeleteConvention);
            conventionSet.ForeignKeyRequirednessChangedConventions.Add(foreignKeyPropertyDiscoveryConvention);

            conventionSet.ForeignKeyOwnershipChangedConventions.Add(new NavigationEagerLoadingConvention(logger));
            conventionSet.ForeignKeyOwnershipChangedConventions.Add(keyDiscoveryConvention);
            conventionSet.ForeignKeyOwnershipChangedConventions.Add(relationshipDiscoveryConvention);

            conventionSet.ModelFinalizedConventions.Add(new ModelCleanupConvention(logger));
            conventionSet.ModelFinalizedConventions.Add(keyAttributeConvention);
            conventionSet.ModelFinalizedConventions.Add(foreignKeyAttributeConvention);
            conventionSet.ModelFinalizedConventions.Add(new ChangeTrackingStrategyConvention(logger));
            conventionSet.ModelFinalizedConventions.Add(new ConstructorBindingConvention(Dependencies.ConstructorBindingFactory, logger));
            conventionSet.ModelFinalizedConventions.Add(new TypeMappingConvention(Dependencies.TypeMappingSource, logger));
            conventionSet.ModelFinalizedConventions.Add(foreignKeyIndexConvention);

            conventionSet.ModelFinalizedConventions.Add(foreignKeyPropertyDiscoveryConvention);
            conventionSet.ModelFinalizedConventions.Add(servicePropertyDiscoveryConvention);

            conventionSet.NavigationAddedConventions.Add(backingFieldConvention);
            conventionSet.NavigationAddedConventions.Add(new RequiredNavigationAttributeConvention(logger));
            conventionSet.NavigationAddedConventions.Add(new NonNullableNavigationConvention(logger));
            conventionSet.NavigationAddedConventions.Add(inversePropertyAttributeConvention);
            conventionSet.NavigationAddedConventions.Add(foreignKeyPropertyDiscoveryConvention);
            conventionSet.NavigationAddedConventions.Add(relationshipDiscoveryConvention);

            conventionSet.NavigationRemovedConventions.Add(relationshipDiscoveryConvention);

            conventionSet.IndexAddedConventions.Add(foreignKeyIndexConvention);

            conventionSet.IndexRemovedConventions.Add(foreignKeyIndexConvention);

            conventionSet.IndexUniquenessChangedConventions.Add(foreignKeyIndexConvention);

            conventionSet.ForeignKeyPrincipalEndChangedConventions.Add(foreignKeyPropertyDiscoveryConvention);

            conventionSet.PropertyNullabilityChangedConventions.Add(foreignKeyPropertyDiscoveryConvention);

            conventionSet.PropertyFieldChangedConventions.Add(keyDiscoveryConvention);
            conventionSet.PropertyFieldChangedConventions.Add(foreignKeyPropertyDiscoveryConvention);
            conventionSet.PropertyFieldChangedConventions.Add(keyAttributeConvention);
            conventionSet.PropertyFieldChangedConventions.Add(concurrencyCheckAttributeConvention);
            conventionSet.PropertyFieldChangedConventions.Add(databaseGeneratedAttributeConvention);
            conventionSet.PropertyFieldChangedConventions.Add(requiredPropertyAttributeConvention);
            conventionSet.PropertyFieldChangedConventions.Add(nonNullableReferencePropertyConvention);
            conventionSet.PropertyFieldChangedConventions.Add(maxLengthAttributeConvention);
            conventionSet.PropertyFieldChangedConventions.Add(stringLengthAttributeConvention);
            conventionSet.PropertyFieldChangedConventions.Add(timestampAttributeConvention);

            return(conventionSet);
        }
        /// <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>
        public virtual ConventionSet CreateConventionSet()
        {
            var conventionSet = new ConventionSet();

            var propertyDiscoveryConvention        = new PropertyDiscoveryConvention();
            var keyDiscoveryConvention             = new KeyDiscoveryConvention();
            var inversePropertyAttributeConvention = new InversePropertyAttributeConvention();
            var relationshipDiscoveryConvention    = new RelationshipDiscoveryConvention();

            conventionSet.EntityTypeAddedConventions.Add(new NotMappedEntityTypeAttributeConvention());
            conventionSet.EntityTypeAddedConventions.Add(new NotMappedMemberAttributeConvention());
            conventionSet.EntityTypeAddedConventions.Add(new BaseTypeDiscoveryConvention());
            conventionSet.EntityTypeAddedConventions.Add(propertyDiscoveryConvention);
            conventionSet.EntityTypeAddedConventions.Add(keyDiscoveryConvention);
            conventionSet.EntityTypeAddedConventions.Add(inversePropertyAttributeConvention);
            conventionSet.EntityTypeAddedConventions.Add(relationshipDiscoveryConvention);
            conventionSet.EntityTypeAddedConventions.Add(new DerivedTypeDiscoveryConvention());

            conventionSet.EntityTypeIgnoredConventions.Add(inversePropertyAttributeConvention);

            var foreignKeyIndexConvention = new ForeignKeyIndexConvention();

            conventionSet.BaseEntityTypeSetConventions.Add(propertyDiscoveryConvention);
            conventionSet.BaseEntityTypeSetConventions.Add(keyDiscoveryConvention);
            conventionSet.BaseEntityTypeSetConventions.Add(inversePropertyAttributeConvention);
            conventionSet.BaseEntityTypeSetConventions.Add(relationshipDiscoveryConvention);
            conventionSet.BaseEntityTypeSetConventions.Add(foreignKeyIndexConvention);

            // An ambiguity might have been resolved
            conventionSet.EntityTypeMemberIgnoredConventions.Add(inversePropertyAttributeConvention);
            conventionSet.EntityTypeMemberIgnoredConventions.Add(relationshipDiscoveryConvention);

            var keyAttributeConvention = new KeyAttributeConvention();
            var foreignKeyPropertyDiscoveryConvention = new ForeignKeyPropertyDiscoveryConvention();
            var backingFieldConvention = new BackingFieldConvention();
            var concurrencyCheckAttributeConvention  = new ConcurrencyCheckAttributeConvention();
            var databaseGeneratedAttributeConvention = new DatabaseGeneratedAttributeConvention();
            var requiredPropertyAttributeConvention  = new RequiredPropertyAttributeConvention();
            var maxLengthAttributeConvention         = new MaxLengthAttributeConvention();
            var stringLengthAttributeConvention      = new StringLengthAttributeConvention();
            var timestampAttributeConvention         = new TimestampAttributeConvention();

            conventionSet.PropertyAddedConventions.Add(backingFieldConvention);
            conventionSet.PropertyAddedConventions.Add(concurrencyCheckAttributeConvention);
            conventionSet.PropertyAddedConventions.Add(databaseGeneratedAttributeConvention);
            conventionSet.PropertyAddedConventions.Add(requiredPropertyAttributeConvention);
            conventionSet.PropertyAddedConventions.Add(maxLengthAttributeConvention);
            conventionSet.PropertyAddedConventions.Add(stringLengthAttributeConvention);
            conventionSet.PropertyAddedConventions.Add(timestampAttributeConvention);
            conventionSet.PropertyAddedConventions.Add(keyDiscoveryConvention);
            conventionSet.PropertyAddedConventions.Add(foreignKeyPropertyDiscoveryConvention);
            conventionSet.PropertyAddedConventions.Add(keyAttributeConvention);

            var keyConvention = new KeyConvention();

            conventionSet.PrimaryKeySetConventions.Add(keyConvention);

            conventionSet.KeyAddedConventions.Add(foreignKeyPropertyDiscoveryConvention);
            conventionSet.KeyAddedConventions.Add(foreignKeyIndexConvention);

            conventionSet.KeyRemovedConventions.Add(foreignKeyPropertyDiscoveryConvention);
            conventionSet.KeyRemovedConventions.Add(foreignKeyIndexConvention);

            var cascadeDeleteConvention = new CascadeDeleteConvention();

            conventionSet.ForeignKeyAddedConventions.Add(new ForeignKeyAttributeConvention());
            conventionSet.ForeignKeyAddedConventions.Add(foreignKeyPropertyDiscoveryConvention);
            conventionSet.ForeignKeyAddedConventions.Add(keyConvention);
            conventionSet.ForeignKeyAddedConventions.Add(cascadeDeleteConvention);
            conventionSet.ForeignKeyAddedConventions.Add(foreignKeyIndexConvention);

            conventionSet.ForeignKeyRemovedConventions.Add(keyConvention);
            conventionSet.ForeignKeyRemovedConventions.Add(foreignKeyIndexConvention);

            conventionSet.ForeignKeyUniquenessConventions.Add(foreignKeyPropertyDiscoveryConvention);
            conventionSet.ForeignKeyUniquenessConventions.Add(foreignKeyIndexConvention);

            conventionSet.ModelBuiltConventions.Add(new ModelCleanupConvention());
            conventionSet.ModelBuiltConventions.Add(keyAttributeConvention);
            conventionSet.ModelBuiltConventions.Add(new IgnoredMembersValidationConvention());
            conventionSet.ModelBuiltConventions.Add(new PropertyMappingValidationConvention());
            conventionSet.ModelBuiltConventions.Add(new RelationshipValidationConvention());

            conventionSet.NavigationAddedConventions.Add(backingFieldConvention);
            conventionSet.NavigationAddedConventions.Add(new RequiredNavigationAttributeConvention());
            conventionSet.NavigationAddedConventions.Add(inversePropertyAttributeConvention);
            conventionSet.NavigationAddedConventions.Add(foreignKeyPropertyDiscoveryConvention);
            conventionSet.NavigationAddedConventions.Add(relationshipDiscoveryConvention);

            conventionSet.NavigationRemovedConventions.Add(relationshipDiscoveryConvention);

            conventionSet.IndexAddedConventions.Add(foreignKeyIndexConvention);

            conventionSet.IndexRemovedConventions.Add(foreignKeyIndexConvention);

            conventionSet.IndexUniquenessConventions.Add(foreignKeyIndexConvention);

            conventionSet.PropertyNullableChangedConventions.Add(cascadeDeleteConvention);

            conventionSet.PrincipalEndSetConventions.Add(foreignKeyPropertyDiscoveryConvention);

            conventionSet.PropertyFieldChangedConventions.Add(keyDiscoveryConvention);
            conventionSet.PropertyFieldChangedConventions.Add(foreignKeyPropertyDiscoveryConvention);
            conventionSet.PropertyFieldChangedConventions.Add(keyAttributeConvention);
            conventionSet.PropertyFieldChangedConventions.Add(concurrencyCheckAttributeConvention);
            conventionSet.PropertyFieldChangedConventions.Add(databaseGeneratedAttributeConvention);
            conventionSet.PropertyFieldChangedConventions.Add(requiredPropertyAttributeConvention);
            conventionSet.PropertyFieldChangedConventions.Add(maxLengthAttributeConvention);
            conventionSet.PropertyFieldChangedConventions.Add(stringLengthAttributeConvention);
            conventionSet.PropertyFieldChangedConventions.Add(timestampAttributeConvention);

            return(conventionSet);
        }