public void Circular_inheritance_should_throw()
        {
            var model = new Model();

            //    A
            //   / \
            //  B   C
            //       \
            //        D

            var a = model.AddEntityType(typeof(A).Name);
            var b = model.AddEntityType(typeof(B).Name);
            var c = model.AddEntityType(typeof(C).Name);
            var d = model.AddEntityType(typeof(D).Name);

            b.BaseType = a;
            c.BaseType = a;
            d.BaseType = c;

            Assert.Equal(
                CoreStrings.CircularInheritance(a, a),
                Assert.Throws<InvalidOperationException>(() => a.BaseType = a).Message);

            Assert.Equal(
                CoreStrings.CircularInheritance(a, b),
                Assert.Throws<InvalidOperationException>(() => a.BaseType = b).Message);

            Assert.Equal(
                CoreStrings.CircularInheritance(a, d),
                Assert.Throws<InvalidOperationException>(() => a.BaseType = d).Message);
        }
        public void Constructor_throws_when_referenced_key_not_on_referenced_entity()
        {
            var model = new Model();

            var principalEntityType = model.AddEntityType("R");
            var dependentEntityType = model.AddEntityType("D");
            var fk = dependentEntityType.AddProperty("Fk", typeof(int));

            var principalKey = dependentEntityType.SetPrimaryKey(fk);

            Assert.Equal(
                CoreStrings.ForeignKeyReferencedEntityKeyMismatch("{'Fk'}", "R"),
                Assert.Throws<ArgumentException>(() => new ForeignKey(new[] { fk }, principalKey, dependentEntityType, principalEntityType)).Message);
        }
 private ForeignKey CreateForeignKey()
 {
     var model = new Model();
     var entityType = model.AddEntityType("E");
     var idProperty = entityType.AddProperty("id", typeof(int));
     var key = entityType.SetPrimaryKey(idProperty);
     var fkProperty = entityType.AddProperty("p", typeof(int));
     return entityType.AddForeignKey(fkProperty, key, entityType);
 }
示例#4
0
        private ForeignKey CreateOneToManySameBaseFK()
        {
            var model = new Model();

            var baseEntityType = model.AddEntityType(typeof(NavigationBase));
            var property1      = baseEntityType.AddProperty(NavigationBase.IdProperty);
            var pk             = baseEntityType.GetOrSetPrimaryKey(property1);

            var principalEntityType = model.AddEntityType(typeof(OneToManyPrincipal));

            principalEntityType.BaseType = baseEntityType;

            var dependentEntityType = model.AddEntityType(typeof(OneToManyDependent));

            dependentEntityType.BaseType = baseEntityType;
            var fkProp = dependentEntityType.AddProperty("Fk", typeof(int));
            var fk     = dependentEntityType.AddForeignKey(new[] { fkProp }, pk, principalEntityType);

            fk.HasPrincipalToDependent("OneToManyDependents");
            fk.HasDependentToPrincipal("OneToManyPrincipal");
            return(fk);
        }
示例#5
0
        public void Can_only_ignore_existing_entity_type_explicitly_using_entity_entity_clr_type()
        {
            var model        = new Model();
            var entityType   = model.AddEntityType(typeof(Customer));
            var modelBuilder = new InternalModelBuilder(model, null);

            Assert.Same(entityType, modelBuilder.Entity(typeof(Customer), ConfigurationSource.Convention).Metadata);

            Assert.False(modelBuilder.IgnoreEntity(typeof(Customer), ConfigurationSource.DataAnnotation));

            Assert.Same(entityType, modelBuilder.Entity(typeof(Customer), ConfigurationSource.Convention).Metadata);
            Assert.False(modelBuilder.IgnoreEntity(typeof(Customer), ConfigurationSource.DataAnnotation));
            Assert.NotNull(model.TryGetEntityType(typeof(Customer)));

            Assert.True(modelBuilder.IgnoreEntity(typeof(Customer), ConfigurationSource.Explicit));
        }
        public void Can_only_override_existing_ClrType_value_explicitly()
        {
            var model = new Model();
            model.AddEntityType(typeof(Customer)).AddProperty(Customer.NameProperty);
            var modelBuilder = new InternalModelBuilder(model, new ConventionSet());
            var entityBuilder = modelBuilder.Entity(typeof(Customer), ConfigurationSource.Explicit);
            var builder = entityBuilder.Property(Customer.NameProperty.Name, ConfigurationSource.Convention);

            Assert.True(builder.ClrType(typeof(string), ConfigurationSource.DataAnnotation));
            Assert.False(builder.ClrType(typeof(int), ConfigurationSource.DataAnnotation));

            Assert.Equal(typeof(string), builder.Metadata.ClrType);

            Assert.True(builder.ClrType(typeof(int), ConfigurationSource.Explicit));
            Assert.Equal(typeof(int), builder.Metadata.ClrType);
        }
示例#7
0
        public void Can_only_override_existing_ClrType_value_explicitly()
        {
            var model = new Model();

            model.AddEntityType(typeof(Customer)).AddProperty(Customer.NameProperty);
            var modelBuilder  = new InternalModelBuilder(model, new ConventionSet());
            var entityBuilder = modelBuilder.Entity(typeof(Customer), ConfigurationSource.Explicit);
            var builder       = entityBuilder.Property(Customer.NameProperty.Name, ConfigurationSource.Convention);

            Assert.True(builder.ClrType(typeof(string), ConfigurationSource.DataAnnotation));
            Assert.False(builder.ClrType(typeof(int), ConfigurationSource.DataAnnotation));

            Assert.Equal(typeof(string), builder.Metadata.ClrType);

            Assert.True(builder.ClrType(typeof(int), ConfigurationSource.Explicit));
            Assert.Equal(typeof(int), builder.Metadata.ClrType);
        }
        public void Can_only_override_existing_Shadow_value_explicitly()
        {
            var model    = new Model();
            var metadata = model.AddEntityType(typeof(Customer)).AddProperty(Customer.NameProperty.Name);

            Assert.Null(metadata.GetIsShadowPropertyConfigurationSource());
            metadata.IsShadowProperty = false;
            var builder = CreateInternalPropertyBuilder(metadata);

            Assert.Equal(ConfigurationSource.Explicit, metadata.GetIsShadowPropertyConfigurationSource());
            Assert.True(builder.IsShadow(false, ConfigurationSource.DataAnnotation));
            Assert.False(builder.IsShadow(true, ConfigurationSource.DataAnnotation));

            Assert.False(builder.Metadata.IsShadowProperty);

            Assert.True(builder.IsShadow(true, ConfigurationSource.Explicit));
            Assert.True(builder.Metadata.IsShadowProperty);
        }
        public void Cannot_ignore_existing_entity_type_using_entity_type_name()
        {
            var model        = new Model();
            var entityType   = model.AddEntityType(typeof(Customer).FullName);
            var modelBuilder = CreateModelBuilder(model);

            Assert.Same(entityType, modelBuilder.Entity(typeof(Customer).FullName, ConfigurationSource.Convention).Metadata);

            Assert.False(modelBuilder.Ignore(typeof(Customer).FullName, ConfigurationSource.DataAnnotation));

            Assert.Same(entityType, modelBuilder.Entity(typeof(Customer).FullName, ConfigurationSource.Convention).Metadata);
            Assert.False(modelBuilder.Ignore(typeof(Customer).FullName, ConfigurationSource.DataAnnotation));
            Assert.NotNull(model.FindEntityType(typeof(Customer).FullName));

            Assert.Equal(Strings.EntityAddedExplicitly(typeof(Customer).FullName),
                         Assert.Throws <InvalidOperationException>(() =>
                                                                   Assert.False(modelBuilder.Ignore(typeof(Customer).FullName, ConfigurationSource.Explicit))).Message);
        }
示例#10
0
        public void Can_add_and_remove_entity_by_type()
        {
            var model = new Model();
            Assert.Null(model.FindEntityType(typeof(Customer)));
            Assert.Null(model.RemoveEntityType(typeof(Customer)));

            var entityType = model.AddEntityType(typeof(Customer));

            Assert.Equal(typeof(Customer), entityType.ClrType);
            Assert.NotNull(model.FindEntityType(typeof(Customer)));
            Assert.Same(model, entityType.Model);

            Assert.Same(entityType, model.GetOrAddEntityType(typeof(Customer)));

            Assert.Equal(new[] { entityType }, model.GetEntityTypes().ToArray());

            Assert.Same(entityType, model.RemoveEntityType(entityType.ClrType));
            Assert.Null(model.RemoveEntityType(entityType.ClrType));
            Assert.Null(model.FindEntityType(typeof(Customer)));
        }
示例#11
0
        public void Can_add_and_remove_entity_by_type()
        {
            var model = new Model();

            Assert.Null(model.FindEntityType(typeof(Customer)));
            Assert.Null(model.RemoveEntityType(typeof(Customer)));

            var entityType = model.AddEntityType(typeof(Customer));

            Assert.Equal(typeof(Customer), entityType.ClrType);
            Assert.NotNull(model.FindEntityType(typeof(Customer)));
            Assert.Same(model, entityType.Model);

            Assert.Same(entityType, model.GetOrAddEntityType(typeof(Customer)));

            Assert.Equal(new[] { entityType }, model.GetEntityTypes().ToArray());

            Assert.Same(entityType, model.RemoveEntityType(entityType.ClrType));
            Assert.Null(model.RemoveEntityType(entityType.ClrType));
            Assert.Null(model.FindEntityType(typeof(Customer)));
        }
示例#12
0
        public void Index_added_to_base_type_should_be_inherited()
        {
            var model = new Model();

            var orderType = model.AddEntityType(typeof(Order));
            var indexProperty = orderType.GetOrAddProperty(Order.CustomerIdProperty);

            var specialOrderType = model.AddEntityType(typeof(SpecialOrder));

            specialOrderType.BaseType = orderType;
            orderType.GetOrAddIndex(indexProperty);

            Assert.Equal(new[] { new[] { Order.CustomerIdProperty.Name } },
                orderType.GetIndexes().Select(fk => fk.Properties.Select(p => p.Name).ToArray()).ToArray());
            Assert.Equal(new[] { new[] { Order.CustomerIdProperty.Name } },
                specialOrderType.GetIndexes().Select(fk => fk.Properties.Select(p => p.Name).ToArray()).ToArray());

            var derivedIndexProperty = specialOrderType.GetOrAddProperty(Order.IdProperty);
            specialOrderType.GetOrAddIndex(derivedIndexProperty);

            Assert.Equal(new[] { new[] { Order.CustomerIdProperty.Name } },
                orderType.GetIndexes().Select(fk => fk.Properties.Select(p => p.Name).ToArray()).ToArray());
            Assert.Equal(new[] { new[] { Order.CustomerIdProperty.Name }, new[] { Order.IdProperty.Name } },
                specialOrderType.GetIndexes().Select(fk => fk.Properties.Select(p => p.Name).ToArray()).ToArray());
        }
示例#13
0
        public void ForeignKeys_should_be_updated_when_base_type_changes()
        {
            var model = new Model();

            var customerType = model.AddEntityType(typeof(SpecialCustomer));
            var customerKey = customerType.GetOrAddKey(customerType.GetOrAddProperty(Customer.IdProperty));

            var orderType = model.AddEntityType(typeof(Order));

            var specialOrderType = model.AddEntityType(typeof(SpecialOrder));
            specialOrderType.BaseType = orderType;
            var derivedForeignKeyProperty = specialOrderType.GetOrAddProperty(Order.IdProperty);
            specialOrderType.GetOrAddForeignKey(derivedForeignKeyProperty, customerKey, customerType);

            var foreignKeyProperty = orderType.GetOrAddProperty(Order.CustomerIdProperty);
            orderType.GetOrAddForeignKey(foreignKeyProperty, customerKey, customerType);

            specialOrderType.BaseType = null;

            Assert.Equal(new[] { new[] { Order.CustomerIdProperty.Name } },
                orderType.GetForeignKeys().Select(fk => fk.Properties.Select(p => p.Name).ToArray()).ToArray());
            Assert.Equal(new[] { new[] { Order.IdProperty.Name } },
                specialOrderType.GetForeignKeys().Select(fk => fk.Properties.Select(p => p.Name).ToArray()).ToArray());
        }
示例#14
0
        public void Adding_navigation_throws_when_grandchild_type_has_navigation_with_same_name()
        {
            var model = new Model();

            var customerType = model.AddEntityType(typeof(Customer));
            var customerKey = customerType.GetOrAddKey(customerType.GetOrAddProperty(Customer.IdProperty));

            var orderType = model.AddEntityType(typeof(SpecialOrder));
            var foreignKeyProperty = orderType.GetOrAddProperty(Order.CustomerIdProperty);
            var customerForeignKey = orderType.GetOrAddForeignKey(foreignKeyProperty, customerKey, customerType);

            var specialCustomerType = model.AddEntityType(typeof(SpecialCustomer));
            specialCustomerType.BaseType = customerType;

            var verySpecialCustomerType = model.AddEntityType(typeof(VerySpecialCustomer));
            verySpecialCustomerType.BaseType = specialCustomerType;

            var derivedForeignKeyProperty = orderType.GetOrAddProperty(Order.IdProperty);
            var specialCustomerForeignKey = orderType.GetOrAddForeignKey(derivedForeignKeyProperty, customerKey, verySpecialCustomerType);
            specialCustomerForeignKey.HasPrincipalToDependent("Orders");

            Assert.Equal(
                CoreStrings.NavigationForWrongForeignKey("Orders", typeof(VerySpecialCustomer).Name, "{'CustomerId'}", "{'Id'}"),
                Assert.Throws<InvalidOperationException>(() =>
                    customerForeignKey.HasPrincipalToDependent("Orders")).Message);

            Assert.Equal("Orders", ((IEntityType)verySpecialCustomerType).GetNavigations().Single().Name);
        }
示例#15
0
        public void Setting_base_type_throws_when_child_contains_key()
        {
            var model = new Model();

            var a = model.AddEntityType(typeof(A));
            var b = model.AddEntityType(typeof(B));
            var key = b.AddKey(b.AddProperty(B.HProperty));

            Assert.Equal(
                CoreStrings.DerivedEntityCannotHaveKeys(typeof(B).FullName),
                Assert.Throws<InvalidOperationException>(() => b.BaseType = a).Message);

            b.RemoveKey(key.Properties);
            b.SetPrimaryKey(b.AddProperty(B.FProperty));

            Assert.Equal(
                CoreStrings.DerivedEntityCannotHaveKeys(typeof(B).FullName),
                Assert.Throws<InvalidOperationException>(() => b.BaseType = a).Message);
        }
示例#16
0
        public void Keys_should_be_updated_when_base_type_changes()
        {
            var model = new Model();

            var a = model.AddEntityType(typeof(A));
            a.SetPrimaryKey(a.AddProperty(A.GProperty));
            a.AddKey(a.AddProperty(A.EProperty));

            var b = model.AddEntityType(typeof(B));
            b.AddProperty(B.FProperty);
            b.BaseType = a;

            b.BaseType = null;

            Assert.Equal(new[] { new[] { "E" }, new[] { "G" } },
                a.GetKeys().Select(fk => fk.Properties.Select(p => p.Name).ToArray()).ToArray());
            Assert.Equal(new string[0][],
                b.GetKeys().Select(fk => fk.Properties.Select(p => p.Name).ToArray()).ToArray());
            Assert.Equal(new[] { "G", "E" }, a.GetProperties().Select(p => p.Name).ToArray());
            Assert.Equal(new[] { "F" }, b.GetProperties().Select(p => p.Name).ToArray());
        }
示例#17
0
        public void Keys_on_base_type_should_be_inherited()
        {
            var model = new Model();

            var a = model.AddEntityType(typeof(A));
            var pk = a.SetPrimaryKey(a.AddProperty(A.GProperty));
            a.AddKey(a.AddProperty(A.EProperty));

            var b = model.AddEntityType(typeof(B));
            b.AddProperty(B.FProperty);

            Assert.Equal(new[] { new[] { "E" }, new[] { "G" } },
                a.GetKeys().Select(fk => fk.Properties.Select(p => p.Name).ToArray()).ToArray());
            Assert.Equal(new string[0][],
                b.GetKeys().Select(fk => fk.Properties.Select(p => p.Name).ToArray()).ToArray());
            Assert.Equal(new[] { "G", "E" }, a.GetProperties().Select(p => p.Name).ToArray());
            Assert.Equal(new[] { "F" }, b.GetProperties().Select(p => p.Name).ToArray());

            b.BaseType = a;

            Assert.Equal(new[] { new[] { "E" }, new[] { "G" } },
                a.GetKeys().Select(fk => fk.Properties.Select(p => p.Name).ToArray()).ToArray());
            Assert.Equal(new[] { new[] { "E" }, new[] { "G" } },
                b.GetKeys().Select(fk => fk.Properties.Select(p => p.Name).ToArray()).ToArray());
            Assert.Equal(new[] { "G", "E" }, a.GetProperties().Select(p => p.Name).ToArray());
            Assert.Equal(new[] { "G", "E", "F" }, b.GetProperties().Select(p => p.Name).ToArray());
            Assert.Equal(new[] { 0, 1, 2 }, b.GetProperties().Select(p => p.GetIndex()));
            Assert.Same(pk, b.FindPrimaryKey(new[] { b.FindProperty("G") }));
            Assert.Same(b.FindKey(b.FindProperty("G")), a.FindKey(a.FindProperty("G")));
        }
示例#18
0
        public void Setting_base_type_throws_when_parent_contains_duplicate_property()
        {
            var model = new Model();

            var a = model.AddEntityType(typeof(A));
            a.AddProperty(A.GProperty);

            var b = model.AddEntityType(typeof(B));
            b.AddProperty(A.GProperty);

            Assert.Equal(
                CoreStrings.DuplicatePropertiesOnBase(typeof(B).FullName, typeof(A).FullName, "G"),
                Assert.Throws<InvalidOperationException>(() => b.BaseType = a).Message);
        }
示例#19
0
        public void Adding_property_throws_when_grandchild_type_has_property_with_same_name()
        {
            var model = new Model();

            var a = model.AddEntityType(typeof(A));

            var c = model.AddEntityType(typeof(C));
            c.BaseType = a;

            var d = model.AddEntityType(typeof(D));
            d.BaseType = c;

            d.AddProperty(A.GProperty);

            Assert.Equal(
                CoreStrings.DuplicateProperty("G", typeof(A).Name, typeof(D).Name),
                Assert.Throws<InvalidOperationException>(() => a.AddProperty(A.GProperty)).Message);
        }
示例#20
0
        public void Adding_a_new_property_with_a_name_that_already_exists_throws()
        {
            var model = new Model();
            var entityType = model.AddEntityType(typeof(Customer));
            entityType.AddProperty(Customer.IdProperty);

            Assert.Equal(
                CoreStrings.DuplicateProperty("Id", typeof(Customer).Name, typeof(Customer).Name),
                Assert.Throws<InvalidOperationException>(() => entityType.AddProperty("Id")).Message);
        }
示例#21
0
        public void Setting_base_type_throws_when_grandchild_contain_duplicate_property()
        {
            var model = new Model();

            var a = model.AddEntityType(typeof(A));
            a.AddProperty(A.EProperty);
            a.AddProperty(A.GProperty);

            var c = model.AddEntityType(typeof(C));

            var d = model.AddEntityType(typeof(D));
            d.AddProperty(A.EProperty);
            d.AddProperty(A.GProperty);
            d.BaseType = c;

            Assert.Equal(
                CoreStrings.DuplicatePropertiesOnBase(typeof(C).FullName, typeof(A).FullName, "E, G"),
                Assert.Throws<InvalidOperationException>(() => c.BaseType = a).Message);
        }
示例#22
0
        public void Adding_a_new_property_with_a_name_that_conflicts_with_a_navigation_throws()
        {
            var model = new Model();
            var customerType = model.AddEntityType(typeof(Customer));
            var customerKey = customerType.GetOrAddKey(customerType.GetOrAddProperty(Customer.IdProperty));

            var orderType = model.AddEntityType(typeof(Order));
            var foreignKeyProperty = orderType.GetOrAddProperty(Order.CustomerIdProperty);
            var customerForeignKey = orderType.GetOrAddForeignKey(foreignKeyProperty, customerKey, customerType);

            customerForeignKey.HasDependentToPrincipal("Customer");

            Assert.Equal(
                CoreStrings.ConflictingNavigation("Customer", typeof(Order).Name, typeof(Order).Name),
                Assert.Throws<InvalidOperationException>(() => orderType.AddProperty("Customer")).Message);
        }
示例#23
0
        public void Keys_added_to_base_type_should_be_inherited()
        {
            var model = new Model();

            var a = model.AddEntityType(typeof(A));
            a.AddProperty(A.GProperty);
            a.AddProperty(A.EProperty);

            var b = model.AddEntityType(typeof(B));
            b.AddProperty(B.FProperty);

            b.BaseType = a;

            a.SetPrimaryKey(a.FindProperty("G"));
            a.AddKey(a.FindProperty("E"));

            Assert.Equal(new[] { new[] { "E" }, new[] { "G" } },
                a.GetKeys().Select(fk => fk.Properties.Select(p => p.Name).ToArray()).ToArray());
            Assert.Equal(new[] { new[] { "E" }, new[] { "G" } },
                b.GetKeys().Select(fk => fk.Properties.Select(p => p.Name).ToArray()).ToArray());
            Assert.Equal(new[] { "G", "E" }, a.GetProperties().Select(p => p.Name).ToArray());
            Assert.Equal(new[] { "G", "E", "F" }, b.GetProperties().Select(p => p.Name).ToArray());
        }
示例#24
0
        public void Adding_a_CLR_property_from_wrong_CLR_type_throws()
        {
            var model = new Model();
            var entityType = model.AddEntityType(typeof(Customer));

            Assert.Equal(
                CoreStrings.PropertyWrongEntityClrType(Order.CustomerIdProperty.Name, typeof(Customer).Name, typeof(Order).Name),
                Assert.Throws<ArgumentException>(() => entityType.GetOrAddProperty(Order.CustomerIdProperty)).Message);
        }
示例#25
0
        public void Adding_keys_throws_when_there_is_a_parent_type()
        {
            var model = new Model();

            var a = model.AddEntityType(typeof(A));
            var b = model.AddEntityType(typeof(B));
            b.BaseType = a;

            Assert.Equal(
                CoreStrings.DerivedEntityTypeKey(typeof(B).FullName),
                Assert.Throws<InvalidOperationException>(() => b.SetPrimaryKey(b.AddProperty("G"))).Message);
            Assert.Equal(
                CoreStrings.DerivedEntityTypeKey(typeof(B).FullName),
                Assert.Throws<InvalidOperationException>(() => b.AddKey(b.AddProperty("E"))).Message);
        }
示例#26
0
        public void Adding_a_CLR_property_to_shadow_type_throws()
        {
            var model = new Model();
            var entityType = model.AddEntityType(typeof(Customer).Name);

            Assert.Equal(
                CoreStrings.ClrPropertyOnShadowEntity(Order.CustomerIdProperty.Name, typeof(Customer).Name),
                Assert.Throws<InvalidOperationException>(() => entityType.GetOrAddProperty(Order.CustomerIdProperty)).Message);
        }
示例#27
0
        public void Navigations_on_base_type_should_be_inherited()
        {
            var model = new Model();

            var customerType = model.AddEntityType(typeof(Customer));
            var customerKey = customerType.GetOrAddKey(customerType.GetOrAddProperty(Customer.IdProperty));

            var orderType = model.AddEntityType(typeof(SpecialOrder));
            var foreignKeyProperty = orderType.GetOrAddProperty(Order.CustomerIdProperty);
            var customerForeignKey = orderType.GetOrAddForeignKey(foreignKeyProperty, customerKey, customerType);

            customerForeignKey.HasPrincipalToDependent("Orders");

            var specialCustomerType = model.AddEntityType(typeof(SpecialCustomer));

            Assert.Equal(new[] { "Orders" }, customerType.GetNavigations().Select(p => p.Name).ToArray());
            Assert.Equal(new string[0], specialCustomerType.GetNavigations().Select(p => p.Name).ToArray());

            specialCustomerType.BaseType = customerType;

            Assert.Equal(new[] { "Orders" }, customerType.GetNavigations().Select(p => p.Name).ToArray());
            Assert.Equal(new[] { "Orders" }, specialCustomerType.GetNavigations().Select(p => p.Name).ToArray());

            var derivedForeignKeyProperty = orderType.GetOrAddProperty(Order.IdProperty);
            var specialCustomerForeignKey = orderType.GetOrAddForeignKey(derivedForeignKeyProperty, customerKey, specialCustomerType);
            specialCustomerForeignKey.HasPrincipalToDependent("DerivedOrders");
            Assert.Equal(new[] { "Orders" }, customerType.GetNavigations().Select(p => p.Name).ToArray());
            Assert.Equal(new[] { "Orders", "DerivedOrders" }, specialCustomerType.GetNavigations().Select(p => p.Name).ToArray());
            Assert.Equal(new[] { "Orders", "DerivedOrders" }, ((IEntityType)specialCustomerType).GetNavigations().Select(p => p.Name).ToArray());
            Assert.Same(customerType.FindNavigation("Orders"), specialCustomerType.FindNavigation("Orders"));
        }
示例#28
0
        public void Can_get_property_indexes()
        {
            var model = new Model();
            var entityType = model.AddEntityType(typeof(Customer));

            entityType.GetOrAddProperty(Customer.NameProperty);
            entityType.AddProperty("Id", typeof(int));
            entityType.AddProperty("Mane", typeof(int));

            Assert.Equal(0, entityType.FindProperty("Id").GetIndex());
            Assert.Equal(1, entityType.FindProperty("Mane").GetIndex());
            Assert.Equal(2, entityType.FindProperty("Name").GetIndex());

            Assert.Equal(0, entityType.FindProperty("Id").GetShadowIndex());
            Assert.Equal(1, entityType.FindProperty("Mane").GetShadowIndex());
            Assert.Equal(-1, entityType.FindProperty("Name").GetShadowIndex());

            Assert.Equal(2, entityType.ShadowPropertyCount());
        }
示例#29
0
        public void Navigations_should_be_updated_when_base_type_changes()
        {
            var model = new Model();

            var customerType = model.AddEntityType(typeof(Customer));
            var customerKey = customerType.GetOrAddKey(customerType.GetOrAddProperty(Customer.IdProperty));

            var orderType = model.AddEntityType(typeof(SpecialOrder));
            var foreignKeyProperty = orderType.GetOrAddProperty(Order.CustomerIdProperty);
            var customerForeignKey = orderType.GetOrAddForeignKey(foreignKeyProperty, customerKey, customerType);

            customerForeignKey.HasPrincipalToDependent("Orders");

            var specialCustomerType = model.AddEntityType(typeof(SpecialCustomer));
            specialCustomerType.BaseType = customerType;

            var derivedForeignKeyProperty = orderType.GetOrAddProperty(Order.IdProperty);
            var specialCustomerForeignKey = orderType.GetOrAddForeignKey(derivedForeignKeyProperty, customerKey, specialCustomerType);
            specialCustomerForeignKey.HasPrincipalToDependent("DerivedOrders");

            specialCustomerType.BaseType = null;

            Assert.Equal(new[] { "Orders" }, customerType.GetNavigations().Select(p => p.Name).ToArray());
            Assert.Equal(new[] { "DerivedOrders" }, specialCustomerType.GetNavigations().Select(p => p.Name).ToArray());
        }
示例#30
0
        public void Indexes_are_rebuilt_when_more_properties_added_or_relevant_state_changes()
        {
            var model = new Model();
            var entityType = model.AddEntityType(typeof(FullNotificationEntity));

            var nameProperty = entityType.AddProperty("Name", typeof(string));
            nameProperty.IsShadowProperty = false;
            var property = entityType.AddProperty("Id", typeof(int)).IsConcurrencyToken = true;

            Assert.Equal(0, entityType.FindProperty("Id").GetIndex());
            Assert.Equal(1, entityType.FindProperty("Name").GetIndex());

            Assert.Equal(0, entityType.FindProperty("Id").GetShadowIndex());
            Assert.Equal(-1, entityType.FindProperty("Name").GetShadowIndex());

            Assert.Equal(0, entityType.FindProperty("Id").GetOriginalValueIndex());
            Assert.Equal(-1, entityType.FindProperty("Name").GetOriginalValueIndex());

            Assert.Equal(1, entityType.ShadowPropertyCount());
            Assert.Equal(1, entityType.OriginalValueCount());

            var gameProperty = entityType.AddProperty("Game", typeof(int));
            gameProperty.IsConcurrencyToken = true;

            var maneProperty = entityType.AddProperty("Mane", typeof(int));
            maneProperty.IsConcurrencyToken = true;

            Assert.Equal(0, entityType.FindProperty("Game").GetIndex());
            Assert.Equal(1, entityType.FindProperty("Id").GetIndex());
            Assert.Equal(2, entityType.FindProperty("Mane").GetIndex());
            Assert.Equal(3, entityType.FindProperty("Name").GetIndex());

            Assert.Equal(0, entityType.FindProperty("Game").GetShadowIndex());
            Assert.Equal(1, entityType.FindProperty("Id").GetShadowIndex());
            Assert.Equal(2, entityType.FindProperty("Mane").GetShadowIndex());
            Assert.Equal(-1, entityType.FindProperty("Name").GetShadowIndex());

            Assert.Equal(0, entityType.FindProperty("Game").GetOriginalValueIndex());
            Assert.Equal(1, entityType.FindProperty("Id").GetOriginalValueIndex());
            Assert.Equal(2, entityType.FindProperty("Mane").GetOriginalValueIndex());
            Assert.Equal(-1, entityType.FindProperty("Name").GetOriginalValueIndex());

            Assert.Equal(3, entityType.ShadowPropertyCount());
            Assert.Equal(3, entityType.OriginalValueCount());

            gameProperty.IsConcurrencyToken = false;
            nameProperty.IsConcurrencyToken = true;

            Assert.Equal(0, entityType.FindProperty("Game").GetIndex());
            Assert.Equal(1, entityType.FindProperty("Id").GetIndex());
            Assert.Equal(2, entityType.FindProperty("Mane").GetIndex());
            Assert.Equal(3, entityType.FindProperty("Name").GetIndex());

            Assert.Equal(0, entityType.FindProperty("Game").GetShadowIndex());
            Assert.Equal(1, entityType.FindProperty("Id").GetShadowIndex());
            Assert.Equal(2, entityType.FindProperty("Mane").GetShadowIndex());
            Assert.Equal(-1, entityType.FindProperty("Name").GetShadowIndex());

            Assert.Equal(-1, entityType.FindProperty("Game").GetOriginalValueIndex());
            Assert.Equal(0, entityType.FindProperty("Id").GetOriginalValueIndex());
            Assert.Equal(1, entityType.FindProperty("Mane").GetOriginalValueIndex());
            Assert.Equal(2, entityType.FindProperty("Name").GetOriginalValueIndex());

            Assert.Equal(3, entityType.ShadowPropertyCount());
            Assert.Equal(3, entityType.OriginalValueCount());

            gameProperty.IsShadowProperty = false;
            nameProperty.IsShadowProperty = true;

            Assert.Equal(0, entityType.FindProperty("Game").GetIndex());
            Assert.Equal(1, entityType.FindProperty("Id").GetIndex());
            Assert.Equal(2, entityType.FindProperty("Mane").GetIndex());
            Assert.Equal(3, entityType.FindProperty("Name").GetIndex());

            Assert.Equal(-1, entityType.FindProperty("Game").GetShadowIndex());
            Assert.Equal(0, entityType.FindProperty("Id").GetShadowIndex());
            Assert.Equal(1, entityType.FindProperty("Mane").GetShadowIndex());
            Assert.Equal(2, entityType.FindProperty("Name").GetShadowIndex());

            Assert.Equal(-1, entityType.FindProperty("Game").GetOriginalValueIndex());
            Assert.Equal(0, entityType.FindProperty("Id").GetOriginalValueIndex());
            Assert.Equal(1, entityType.FindProperty("Mane").GetOriginalValueIndex());
            Assert.Equal(2, entityType.FindProperty("Name").GetOriginalValueIndex());

            Assert.Equal(3, entityType.ShadowPropertyCount());
            Assert.Equal(3, entityType.OriginalValueCount());
        }
示例#31
0
        public void Setting_base_type_throws_when_grandchild_contain_duplicate_navigation()
        {
            var model = new Model();

            var customerType = model.AddEntityType(typeof(Customer));
            var customerKey = customerType.GetOrAddKey(customerType.GetOrAddProperty(Customer.IdProperty));

            var orderType = model.AddEntityType(typeof(Order));
            var foreignKeyProperty = orderType.GetOrAddProperty(Order.CustomerIdProperty);
            var customerForeignKey = orderType.GetOrAddForeignKey(foreignKeyProperty, customerKey, customerType);
            customerForeignKey.HasDependentToPrincipal("Customer");

            var specialOrderType = model.AddEntityType(typeof(SpecialOrder));
            specialOrderType.BaseType = orderType;

            var specialCustomerType = model.AddEntityType(typeof(SpecialCustomer));

            var verySpecialOrderType = model.AddEntityType(typeof(VerySpecialOrder));
            var derivedForeignKeyProperty = verySpecialOrderType.GetOrAddProperty(Order.IdProperty);
            var property = specialCustomerType.AddProperty("AltId", typeof(int));
            var specialCustomerKey = specialCustomerType.GetOrAddKey(property);
            var specialCustomerForeignKey = verySpecialOrderType.GetOrAddForeignKey(derivedForeignKeyProperty, specialCustomerKey, specialCustomerType);
            specialCustomerForeignKey.HasDependentToPrincipal("Customer");

            Assert.Equal(
                CoreStrings.DuplicateNavigationsOnBase(typeof(VerySpecialOrder).FullName, typeof(SpecialOrder).FullName, "Customer"),
                Assert.Throws<InvalidOperationException>(() => verySpecialOrderType.BaseType = specialOrderType).Message);
        }
示例#32
0
        public void Indexes_are_ordered_by_property_count_then_property_names()
        {
            var model = new Model();
            var customerType = model.AddEntityType(typeof(Customer));
            var idProperty = customerType.GetOrAddProperty(Customer.IdProperty);
            var nameProperty = customerType.GetOrAddProperty(Customer.NameProperty);
            var otherProperty = customerType.AddProperty("OtherProperty", typeof(string));

            var i2 = customerType.AddIndex(nameProperty);
            var i4 = customerType.AddIndex(new[] { idProperty, otherProperty });
            var i3 = customerType.AddIndex(new[] { idProperty, nameProperty });
            var i1 = customerType.AddIndex(idProperty);

            Assert.True(new[] { i1, i2, i3, i4 }.SequenceEqual(customerType.GetIndexes()));
        }
示例#33
0
        // Issue #2514
        //[Fact]
        public void Adding_foreignKey_throws_when_grandchild_type_has_foreignKey_on_same_properties()
        {
            var model = new Model();

            var customerType = model.AddEntityType(typeof(Customer));
            var customerKey = customerType.GetOrAddKey(customerType.GetOrAddProperty(Customer.IdProperty));

            var orderType = model.AddEntityType(typeof(Order));
            var foreignKeyProperty = orderType.GetOrAddProperty(Order.CustomerIdProperty);

            var specialOrderType = model.AddEntityType(typeof(SpecialOrder));
            specialOrderType.BaseType = orderType;

            var verySpecialOrderType = model.AddEntityType(typeof(VerySpecialOrder));
            verySpecialOrderType.BaseType = specialOrderType;
            verySpecialOrderType.GetOrAddForeignKey(foreignKeyProperty, customerKey, customerType);

            Assert.Equal(
                CoreStrings.DuplicateForeignKey(Property.Format(new[] { foreignKeyProperty }), typeof(Order).FullName, typeof(VerySpecialOrder).FullName),
                Assert.Throws<InvalidOperationException>(() =>
                    orderType.GetOrAddForeignKey(foreignKeyProperty, customerKey, customerType)).Message);
        }
示例#34
0
        public void Only_required_properties_have_original_value_indexes_when_using_lazy_original_values()
        {
            var model = new Model();
            var entityType = model.AddEntityType(typeof(FullNotificationEntity));

            entityType.AddProperty(FullNotificationEntity.NameProperty).IsConcurrencyToken = true;
            entityType.AddProperty(FullNotificationEntity.IdProperty);

            Assert.Equal(-1, entityType.FindProperty("Id").GetOriginalValueIndex());
            Assert.Equal(0, entityType.FindProperty("Name").GetOriginalValueIndex());

            Assert.Equal(1, entityType.OriginalValueCount());
        }
示例#35
0
        public void Setting_shadow_base_for_CLR_entity_type_should_throw()
        {
            var model = new Model();

            var a = model.AddEntityType(typeof(A).Name);
            var b = model.AddEntityType(typeof(B));

            Assert.Equal(
                CoreStrings.NonClrBaseType(b, a),
                Assert.Throws<InvalidOperationException>(() => b.BaseType = a).Message);
        }
示例#36
0
        public void FK_properties_are_marked_as_requiring_original_values()
        {
            var model = new Model();
            var entityType = model.AddEntityType(typeof(FullNotificationEntity));
            var key = entityType.GetOrSetPrimaryKey(entityType.AddProperty(FullNotificationEntity.IdProperty));
            var fkProperty = entityType.AddProperty("Fk", typeof(int));

            Assert.Equal(-1, fkProperty.GetOriginalValueIndex());

            entityType.GetOrAddForeignKey(new[] { fkProperty }, key, entityType);

            Assert.Equal(0, fkProperty.GetOriginalValueIndex());
        }