public virtual InternalRelationshipBuilder Apply(InternalRelationshipBuilder relationshipBuilder) { var foreignKey = (IForeignKey)relationshipBuilder.Metadata; if (foreignKey.Properties.All(fk => fk.IsShadowProperty)) { var foreignKeyProperties = FindCandidateForeignKeyProperties(relationshipBuilder, onDependent: true); if (foreignKey.IsUnique && !foreignKey.IsSelfPrimaryKeyReferencing()) { if (ShouldFlip(relationshipBuilder, foreignKeyProperties)) { var newRelationshipBuilder = relationshipBuilder.Invert(ConfigurationSource.Convention); if (newRelationshipBuilder != null) { return(newRelationshipBuilder); } } if (foreignKeyProperties == null) { foreignKeyProperties = GetCompatiblePrimaryKeyProperties( relationshipBuilder.Metadata.DeclaringEntityType, 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)) { var foreignKeyProperties = FindCandidateForeignKeyProperties(relationshipBuilder, onDependent: true); if (foreignKey.IsUnique && !foreignKey.IsSelfPrimaryKeyReferencing()) { if (ShouldFlip(relationshipBuilder, foreignKeyProperties)) { var newRelationshipBuilder = relationshipBuilder.Invert(ConfigurationSource.Convention); if (newRelationshipBuilder != null) { return newRelationshipBuilder; } } if (foreignKeyProperties == null) { foreignKeyProperties = GetCompatiblePrimaryKeyProperties( relationshipBuilder.Metadata.DeclaringEntityType, 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) { Check.NotNull(relationshipBuilder, nameof(relationshipBuilder)); var foreignKey = relationshipBuilder.Metadata; var fkPropertyOnPrincipal = FindForeignKeyAttributeOnProperty(foreignKey.PrincipalEntityType, foreignKey.PrincipalToDependent?.Name); var fkPropertyOnDependent = FindForeignKeyAttributeOnProperty(foreignKey.DeclaringEntityType, foreignKey.DependentToPrincipal?.Name); if (!string.IsNullOrEmpty(fkPropertyOnDependent) && !string.IsNullOrEmpty(fkPropertyOnPrincipal)) { // TODO: Log Error that unable to determine principal end based on foreign key attributes on properties SplitNavigationsInSeparateRelationships(relationshipBuilder); return(null); } var fkPropertiesOnPrincipalToDependent = FindCandidateDependentPropertiesThroughNavigation(relationshipBuilder, pointsToPrincipal: false); var fkPropertiesOnDependentToPrincipal = FindCandidateDependentPropertiesThroughNavigation(relationshipBuilder, pointsToPrincipal: true); if (fkPropertiesOnDependentToPrincipal != null && fkPropertiesOnPrincipalToDependent != null && !fkPropertiesOnDependentToPrincipal.SequenceEqual(fkPropertiesOnPrincipalToDependent)) { // TODO: Log error that foreign key properties on both navigations do not match SplitNavigationsInSeparateRelationships(relationshipBuilder); return(null); } var fkPropertiesOnNavigation = fkPropertiesOnDependentToPrincipal ?? fkPropertiesOnPrincipalToDependent; InternalRelationshipBuilder newRelationshipBuilder = null; if (fkPropertiesOnNavigation == null || fkPropertiesOnNavigation.Count == 0) { if (fkPropertyOnDependent == null && fkPropertyOnPrincipal == null) { return(relationshipBuilder); } if (fkPropertyOnDependent != null) { newRelationshipBuilder = relationshipBuilder.ForeignKey(new List <string> { fkPropertyOnDependent }, ConfigurationSource.DataAnnotation); } else { newRelationshipBuilder = relationshipBuilder.Invert(ConfigurationSource.DataAnnotation) ?.ForeignKey(new List <string> { fkPropertyOnPrincipal }, ConfigurationSource.DataAnnotation); } } else { if (fkPropertyOnDependent == null && fkPropertyOnPrincipal == null) { if (fkPropertiesOnNavigation.All(p => foreignKey.DeclaringEntityType.FindProperty(p) != null) || fkPropertiesOnNavigation.Any(p => foreignKey.PrincipalEntityType.FindProperty(p) == null)) { newRelationshipBuilder = relationshipBuilder.ForeignKey(fkPropertiesOnNavigation, ConfigurationSource.DataAnnotation); } else { newRelationshipBuilder = relationshipBuilder.Invert(ConfigurationSource.DataAnnotation) ?.ForeignKey(fkPropertiesOnNavigation, ConfigurationSource.DataAnnotation); } } else { if (fkPropertiesOnNavigation.Count != 1 || !string.Equals(fkPropertiesOnNavigation.First(), fkPropertyOnDependent ?? fkPropertyOnPrincipal)) { // TODO: Log error that mismatch in foreignKey Attribute on navigation and property SplitNavigationsInSeparateRelationships(relationshipBuilder); return(null); } if (fkPropertyOnDependent != null) { newRelationshipBuilder = relationshipBuilder.ForeignKey(fkPropertiesOnNavigation, ConfigurationSource.DataAnnotation); } else { newRelationshipBuilder = relationshipBuilder.Invert(ConfigurationSource.DataAnnotation) ?.ForeignKey(fkPropertiesOnNavigation, ConfigurationSource.DataAnnotation); } } } return(newRelationshipBuilder ?? relationshipBuilder); }
public virtual InternalRelationshipBuilder Apply(InternalRelationshipBuilder relationshipBuilder) { Check.NotNull(relationshipBuilder, nameof(relationshipBuilder)); var foreignKey = relationshipBuilder.Metadata; var fkPropertyOnPrincipal = FindCandidateDependentPropertyThroughEntityType(foreignKey.PrincipalEntityType, foreignKey.PrincipalToDependent?.Name); var fkPropertyOnDependent = FindCandidateDependentPropertyThroughEntityType(foreignKey.DeclaringEntityType, foreignKey.DependentToPrincipal?.Name); if (!string.IsNullOrEmpty(fkPropertyOnDependent) && !string.IsNullOrEmpty(fkPropertyOnPrincipal)) { // TODO: Log Error that unable to determine principal end based on foreign key attributes var principalTypeNavigationName = foreignKey.PrincipalToDependent?.Name; var dependentTypeNavigationName = foreignKey.DependentToPrincipal?.Name; var dependentEntityTypebuilder = relationshipBuilder.ModelBuilder.Entity(foreignKey.DeclaringEntityType.Name, ConfigurationSource.Convention); var removedConfigurationSource = dependentEntityTypebuilder.RemoveRelationship(foreignKey, ConfigurationSource.DataAnnotation); if (removedConfigurationSource == null) { return relationshipBuilder; } var principalEntityTypeBuilder = relationshipBuilder.ModelBuilder.Entity(foreignKey.PrincipalEntityType.Name, ConfigurationSource.Convention); dependentEntityTypebuilder.Relationship( principalEntityTypeBuilder, dependentEntityTypebuilder, navigationToPrincipalName: dependentTypeNavigationName, navigationToDependentName: null, configurationSource: ConfigurationSource.DataAnnotation); principalEntityTypeBuilder.Relationship( dependentEntityTypebuilder, principalEntityTypeBuilder, navigationToPrincipalName: principalTypeNavigationName, navigationToDependentName: null, configurationSource: ConfigurationSource.DataAnnotation); return null; } var fkPropertiesOnPrincipalToDependent = FindCandidateDependentPropertiesThroughNavigation(relationshipBuilder, pointsToPrincipal: false); var fkPropertiesOnDependentToPrincipal = FindCandidateDependentPropertiesThroughNavigation(relationshipBuilder, pointsToPrincipal: true); if (fkPropertiesOnDependentToPrincipal != null && fkPropertiesOnPrincipalToDependent != null && !fkPropertiesOnDependentToPrincipal.SequenceEqual(fkPropertiesOnPrincipalToDependent)) { // TODO: Log error that mismatch in foreignKey Attribute on both navigations return relationshipBuilder; } var fkPropertiesOnNavigation = fkPropertiesOnDependentToPrincipal ?? fkPropertiesOnPrincipalToDependent; InternalRelationshipBuilder newRelationshipBuilder = null; if (fkPropertiesOnNavigation == null || fkPropertiesOnNavigation.Count == 0) { if (fkPropertyOnDependent == null && fkPropertyOnPrincipal == null) { return relationshipBuilder; } if (fkPropertyOnDependent != null) { newRelationshipBuilder = relationshipBuilder.ForeignKey(new List<string> { fkPropertyOnDependent }, ConfigurationSource.DataAnnotation); } else { newRelationshipBuilder = relationshipBuilder.Invert(ConfigurationSource.DataAnnotation) ?.ForeignKey(new List<string> { fkPropertyOnPrincipal }, ConfigurationSource.DataAnnotation); } } else { if (fkPropertyOnDependent == null && fkPropertyOnPrincipal == null) { if (fkPropertiesOnNavigation.All(p => foreignKey.DeclaringEntityType.FindProperty(p) != null) || fkPropertiesOnNavigation.Any(p => foreignKey.PrincipalEntityType.FindProperty(p) == null)) { newRelationshipBuilder = relationshipBuilder.ForeignKey(fkPropertiesOnNavigation, ConfigurationSource.DataAnnotation); } else { newRelationshipBuilder = relationshipBuilder.Invert(ConfigurationSource.DataAnnotation) ?.ForeignKey(fkPropertiesOnNavigation, ConfigurationSource.DataAnnotation); } } else { if (fkPropertiesOnNavigation.Count != 1 || !string.Equals(fkPropertiesOnNavigation.First(), fkPropertyOnDependent ?? fkPropertyOnPrincipal)) { // TODO: Log error that mismatch in foreignKey Attribute on navigation and property return relationshipBuilder; } if (fkPropertyOnDependent != null) { newRelationshipBuilder = relationshipBuilder.ForeignKey(fkPropertiesOnNavigation, ConfigurationSource.DataAnnotation); } else { newRelationshipBuilder = relationshipBuilder.Invert(ConfigurationSource.DataAnnotation) ?.ForeignKey(fkPropertiesOnNavigation, ConfigurationSource.DataAnnotation); } } } return newRelationshipBuilder ?? 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; }
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); }