public static bool IsCompatible( [NotNull] this Navigation navigation, [NotNull] EntityType principalType, [NotNull] EntityType dependentType, bool?shouldPointToPrincipal, bool?oneToOne) { Check.NotNull(navigation, nameof(navigation)); Check.NotNull(principalType, nameof(principalType)); Check.NotNull(dependentType, nameof(dependentType)); if ((!shouldPointToPrincipal.HasValue || navigation.PointsToPrincipal() == shouldPointToPrincipal.Value) && navigation.ForeignKey.IsCompatible(principalType, dependentType, oneToOne)) { return(true); } if (!shouldPointToPrincipal.HasValue && navigation.ForeignKey.IsCompatible(dependentType, principalType, oneToOne)) { return(true); } return(false); }
public static EntityType GetTargetType([NotNull] this Navigation navigation) { Check.NotNull(navigation, nameof(navigation)); return(navigation.PointsToPrincipal() ? navigation.ForeignKey.PrincipalEntityType : navigation.ForeignKey.EntityType); }
public static Navigation FindInverse([NotNull] this Navigation navigation) { Check.NotNull(navigation, nameof(navigation)); return(navigation.PointsToPrincipal() ? navigation.ForeignKey.PrincipalToDependent : navigation.ForeignKey.DependentToPrincipal); }
public void Can_create_navigation_to_collection_of_dependents() { var foreignKey = CreateForeignKey(); foreignKey.IsUnique = false; var navigation = new Navigation("Deception", foreignKey, pointsToPrincipal: false); Assert.Same(foreignKey, navigation.ForeignKey); Assert.Equal("Deception", navigation.Name); Assert.NotNull(navigation.EntityType); Assert.False(navigation.PointsToPrincipal()); Assert.True(navigation.IsCollection()); }
public virtual Navigation RemoveNavigation([NotNull] Navigation navigation) { Check.NotNull(navigation, nameof(navigation)); Navigation removedNavigation; if (_navigations.TryGetValue(navigation.Name, out removedNavigation)) { if (navigation.PointsToPrincipal()) { navigation.ForeignKey.DependentToPrincipal = null; } else { navigation.ForeignKey.PrincipalToDependent = null; } _navigations.Remove(navigation.Name); return(removedNavigation); } return(null); }
public virtual Navigation AddNavigation([NotNull] string name, [NotNull] ForeignKey foreignKey, bool pointsToPrincipal) { Check.NotEmpty(name, nameof(name)); Check.NotNull(foreignKey, nameof(foreignKey)); if (_navigations.ContainsKey(name)) { throw new InvalidOperationException(Strings.DuplicateNavigation(name, Name)); } var navigation = new Navigation(name, foreignKey, pointsToPrincipal); if (navigation.EntityType != null && navigation.EntityType != this) { throw new InvalidOperationException(Strings.NavigationAlreadyOwned(navigation.Name, Name, navigation.EntityType.Name)); } 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.TryGetElementType(typeof(IEnumerable <>)); 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)); } var otherNavigation = _navigations.Values.FirstOrDefault( n => n.ForeignKey == navigation.ForeignKey && n.PointsToPrincipal() == navigation.PointsToPrincipal()); if (otherNavigation != null) { throw new InvalidOperationException(Strings.MultipleNavigations(navigation.Name, otherNavigation.Name, Name)); } _navigations.Add(name, navigation); return(navigation); }