public Navigation([NotNull] string name, [NotNull] ForeignKey foreignKey)
            : base(Check.NotEmpty(name, nameof(name)))
        {
            Check.NotNull(foreignKey, nameof(foreignKey));

            _foreignKey = foreignKey;
        }
示例#2
0
        public Navigation([NotNull] ForeignKey foreignKey, [NotNull] string name, bool pointsToPrincipal)
            : base(Check.NotEmpty(name, "name"))
        {
            Check.NotNull(foreignKey, "foreignKey");

            _foreignKey = foreignKey;
            _pointsToPrincipal = pointsToPrincipal;
        }
示例#3
0
        public void IsRequired_when_dependent_property_nullable()
        {
            var entityType = new EntityType("E");
            entityType.SetKey(entityType.AddProperty("Id", typeof(int)));
            var dependentProp = entityType.AddProperty("P", typeof(int?));

            var foreignKey = new ForeignKey(entityType.GetKey(), new[] { dependentProp });

            Assert.False(foreignKey.IsRequired);
        }
示例#4
0
        public Navigation(
            [NotNull] string name, [NotNull] ForeignKey foreignKey, bool pointsToPrincipal)
            : base(Check.NotEmpty(name, nameof(name)))
        {
            Check.NotNull(foreignKey, nameof(foreignKey));

            _foreignKey = foreignKey;

            if (pointsToPrincipal)
            {
                foreignKey.DependentToPrincipal = this;
            }
            else
            {
                foreignKey.PrincipalToDependent = this;
            }
        }
示例#5
0
        public void Can_create_foreign_key()
        {
            var entityType = new EntityType("E");
            var dependentProp = entityType.AddProperty("P", typeof(int));
            var principalProp = entityType.AddProperty("Id", typeof(int));
            entityType.SetKey(principalProp);

            var foreignKey
                = new ForeignKey(entityType.GetKey(), new[] { dependentProp })
                    {
                        IsUnique = true,
                    };

            Assert.Same(entityType, foreignKey.ReferencedEntityType);
            Assert.Same(principalProp, foreignKey.ReferencedProperties.Single());
            Assert.Same(dependentProp, foreignKey.Properties.Single());
            Assert.True(foreignKey.IsUnique);
        }
        public void Can_create_foreign_key()
        {
            var entityType = new Model().AddEntityType("E");
            var dependentProp = entityType.GetOrAddProperty("P", typeof(int), shadowProperty: true);
            var principalProp = entityType.GetOrAddProperty("Id", typeof(int), shadowProperty: true);
            entityType.GetOrSetPrimaryKey(principalProp);

            var foreignKey
                = new ForeignKey(new[] { dependentProp }, entityType.GetPrimaryKey())
                    {
                        IsUnique = true
                    };

            Assert.Same(entityType, foreignKey.PrincipalEntityType);
            Assert.Same(principalProp, foreignKey.PrincipalKey.Properties.Single());
            Assert.Same(dependentProp, foreignKey.Properties.Single());
            Assert.True(foreignKey.IsUnique.Value);
            Assert.Same(entityType.GetPrimaryKey(), foreignKey.PrincipalKey);
        }
示例#7
0
        public NpgsqlForeignKeyBuilder([NotNull] ForeignKey foreignKey)
        {
            Check.NotNull(foreignKey, nameof(foreignKey));

            _foreignKey = foreignKey;
        }
 public static Navigation GetNavigationToDependent([NotNull] this ForeignKey foreignKey)
 {
     return((Navigation)((IForeignKey)foreignKey).GetNavigationToDependent());
 }
 public static Navigation GetNavigationToPrincipal([NotNull] this ForeignKey foreignKey)
 {
     return((Navigation)((IForeignKey)foreignKey).GetNavigationToPrincipal());
 }
        public RelationalForeignKeyBuilder([NotNull] ForeignKey foreignKey)
        {
            Check.NotNull(foreignKey, nameof(foreignKey));

            _foreignKey = foreignKey;
        }
示例#11
0
 public virtual Navigation GetOrAddNavigation([NotNull] string name, [NotNull] ForeignKey foreignKey, bool pointsToPrincipal)
 => FindNavigation(name) ?? AddNavigation(name, foreignKey, pointsToPrincipal);
        public void IsRequired_false_when_any_part_of_composite_FK_is_nullable()
        {
            var entityType = new Model().AddEntityType("E");
            entityType.GetOrSetPrimaryKey(new[]
                {
                    entityType.GetOrAddProperty("Id1", typeof(int), shadowProperty: true),
                    entityType.GetOrAddProperty("Id2", typeof(string), shadowProperty: true)
                });

            var dependentProp1 = entityType.GetOrAddProperty("P1", typeof(int), shadowProperty: true);
            var dependentProp2 = entityType.GetOrAddProperty("P2", typeof(string), shadowProperty: true);
            dependentProp2.IsNullable = true;

            var foreignKey = new ForeignKey(new[] { dependentProp1, dependentProp2 }, entityType.GetPrimaryKey());

            Assert.Null(foreignKey.IsRequired);
            Assert.False(((IForeignKey)foreignKey).IsRequired);

            dependentProp2.IsNullable = false;

            Assert.Null(foreignKey.IsRequired);
            Assert.True(((IForeignKey)foreignKey).IsRequired);
        }
示例#13
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);
        }
        public void IsRequired_and_IsUnique_null_when_dependent_property_nullable_by_default()
        {
            var entityType = new Model().AddEntityType("E");
            entityType.GetOrSetPrimaryKey(entityType.GetOrAddProperty("Id", typeof(int), shadowProperty: true));
            var dependentProp = entityType.GetOrAddProperty("P", typeof(int?), shadowProperty: true);

            var foreignKey = new ForeignKey(new[] { dependentProp }, entityType.GetPrimaryKey());

            Assert.Null(foreignKey.IsRequired);
            Assert.False(((IForeignKey)foreignKey).IsRequired);
            Assert.Null(foreignKey.IsUnique);
            Assert.False(((IForeignKey)foreignKey).IsUnique);
        }
示例#15
0
 public virtual Navigation GetOrAddNavigation([NotNull] string name, [NotNull] ForeignKey foreignKey, bool pointsToPrincipal)
 {
     return(TryGetNavigation(name) ?? AddNavigation(name, foreignKey, pointsToPrincipal));
 }
示例#16
0
 private static bool Matches(ForeignKey foreignKey, IEnumerable <Property> properties)
 {
     return(foreignKey.Properties.SequenceEqual(properties));
 }
示例#17
0
 public RelationalForeignKeyAnnotations([NotNull] ForeignKey foreignKey)
     : base(foreignKey)
 {
 }
        public void Setting_IsRequired_to_false_will_set_all_FK_properties_as_nullable()
        {
            var entityType = new Model().AddEntityType("E");
            entityType.GetOrSetPrimaryKey(new[]
                {
                    entityType.GetOrAddProperty("Id1", typeof(int), shadowProperty: true),
                    entityType.GetOrAddProperty("Id2", typeof(string), shadowProperty: true)
                });

            var dependentProp1 = entityType.GetOrAddProperty("P1", typeof(int?), shadowProperty: true);
            var dependentProp2 = entityType.GetOrAddProperty("P2", typeof(string), shadowProperty: true);

            var foreignKey = new ForeignKey(new[] { dependentProp1, dependentProp2 }, entityType.GetPrimaryKey()) { IsRequired = false };

            Assert.False(foreignKey.IsRequired.Value);
            Assert.True(dependentProp1.IsNullable.Value);
            Assert.True(dependentProp2.IsNullable.Value);
        }
        public SqlServerForeignKeyBuilder([NotNull] ForeignKey foreignKey)
        {
            Check.NotNull(foreignKey, nameof(foreignKey));

            _foreignKey = foreignKey;
        }
示例#20
0
 public static EntityType ResolveEntityType([NotNull] this ForeignKey foreignKey, [NotNull] EntityType entityType)
 => (EntityType)((IForeignKey)foreignKey).ResolveEntityType(entityType);
        public void IsRequired_true_when_dependent_property_not_nullable()
        {
            var entityType = new Model().AddEntityType("E");
            entityType.GetOrSetPrimaryKey(entityType.GetOrAddProperty("Id", typeof(int), shadowProperty: true));
            var dependentProp = entityType.GetOrAddProperty("P", typeof(int), shadowProperty: true);
            dependentProp.IsNullable = false;

            var foreignKey = new ForeignKey(new[] { dependentProp }, entityType.GetPrimaryKey());

            Assert.Null(foreignKey.IsRequired);
            Assert.True(((IForeignKey)foreignKey).IsRequired);
        }
示例#22
0
        public virtual Navigation AddNavigation([NotNull] string name, [NotNull] ForeignKey foreignKey, bool pointsToPrincipal)
        {
            Check.NotEmpty(name, nameof(name));
            Check.NotNull(foreignKey, nameof(foreignKey));

            if (_navigations.HasValue &&
                _navigations.Value.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.Value.Values.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(name, navigation);

            return(navigation);
        }
示例#23
0
 public static Navigation FindNavigationTo([NotNull] this ForeignKey foreignKey, [NotNull] EntityType entityType)
 => (Navigation)((IForeignKey)foreignKey).FindNavigationTo(entityType);
        public void IsRequired_false_for_composite_FK_by_default()
        {
            var entityType = new Model().AddEntityType("E");
            entityType.GetOrSetPrimaryKey(new[]
                {
                    entityType.GetOrAddProperty("Id1", typeof(int), shadowProperty: true),
                    entityType.GetOrAddProperty("Id2", typeof(string), shadowProperty: true)
                });

            var dependentProp1 = entityType.GetOrAddProperty("P1", typeof(int), shadowProperty: true);
            var dependentProp2 = entityType.GetOrAddProperty("P2", typeof(string), shadowProperty: true);

            var foreignKey = new ForeignKey(new[] { dependentProp1, dependentProp2 }, entityType.GetPrimaryKey());

            Assert.Null(foreignKey.IsRequired);
            Assert.False(((IForeignKey)foreignKey).IsRequired);
        }