private static void GenerateForeignKeyAssociationType( AssociationType associationType, DbDatabaseMapping databaseMapping) { DebugCheck.NotNull(associationType); DebugCheck.NotNull(databaseMapping); Debug.Assert(associationType.Constraint != null); var dependentEnd = associationType.Constraint.DependentEnd; var principalEnd = associationType.GetOtherEnd(dependentEnd); var principalEntityTypeMapping = GetEntityTypeMappingInHierarchy(databaseMapping, principalEnd.GetEntityType()); var dependentEntityTypeMapping = GetEntityTypeMappingInHierarchy(databaseMapping, dependentEnd.GetEntityType()); var foreignKeyConstraint = new ForeignKeyBuilder(databaseMapping.Database, associationType.Name) { PrincipalTable = principalEntityTypeMapping.MappingFragments.Single().Table, DeleteAction = principalEnd.DeleteBehavior != OperationAction.None ? principalEnd.DeleteBehavior : OperationAction.None }; dependentEntityTypeMapping .MappingFragments .Single() .Table .AddForeignKey(foreignKeyConstraint); foreignKeyConstraint.DependentColumns = associationType.Constraint.ToProperties.Select( dependentProperty => dependentEntityTypeMapping.GetPropertyMapping(dependentProperty).ColumnProperty); foreignKeyConstraint.SetAssociationType(associationType); }
private static void GenerateForeignKeyAssociationType( AssociationType associationType, DbDatabaseMapping databaseMapping) { AssociationEndMember dependentEnd = associationType.Constraint.DependentEnd; AssociationEndMember otherEnd = associationType.GetOtherEnd(dependentEnd); EntityTypeMapping mappingInHierarchy = StructuralTypeMappingGenerator.GetEntityTypeMappingInHierarchy(databaseMapping, otherEnd.GetEntityType()); EntityTypeMapping dependentEntityTypeMapping = StructuralTypeMappingGenerator.GetEntityTypeMappingInHierarchy(databaseMapping, dependentEnd.GetEntityType()); ForeignKeyBuilder foreignKeyBuilder = new ForeignKeyBuilder(databaseMapping.Database, associationType.Name) { PrincipalTable = mappingInHierarchy.MappingFragments.Single <MappingFragment>().Table, DeleteAction = otherEnd.DeleteBehavior != OperationAction.None ? otherEnd.DeleteBehavior : OperationAction.None }; dependentEntityTypeMapping.MappingFragments.Single <MappingFragment>().Table.AddForeignKey(foreignKeyBuilder); foreignKeyBuilder.DependentColumns = associationType.Constraint.ToProperties.Select <EdmProperty, EdmProperty>((Func <EdmProperty, EdmProperty>)(dependentProperty => dependentEntityTypeMapping.GetPropertyMapping(dependentProperty).ColumnProperty)); foreignKeyBuilder.SetAssociationType(associationType); }