public virtual InternalRelationshipBuilder Apply(InternalRelationshipBuilder relationshipBuilder) { var foreignKey = (IForeignKey)relationshipBuilder.Metadata; if (!foreignKey.Properties.All(fk => fk.IsShadowProperty)) { return relationshipBuilder; } var foreignKeyProperties = FindCandidateForeignKeyProperties( relationshipBuilder.Metadata, onDependent: true); if (foreignKey.IsUnique && !foreignKey.IsSelfPrimaryKeyReferencing()) { var candidatePropertiesOnPrincipal = FindCandidateForeignKeyProperties( relationshipBuilder.Metadata, onDependent: false); bool shouldInvert; if (ShouldFlip(relationshipBuilder.Metadata, foreignKeyProperties, candidatePropertiesOnPrincipal) && relationshipBuilder.CanSet(relationshipBuilder.Metadata.DeclaringEntityType, relationshipBuilder.Metadata.PrincipalEntityType, null, null, /*dependentProperties:*/ candidatePropertiesOnPrincipal, null, null, null, null, true, ConfigurationSource.Convention, out shouldInvert)) { Debug.Assert(shouldInvert); var invertedBuilder = relationshipBuilder.Invert(ConfigurationSource.Convention); Debug.Assert(invertedBuilder != null); if (candidatePropertiesOnPrincipal == null) { return invertedBuilder; } // TODO: Remove, as this is redundant invertedBuilder = invertedBuilder.ForeignKey( candidatePropertiesOnPrincipal, ConfigurationSource.Convention); Debug.Assert(invertedBuilder != null); return invertedBuilder; } if (foreignKeyProperties == null) { foreignKeyProperties = GetCompatiblePrimaryKeyProperties( relationshipBuilder.Metadata.DeclaringEntityType, relationshipBuilder.Metadata.PrincipalEntityType, relationshipBuilder.Metadata.PrincipalKey.Properties); } } if (foreignKeyProperties != null && relationshipBuilder.Metadata.DeclaringEntityType.FindForeignKey(foreignKeyProperties) == null) { var newRelationshipBuilder = relationshipBuilder.ForeignKey(foreignKeyProperties, ConfigurationSource.Convention); if (newRelationshipBuilder != null) { return newRelationshipBuilder; } } return relationshipBuilder; }
public virtual InternalRelationshipBuilder Apply(InternalRelationshipBuilder relationshipBuilder) { var foreignKey = (IForeignKey)relationshipBuilder.Metadata; if (!foreignKey.Properties.All(fk => fk.IsShadowProperty)) { return(relationshipBuilder); } var foreignKeyProperties = FindCandidateForeignKeyProperties( relationshipBuilder.Metadata, onDependent: true); if (foreignKey.IsUnique && !foreignKey.IsSelfPrimaryKeyReferencing()) { var candidatePropertiesOnPrincipal = FindCandidateForeignKeyProperties( relationshipBuilder.Metadata, onDependent: false); bool shouldInvert; if (ShouldFlip(relationshipBuilder.Metadata, foreignKeyProperties, candidatePropertiesOnPrincipal) && relationshipBuilder.CanSet(relationshipBuilder.Metadata.DeclaringEntityType, relationshipBuilder.Metadata.PrincipalEntityType, null, null, /*dependentProperties:*/ candidatePropertiesOnPrincipal, null, null, null, null, true, ConfigurationSource.Convention, out shouldInvert)) { Debug.Assert(shouldInvert); var invertedBuilder = relationshipBuilder.Invert(ConfigurationSource.Convention); Debug.Assert(invertedBuilder != null); if (candidatePropertiesOnPrincipal == null) { return(invertedBuilder); } // TODO: Remove, as this is redundant invertedBuilder = invertedBuilder.ForeignKey( candidatePropertiesOnPrincipal, ConfigurationSource.Convention); Debug.Assert(invertedBuilder != null); return(invertedBuilder); } if (foreignKeyProperties == null) { foreignKeyProperties = GetCompatiblePrimaryKeyProperties( relationshipBuilder.Metadata.DeclaringEntityType, relationshipBuilder.Metadata.PrincipalEntityType, relationshipBuilder.Metadata.PrincipalKey.Properties); } } if (foreignKeyProperties != null && relationshipBuilder.Metadata.DeclaringEntityType.FindForeignKey(foreignKeyProperties) == null) { var newRelationshipBuilder = relationshipBuilder.ForeignKey(foreignKeyProperties, ConfigurationSource.Convention); if (newRelationshipBuilder != null) { return(newRelationshipBuilder); } } return(relationshipBuilder); }