public virtual Navigation AddNavigation([NotNull] string name, [NotNull] ForeignKey foreignKey, bool pointsToPrincipal) { Check.NotEmpty(name, nameof(name)); Check.NotNull(foreignKey, nameof(foreignKey)); var duplicateNavigation = FindNavigationsInHierarchy(name).FirstOrDefault(); if (duplicateNavigation != null) { throw new InvalidOperationException(CoreStrings.DuplicateNavigation(name, DisplayName(), duplicateNavigation.DeclaringEntityType.DisplayName())); } var duplicateProperty = FindPropertiesInHierarchy(name).FirstOrDefault(); if (duplicateProperty != null) { throw new InvalidOperationException(CoreStrings.ConflictingProperty(name, DisplayName(), duplicateProperty.DeclaringEntityType.DisplayName())); } var otherNavigation = Navigations.FirstOrDefault( n => n.ForeignKey == foreignKey && n.PointsToPrincipal() == pointsToPrincipal); if (otherNavigation != null) { throw new InvalidOperationException(CoreStrings.MultipleNavigations(name, otherNavigation.Name, Name)); } var declaringTypeFromFk = pointsToPrincipal ? foreignKey.DeclaringEntityType : foreignKey.PrincipalEntityType; if (declaringTypeFromFk != this) { throw new InvalidOperationException(CoreStrings.NavigationOnWrongEntityType(name, Name, declaringTypeFromFk.Name)); } Navigation.IsCompatible( name, this, pointsToPrincipal ? foreignKey.PrincipalEntityType : foreignKey.DeclaringEntityType, !pointsToPrincipal && !((IForeignKey)foreignKey).IsUnique, shouldThrow: true); var navigation = new Navigation(name, foreignKey); _navigations.Add(name, navigation); if (pointsToPrincipal) { foreignKey.DependentToPrincipal = navigation; } else { foreignKey.PrincipalToDependent = navigation; } return(navigation); }
public INavigation TryGetNavigation([NotNull] string name) { return(Navigations.FirstOrDefault(p => p.Name == name)); }
public virtual Navigation TryGetNavigation([NotNull] string name) { Check.NotEmpty(name, "name"); return(Navigations.FirstOrDefault(n => Matches(n, name))); }
public virtual Navigation AddNavigation([NotNull] string name, [NotNull] ForeignKey foreignKey, bool pointsToPrincipal) { Check.NotEmpty(name, nameof(name)); Check.NotNull(foreignKey, nameof(foreignKey)); if (FindNavigationCollisions(new[] { name }).Any()) { throw new InvalidOperationException(Strings.DuplicateNavigation(name, Name)); } var otherNavigation = Navigations.FirstOrDefault( n => n.ForeignKey == foreignKey && n.PointsToPrincipal() == pointsToPrincipal); if (otherNavigation != null) { throw new InvalidOperationException(Strings.MultipleNavigations(name, otherNavigation.Name, Name)); } var declaringTypeFromFk = pointsToPrincipal ? foreignKey.DeclaringEntityType : foreignKey.PrincipalEntityType; if (declaringTypeFromFk != this) { throw new InvalidOperationException(Strings.NavigationOnWrongEntityType(name, Name, declaringTypeFromFk.Name)); } var navigation = new Navigation(name, foreignKey); _navigations.Add(name, navigation); if (pointsToPrincipal) { foreignKey.DependentToPrincipal = navigation; } else { foreignKey.PrincipalToDependent = navigation; } if (!HasClrType) { throw new InvalidOperationException(Strings.NavigationOnShadowEntity(navigation.Name, Name)); } var clrProperty = ClrType.GetPropertiesInHierarchy(navigation.Name).FirstOrDefault(); if (clrProperty == null) { throw new InvalidOperationException(Strings.NoClrNavigation(navigation.Name, Name)); } var targetType = navigation.GetTargetType(); if (!targetType.HasClrType) { throw new InvalidOperationException(Strings.NavigationToShadowEntity(navigation.Name, Name, targetType.Name)); } var targetClrType = targetType.ClrType; Debug.Assert(targetClrType != null, "targetClrType != null"); if (navigation.IsCollection()) { var elementType = clrProperty.PropertyType.TryGetSequenceType(); if (elementType == null || !elementType.GetTypeInfo().IsAssignableFrom(targetClrType.GetTypeInfo())) { throw new InvalidOperationException(Strings.NavigationCollectionWrongClrType( navigation.Name, Name, clrProperty.PropertyType.FullName, targetClrType.FullName)); } } else if (!clrProperty.PropertyType.GetTypeInfo().IsAssignableFrom(targetClrType.GetTypeInfo())) { throw new InvalidOperationException(Strings.NavigationSingleWrongClrType( navigation.Name, Name, clrProperty.PropertyType.FullName, targetClrType.FullName)); } return(navigation); }