Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        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());
        }
Esempio n. 3
0
        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));
        }
Esempio n. 4
0
                public ForeignKeyBuilder ForeignKey <TReferencedEntityType>(
                    [NotNull] Expression <Func <TEntity, object> > foreignKeyExpression)
                {
                    Check.NotNull(foreignKeyExpression, "foreignKeyExpression");

                    var principalType = ModelBuilder.Entity <TReferencedEntityType>().Metadata;

                    var dependentProperties
                        = foreignKeyExpression.GetPropertyAccessList()
                          .Select(pi => EntityType.TryGetProperty(pi.Name) ?? EntityType.AddProperty(pi))
                          .ToArray();

                    // TODO: This code currently assumes that the FK maps to a PK on the principal end
                    var foreignKey
                        = EntityType.AddForeignKey(principalType.GetKey(), dependentProperties);

                    return(new ForeignKeyBuilder(foreignKey));
                }
Esempio n. 5
0
        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);
        }
Esempio n. 6
0
        public void Can_get_referencing_foreign_keys()
        {
            var model       = new Model();
            var entityType1 = new EntityType(typeof(Customer));
            var entityType2 = new EntityType(typeof(Order));
            var keyProperty = new Property("Id", typeof(Customer))
            {
                EntityType = entityType1
            };
            var fkProperty = new Property("CustomerId", typeof(Order))
            {
                EntityType = entityType2
            };
            var foreignKey = entityType2.AddForeignKey(new Key(new[] { keyProperty }), fkProperty);

            model.AddEntityType(entityType1);
            model.AddEntityType(entityType2);

            var referencingForeignKeys = model.GetReferencingForeignKeys(entityType1);

            Assert.Same(foreignKey, referencingForeignKeys.Single());
            Assert.Same(foreignKey, entityType1.GetReferencingForeignKeys().Single());
        }