Beispiel #1
0
        public virtual Navigation AddNavigation([NotNull] Navigation navigation)
        {
            Check.NotNull(navigation, "navigation");

            if (navigation.EntityType != null &&
                navigation.EntityType != this)
            {
                throw new InvalidOperationException(Strings.FormatNavigationAlreadyOwned(navigation.Name, Name, navigation.EntityType.Name));
            }

            if (_navigations.Value.Any(n => n.Name == navigation.Name))
            {
                throw new InvalidOperationException(Strings.FormatDuplicateNavigation(navigation.Name, Name));
            }

            if (!HasClrType)
            {
                throw new InvalidOperationException(Strings.FormatNavigationOnShadowEntity(navigation.Name, Name));
            }

            var clrProperty = Type.GetPropertiesInHierarchy(navigation.Name).FirstOrDefault();

            if (clrProperty == null)
            {
                throw new InvalidOperationException(Strings.FormatNoClrNavigation(navigation.Name, Name));
            }

            var targetClrType = navigation.GetTargetType().Type;

            if (navigation.IsCollection())
            {
                var elementType = clrProperty.PropertyType.TryGetElementType(typeof(IEnumerable <>));

                if (elementType == null || !elementType.GetTypeInfo().IsAssignableFrom(targetClrType.GetTypeInfo()))
                {
                    throw new InvalidOperationException(Strings.FormatWrongClrCollectionNavigationType(
                                                            navigation.Name, Name, clrProperty.PropertyType.FullName, targetClrType.FullName));
                }
            }
            else if (!clrProperty.PropertyType.GetTypeInfo().IsAssignableFrom(targetClrType.GetTypeInfo()))
            {
                throw new InvalidOperationException(Strings.FormatWrongClrSingleNavigationType(
                                                        navigation.Name, Name, clrProperty.PropertyType.FullName, targetClrType.FullName));
            }

            var otherNavigation = _navigations.Value.FirstOrDefault(n => n.ForeignKey == navigation.ForeignKey &&
                                                                    navigation.PointsToPrincipal == n.PointsToPrincipal);

            if (otherNavigation != null)
            {
                throw new InvalidOperationException(Strings.FormatMultipleNavigations(navigation.Name, otherNavigation.Name, Name));
            }

            _navigations.Value.Add(navigation);

            navigation.EntityType = this;

            return(navigation);
        }
        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 void Can_create_navigation_to_collection_of_dependents()
        {
            var keyMock = new Mock<ForeignKey>();
            keyMock.Setup(m => m.IsUnique).Returns(false);
            var foreignKey = keyMock.Object;

            var navigation = new Navigation(foreignKey, "Deception", pointsToPrincipal: false);

            Assert.Same(foreignKey, navigation.ForeignKey);
            Assert.Equal("Deception", navigation.Name);
            Assert.Null(navigation.EntityType);
            Assert.False(navigation.PointsToPrincipal);
            Assert.True(navigation.IsCollection());

            Assert.Same(foreignKey, ((INavigation)navigation).ForeignKey);
            Assert.Null(((INavigation)navigation).EntityType);
        }
Beispiel #4
0
        public void Can_create_navigation_to_collection_of_dependents()
        {
            var keyMock = new Mock <ForeignKey>();

            keyMock.Setup(m => m.IsUnique).Returns(false);
            var foreignKey = keyMock.Object;

            var navigation = new Navigation(foreignKey, "Deception", pointsToPrincipal: false);

            Assert.Same(foreignKey, navigation.ForeignKey);
            Assert.Equal("Deception", navigation.Name);
            Assert.Null(navigation.EntityType);
            Assert.False(navigation.PointsToPrincipal);
            Assert.True(navigation.IsCollection());

            Assert.Same(foreignKey, ((INavigation)navigation).ForeignKey);
            Assert.Null(((INavigation)navigation).EntityType);
        }
Beispiel #5
0
        public virtual Navigation AddNavigation([NotNull] string name, [NotNull] ForeignKey foreignKey, bool pointsToPrincipal)
        {
            Check.NotEmpty(name, "name");
            Check.NotNull(foreignKey, "foreignKey");

            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 (TryGetNavigation(name) != null)
            {
                throw new InvalidOperationException(Strings.DuplicateNavigation(navigation.Name, Name));
            }

            if (!HasClrType)
            {
                throw new InvalidOperationException(Strings.NavigationOnShadowEntity(navigation.Name, Name));
            }

            var clrProperty = Type.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.Type;

            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.Value.FirstOrDefault(n => n.ForeignKey == navigation.ForeignKey &&
                                                                    navigation.PointsToPrincipal == n.PointsToPrincipal);

            if (otherNavigation != null)
            {
                throw new InvalidOperationException(Strings.MultipleNavigations(navigation.Name, otherNavigation.Name, Name));
            }

            _navigations.Value.Add(navigation);

            return(navigation);
        }
Beispiel #6
0
        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);
        }