public void Apply_MultiForeignKeysOnForeignKeyProperty_Works() { // Arrange ODataConventionModelBuilder builder = ODataConventionModelBuilderFactory.Create(); builder.EntityType <PrincipalEntity>().HasKey(p => new { p.PrincipalStringId, p.PrincipalIntId }); EntityTypeConfiguration dependentEntity = builder.AddEntityType(typeof(MultiDependentEntity2)); PropertyInfo expectPropertyInfo1 = typeof(MultiDependentEntity2).GetProperty("PrincipalId1"); PrimitivePropertyConfiguration propertyConfig1 = dependentEntity.AddProperty(expectPropertyInfo1); PropertyInfo expectPropertyInfo2 = typeof(MultiDependentEntity2).GetProperty("PrincipalId2"); PrimitivePropertyConfiguration propertyConfig2 = dependentEntity.AddProperty(expectPropertyInfo2); PropertyInfo propertyInfo = typeof(MultiDependentEntity2).GetProperty("Principal"); NavigationPropertyConfiguration navigation = dependentEntity.AddNavigationProperty(propertyInfo, EdmMultiplicity.One); navigation.AddedExplicitly = false; // Act ForeignKeyAttributeConvention convention = new ForeignKeyAttributeConvention(); convention.Apply(propertyConfig1, dependentEntity, builder); convention.Apply(propertyConfig2, dependentEntity, builder); // Assert Assert.Equal(2, navigation.DependentProperties.Count()); Assert.Same(expectPropertyInfo1, navigation.DependentProperties.First()); Assert.Same(expectPropertyInfo2, navigation.DependentProperties.Last()); Assert.Equal(2, navigation.PrincipalProperties.Count()); Assert.Equal("PrincipalIntId", navigation.PrincipalProperties.First().Name); Assert.Equal("PrincipalStringId", navigation.PrincipalProperties.Last().Name); }
public void ForeignKeyAttribute_sets_composite_foreign_key_properties_when_applied_on_navigation() { var dependentEntityTypeBuilder = CreateInternalEntityTypeBuilder <Dependent>(); var principalEntityTypeBuilder = dependentEntityTypeBuilder.ModelBuilder.Entity(typeof(Principal), ConfigurationSource.Convention); var relationshipBuilder = dependentEntityTypeBuilder.Relationship( principalEntityTypeBuilder, "CompositePrincipal", "Dependent", ConfigurationSource.Convention) .HasForeignKey(dependentEntityTypeBuilder.GetOrCreateProperties( new List <PropertyInfo> { Dependent.PrincipalIdProperty }, ConfigurationSource.Convention), ConfigurationSource.Convention); Assert.Equal("PrincipalId", relationshipBuilder.Metadata.Properties.First().Name); relationshipBuilder = new ForeignKeyAttributeConvention().Apply(relationshipBuilder); Assert.Equal(2, relationshipBuilder.Metadata.Properties.Count); Assert.Collection( relationshipBuilder.Metadata.Properties, p => Assert.Equal("PrincipalId", p.Name), p => Assert.Equal("PrincipalFk", p.Name)); }
public void ForeignKeyAttribute_sets_foreign_key_properties_after_inverting_when_applied_on_property_on_principal_side() { var dependentEntityTypeBuilder = CreateInternalEntityTypeBuilder <Principal>(); var principalEntityTypeBuilder = dependentEntityTypeBuilder.ModelBuilder.Entity(typeof(Dependent), ConfigurationSource.Convention); var relationshipBuilder = dependentEntityTypeBuilder.Relationship( principalEntityTypeBuilder, "Dependent", "AnotherPrincipal", ConfigurationSource.Convention) .HasForeignKey(dependentEntityTypeBuilder.GetOrCreateProperties( new List <PropertyInfo> { Principal.DependentIdProperty }, ConfigurationSource.Convention), ConfigurationSource.Convention); Assert.Equal("DependentId", relationshipBuilder.Metadata.Properties.First().Name); Assert.Equal(typeof(Principal), relationshipBuilder.Metadata.DeclaringEntityType.ClrType); Assert.Equal(typeof(Dependent), relationshipBuilder.Metadata.PrincipalEntityType.ClrType); relationshipBuilder = new ForeignKeyAttributeConvention().Apply(relationshipBuilder); Assert.Equal("PrincipalAnotherFk", relationshipBuilder.Metadata.Properties.First().Name); Assert.Equal(typeof(Dependent), relationshipBuilder.Metadata.DeclaringEntityType.ClrType); Assert.Equal(typeof(Principal), relationshipBuilder.Metadata.PrincipalEntityType.ClrType); }
private void ApplyForeignKeyConventions() { ForeignKeyAttributeConvention foreignKeyAttributeConvention = new ForeignKeyAttributeConvention(); ForeignKeyDiscoveryConvention foreignKeyDiscoveryConvention = new ForeignKeyDiscoveryConvention(); ActionOnDeleteAttributeConvention actionOnDeleteConvention = new ActionOnDeleteAttributeConvention(); foreach (EntityTypeConfiguration edmType in StructuralTypes.OfType <EntityTypeConfiguration>()) { foreach (PropertyConfiguration property in edmType.Properties) { // ForeignKeyDiscoveryConvention has to run after ForeignKeyAttributeConvention foreignKeyAttributeConvention.Apply(property, edmType, this); foreignKeyDiscoveryConvention.Apply(property, edmType, this); actionOnDeleteConvention.Apply(property, edmType, this); } } }
public void ForeignKeyAttribute_sets_foreign_key_properties_when_applied_on_principal_to_dependent_navigation() { var dependentEntityTypeBuilder = CreateInternalEntityTypeBuilder <Dependent>(); var principalEntityTypeBuilder = dependentEntityTypeBuilder.ModelBuilder.Entity(typeof(Principal), ConfigurationSource.Convention); var relationshipBuilder = dependentEntityTypeBuilder.Relationship( principalEntityTypeBuilder, dependentEntityTypeBuilder, "AnotherPrincipal", "Dependent", dependentEntityTypeBuilder.GetOrCreateProperties(new List <PropertyInfo> { Dependent.PrincipalIdProperty }, ConfigurationSource.Convention), null, ConfigurationSource.Convention); Assert.Equal("PrincipalId", relationshipBuilder.Metadata.Properties.First().Name); relationshipBuilder = new ForeignKeyAttributeConvention().Apply(relationshipBuilder); Assert.Equal("PrincipalAnotherFk", relationshipBuilder.Metadata.Properties.First().Name); }
public void ForeignKeyAttribute_does_not_override_configuration_from_explicit_source() { var dependentEntityTypeBuilder = CreateInternalEntityTypeBuilder <Dependent>(); var principalEntityTypeBuilder = dependentEntityTypeBuilder.ModelBuilder.Entity(typeof(Principal), ConfigurationSource.Convention); var relationshipBuilder = dependentEntityTypeBuilder.Relationship( principalEntityTypeBuilder, dependentEntityTypeBuilder, "Principal", "Dependent", dependentEntityTypeBuilder.GetOrCreateProperties(new List <PropertyInfo> { Dependent.PrincipalIdProperty }, ConfigurationSource.Convention), null, ConfigurationSource.Explicit); Assert.Equal("PrincipalId", relationshipBuilder.Metadata.Properties.First().Name); relationshipBuilder = new ForeignKeyAttributeConvention().Apply(relationshipBuilder); Assert.Equal("PrincipalId", relationshipBuilder.Metadata.Properties.First().Name); }
public void ForeignKeyAttribute_overrides_configuration_from_convention_source() { var dependentEntityTypeBuilder = CreateInternalEntityTypeBuilder <PostDetails>(); var principalEntityTypeBuilder = dependentEntityTypeBuilder.ModelBuilder.Entity(typeof(Post), ConfigurationSource.Convention); var relationshipBuilder = dependentEntityTypeBuilder.Relationship( principalEntityTypeBuilder, dependentEntityTypeBuilder, "Post", "PostDetails", dependentEntityTypeBuilder.GetOrCreateProperties(new List <PropertyInfo> { PostDetails.PostIdProperty }, ConfigurationSource.Convention), null, ConfigurationSource.Convention, isUnique: true); Assert.Equal("PostId", relationshipBuilder.Metadata.Properties.First().Name); relationshipBuilder = new ForeignKeyAttributeConvention().Apply(relationshipBuilder); Assert.Equal("PostFK", relationshipBuilder.Metadata.Properties.First().Name); }
/// <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); }
/// <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); }
public void Apply_MultiForeignKeysOnForeignKeyProperty_Works() { // Arrange ODataConventionModelBuilder builder = new ODataConventionModelBuilder(); builder.EntityType<PrincipalEntity>().HasKey(p => new { p.PrincipalStringId, p.PrincipalIntId }); EntityTypeConfiguration dependentEntity = builder.AddEntityType(typeof(MultiDependentEntity2)); PropertyInfo expectPropertyInfo1 = typeof(MultiDependentEntity2).GetProperty("PrincipalId1"); PrimitivePropertyConfiguration propertyConfig1 = dependentEntity.AddProperty(expectPropertyInfo1); PropertyInfo expectPropertyInfo2 = typeof(MultiDependentEntity2).GetProperty("PrincipalId2"); PrimitivePropertyConfiguration propertyConfig2 = dependentEntity.AddProperty(expectPropertyInfo2); PropertyInfo propertyInfo = typeof(MultiDependentEntity2).GetProperty("Principal"); NavigationPropertyConfiguration navigation = dependentEntity.AddNavigationProperty(propertyInfo, EdmMultiplicity.One); navigation.AddedExplicitly = false; // Act ForeignKeyAttributeConvention convention = new ForeignKeyAttributeConvention(); convention.Apply(propertyConfig1, dependentEntity, builder); convention.Apply(propertyConfig2, dependentEntity, builder); // Assert Assert.Equal(2, navigation.DependentProperties.Count()); Assert.Same(expectPropertyInfo1, navigation.DependentProperties.First()); Assert.Same(expectPropertyInfo2, navigation.DependentProperties.Last()); Assert.Equal(2, navigation.PrincipalProperties.Count()); Assert.Equal("PrincipalIntId", navigation.PrincipalProperties.First().Name); Assert.Equal("PrincipalStringId", navigation.PrincipalProperties.Last().Name); }