public void Navigation_back_pointer_is_fixed_up_as_navigation_is_added_and_removed()
        {
            var entityType1 = new EntityType(typeof(Customer));

            entityType1.SetKey(entityType1.AddProperty(Customer.IdProperty));
            var entityType2 = new EntityType(typeof(Customer));

            var navigation
                = new Navigation(
                      new ForeignKey(
                          entityType1.GetKey(),
                          new[] { entityType1.AddProperty(Customer.IdProperty) }), "Nav", pointsToPrincipal: true);

            entityType1.AddNavigation(navigation);

            Assert.Same(entityType1, navigation.EntityType);

            entityType2.AddNavigation(navigation);

            Assert.Same(entityType2, navigation.EntityType);
            Assert.Empty(entityType1.Navigations);

            entityType2.RemoveNavigation(navigation);

            Assert.Empty(entityType2.Navigations);
            Assert.Null(navigation.EntityType);
        }
Example #2
0
                public KeyMetadataBuilder Properties([NotNull] params string[] propertyNames)
                {
                    Check.NotNull(propertyNames, "propertyNames");

                    _entityType.SetKey(propertyNames.Select(n => _entityType.GetProperty(n)).ToArray());

                    return(new KeyMetadataBuilder(_entityType.GetKey()));
                }
        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);
        }
Example #4
0
                public virtual KeyMetadataBuilder Properties([NotNull] Expression <Func <TEntity, object> > keyExpression)
                {
                    Check.NotNull(keyExpression, "keyExpression");

                    EntityType.SetKey(
                        keyExpression.GetPropertyAccessList()
                        .Select(pi => EntityType.TryGetProperty(pi.Name)
                                ?? EntityType.AddProperty(pi))
                        .ToArray());

                    return(new KeyMetadataBuilder(EntityType.GetKey()));
                }
        public void FK_properties_are_marked_as_requiring_original_values()
        {
            var entityType = new EntityType(typeof(FullNotificationEntity));

            entityType.SetKey(entityType.AddProperty("Id", typeof(int)));

            Assert.Equal(-1, entityType.GetProperty("Id").OriginalValueIndex);

            entityType.AddForeignKey(entityType.GetKey(), new[] { entityType.AddProperty("Id", typeof(int)) });

            Assert.Equal(0, entityType.GetProperty("Id").OriginalValueIndex);
        }
        public void Setting_foreign_key_properties_should_update_existing_properties()
        {
            var entityType = new EntityType(typeof(Order));

            entityType.SetKey(entityType.AddProperty(Order.CustomerIdProperty));

            var newIdProperty = entityType.AddProperty(Order.CustomerIdProperty);
            var property2     = entityType.AddProperty(Order.CustomerUniqueProperty);

            entityType.AddForeignKey(entityType.GetKey(), new[] { newIdProperty, property2 });

            Assert.Equal(new[] { newIdProperty, property2 }, entityType.Properties.ToArray());
        }
        public void Add_foreign_key()
        {
            var entityType = new EntityType(typeof(Order));

            entityType.SetKey(entityType.AddProperty(Order.IdProperty));

            var foreignKey
                = entityType.AddForeignKey(
                      entityType.GetKey(),
                      new[] { entityType.AddProperty(Order.CustomerUniqueProperty) });

            Assert.True(entityType.ForeignKeys.Contains(foreignKey));
        }
        public void Can_clear_key()
        {
            var entityType = new EntityType(typeof(Customer));

            var property1 = entityType.AddProperty(Customer.IdProperty);
            var property2 = entityType.AddProperty(Customer.NameProperty);

            entityType.SetKey(property1, property2);

            Assert.Equal(2, entityType.GetKey().Properties.Count());

            entityType.SetKey(null);

            Assert.Null(entityType.TryGetKey());
        }
        public void Can_set_and_reset_key()
        {
            var entityType = new EntityType(typeof(Customer));

            var property1 = entityType.AddProperty(Customer.IdProperty);
            var property2 = entityType.AddProperty(Customer.NameProperty);

            entityType.SetKey(property1, property2);

            Assert.True(new[] { property1, property2 }.SequenceEqual(entityType.GetKey().Properties));
            Assert.True(new[] { property1, property2 }.SequenceEqual(entityType.Properties));

            entityType.RemoveProperty(property1);

            Assert.True(new[] { property2 }.SequenceEqual(entityType.GetKey().Properties));
            Assert.True(new[] { property2 }.SequenceEqual(entityType.Properties));

            property1 = entityType.AddProperty(Customer.IdProperty);

            entityType.SetKey(property1);

            Assert.True(new[] { property1 }.SequenceEqual(entityType.GetKey().Properties));
            Assert.True(new[] { property1, property2 }.SequenceEqual(entityType.Properties));
        }
        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 FK_back_pointer_is_fixed_up_as_FK_is_added()
        {
            var entityType = new EntityType(typeof(Customer));
            var property   = entityType.AddProperty(Customer.IdProperty);

            entityType.SetKey(property);
            var foreignKey
                = entityType.AddForeignKey(entityType.GetKey(), property);

            Assert.Same(entityType, foreignKey.EntityType);
            Assert.Same(entityType, property.EntityType);

            entityType.RemoveForeignKey(foreignKey);

            // Currently property is not removed when FK is removed
            Assert.Empty(entityType.ForeignKeys);
            Assert.Same(property, entityType.Properties.Single());
            Assert.Same(entityType, foreignKey.EntityType); // TODO: Throw here?
            Assert.Same(entityType, property.EntityType);
        }