private void ValidateAgainstClrProperty(IProperty property) { if (!property.IsShadowProperty) { if (HasClrType) { var clrProperty = ClrType.GetPropertiesInHierarchy(property.Name).FirstOrDefault(); if (clrProperty == null) { throw new InvalidOperationException(Strings.NoClrProperty(property.Name, Name)); } if (property.ClrType != clrProperty.PropertyType) { throw new InvalidOperationException(Strings.PropertyWrongClrType(property.Name, Name)); } } else { throw new InvalidOperationException(Strings.ClrPropertyOnShadowEntity(property.Name, Name)); } } }
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); }
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); }