当前位置: 首页>>代码示例>>C#>>正文


C# EntityType.AddForeignKey方法代码示例

本文整理汇总了C#中EntityType.AddForeignKey方法的典型用法代码示例。如果您正苦于以下问题:C# EntityType.AddForeignKey方法的具体用法?C# EntityType.AddForeignKey怎么用?C# EntityType.AddForeignKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在EntityType的用法示例。


在下文中一共展示了EntityType.AddForeignKey方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Get_generation_property_returns_generation_property_from_foreign_key_tree

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

            var leftType = new EntityType("Left", model);
            var leftId = leftType.AddProperty("Id", typeof(int), true);
            var leftKey = leftType.AddKey(leftId);

            var rightType = new EntityType("Right", model);
            var rightId1 = rightType.AddProperty("Id1", typeof(int), true);
            var rightId2 = rightType.AddProperty("Id2", typeof(int), true);
            var rightKey = rightType.AddKey(new[] { rightId1, rightId2 });

            var middleType = new EntityType("Middle", model);
            var middleProperty1 = middleType.AddProperty("FK1", typeof(int), true);
            var middleProperty2 = middleType.AddProperty("FK2", typeof(int), true);
            var middleKey1 = middleType.AddKey(middleProperty1);
            var middleFK1 = middleType.AddForeignKey(middleProperty1, leftKey, leftType);
            var middleFK2 = middleType.AddForeignKey(new[] { middleProperty2, middleProperty1 }, rightKey, rightType);

            var endType = new EntityType("End", model);
            var endProperty = endType.AddProperty("FK", typeof(int), true);

            var endFK = endType.AddForeignKey(endProperty, middleKey1, middleType);

            rightId2.RequiresValueGenerator = true;

            Assert.Equal(rightId2, endProperty.GetGenerationProperty());
        }
开发者ID:JamesWang007,项目名称:EntityFramework,代码行数:29,代码来源:PropertyExtensionsTest.cs

示例2: BuildModel

        protected override Model BuildModel()
        {
            var model = new Model();

            var entityType1 = new EntityType("SomeEntity");
            model.AddEntityType(entityType1);
            var key1 = entityType1.AddProperty("Id", typeof(int), shadowProperty: true, concurrencyToken: false);
            key1.ValueGenerationOnSave = ValueGenerationOnSave.WhenInserting;
            key1.ValueGenerationOnAdd = ValueGenerationOnAdd.Client;
            entityType1.SetKey(key1);
            entityType1.AddProperty("Name", typeof(string), shadowProperty: true, concurrencyToken: true);

            var entityType2 = new EntityType("SomeDependentEntity");
            model.AddEntityType(entityType2);
            var key2a = entityType2.AddProperty("Id1", typeof(int), shadowProperty: true, concurrencyToken: false);
            var key2b = entityType2.AddProperty("Id2", typeof(string), shadowProperty: true, concurrencyToken: false);
            entityType2.SetKey(key2a, key2b);
            var fk = entityType2.AddProperty("SomeEntityId", typeof(int), shadowProperty: true, concurrencyToken: false);
            entityType2.AddForeignKey(entityType1.GetKey(), new[] { fk });
            var justAProperty = entityType2.AddProperty("JustAProperty", typeof(int), shadowProperty: true, concurrencyToken: false);
            justAProperty.ValueGenerationOnSave = ValueGenerationOnSave.WhenInserting;
            justAProperty.ValueGenerationOnAdd = ValueGenerationOnAdd.Client;

            var entityType3 = new EntityType(typeof(FullNotificationEntity));
            model.AddEntityType(entityType3);
            entityType3.SetKey(entityType3.AddProperty("Id", typeof(int), shadowProperty: true, concurrencyToken: false));
            entityType3.AddProperty("Name", typeof(string), shadowProperty: true, concurrencyToken: true);

            var entityType4 = new EntityType(typeof(ChangedOnlyEntity));
            model.AddEntityType(entityType4);
            entityType4.SetKey(entityType4.AddProperty("Id", typeof(int), shadowProperty: true, concurrencyToken: false));
            entityType4.AddProperty("Name", typeof(string), shadowProperty: true, concurrencyToken: true);

            var entityType5 = new EntityType("SomeMoreDependentEntity");
            model.AddEntityType(entityType5);
            var key5 = entityType5.AddProperty("Id", typeof(int), shadowProperty: true, concurrencyToken: false);
            entityType5.SetKey(key5);
            var fk5a = entityType5.AddProperty("Fk1", typeof(int), shadowProperty: true, concurrencyToken: false);
            var fk5b = entityType5.AddProperty("Fk2", typeof(string), shadowProperty: true, concurrencyToken: false);
            entityType5.AddForeignKey(entityType2.GetKey(), new[] { fk5a, fk5b });

            return model;
        }
开发者ID:Nyaoso,项目名称:EntityFramework,代码行数:43,代码来源:ShadowStateEntryTest.cs

示例3: Get_generation_property_returns_generation_property_from_foreign_key_chain

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

            var firstType = new EntityType("First", model);
            var firstProperty = firstType.AddProperty("ID", typeof(int), true);
            var firstKey = firstType.AddKey(firstProperty);

            var secondType = new EntityType("Second", model);
            var secondProperty = secondType.AddProperty("ID", typeof(int), true);
            var secondKey = secondType.AddKey(secondProperty);
            var secondForeignKey = secondType.AddForeignKey(secondProperty, firstKey, firstType);

            var thirdType = new EntityType("Third", model);
            var thirdProperty = thirdType.AddProperty("ID", typeof(int), true);
            var thirdForeignKey = thirdType.AddForeignKey(thirdProperty, secondKey, secondType);

            firstProperty.RequiresValueGenerator = true;

            Assert.Equal(firstProperty, thirdProperty.GetGenerationProperty());
        }
开发者ID:JamesWang007,项目名称:EntityFramework,代码行数:21,代码来源:PropertyExtensionsTest.cs

示例4: Get_generation_property_returns_generation_property_from_foreign_key_graph_with_cycle

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

            var leafType = new EntityType("leaf", model);
            var leafId1 = leafType.AddProperty("Id1", typeof(int), true);
            var leafId2 = leafType.AddProperty("Id2", typeof(int), true);
            var leafKey = leafType.AddKey(new[] { leafId1, leafId2 });

            var firstType = new EntityType("First", model);
            var firstId = firstType.AddProperty("Id", typeof(int), true);
            var firstKey = firstType.AddKey(firstId);
            
            var secondType = new EntityType("Second", model);
            var secondId1 = secondType.AddProperty("Id1", typeof(int), true);
            var secondId2 = secondType.AddProperty("Id2", typeof(int), true);
            var secondKey = secondType.AddKey(secondId1);

            var firstForeignKey = firstType.AddForeignKey(firstId, secondKey, secondType);
            var secondForeignKey1 = secondType.AddForeignKey(secondId1, firstKey, firstType);
            var secondForeignKey2 = secondType.AddForeignKey(new[] { secondId1, secondId2 }, leafKey, leafType);

            leafId1.RequiresValueGenerator = true;

            Assert.Equal(leafId1, secondId1.GetGenerationProperty());
        }
开发者ID:JamesWang007,项目名称:EntityFramework,代码行数:26,代码来源:PropertyExtensionsTest.cs

示例5: Setting_foreign_key_properties_should_update_existing_properties

        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());
        }
开发者ID:Nyaoso,项目名称:EntityFramework,代码行数:12,代码来源:EntityTypeTest.cs

示例6: BuildModel

        private static Model BuildModel()
        {
            var model = new Model();

            var entityType = new EntityType(typeof(Banana));
            var property1 = entityType.AddProperty("P1", typeof(int));
            var property2 = entityType.AddProperty("P2", typeof(string));
            var property3 = entityType.AddProperty("P3", typeof(Random));
            var property4 = entityType.AddProperty("P4", typeof(int));
            var property5 = entityType.AddProperty("P5", typeof(string));
            var property6 = entityType.AddProperty("P6", typeof(Random));

            entityType.SetKey(property1, property2, property3);
            entityType.AddForeignKey(entityType.GetKey(), property6, property4, property5);

            model.AddEntityType(entityType);

            return model;
        }
开发者ID:Nyaoso,项目名称:EntityFramework,代码行数:19,代码来源:CompositeEntityKeyFactoryTest.cs

示例7: Diff_finds_created_table

        public void Diff_finds_created_table()
        {
            var sourceModel = CreateModel();
            var targetModel = CreateModel();

            var principalEntityType = targetModel.GetEntityType("Principal");
            var dependentEntityType = new EntityType("NewDependent");
            dependentEntityType.SetTableName("MyNewTable");
            dependentEntityType.SetSchema("dbo");
            var dependentProperty = dependentEntityType.AddProperty("Id", typeof(int));

            targetModel.AddEntityType(dependentEntityType);

            dependentProperty.Annotations.Add(new Annotation(
                MetadataExtensions.Annotations.StorageTypeName, "int"));

            dependentEntityType.SetKey(dependentProperty);
            dependentEntityType.GetKey().SetKeyName("MyNewPK");

            var foreignKey = dependentEntityType.AddForeignKey(principalEntityType.GetKey(), dependentProperty);
            foreignKey.SetKeyName("MyNewFK");
            foreignKey.Annotations.Add(new Annotation(
                MetadataExtensions.Annotations.CascadeDelete, "True"));

            var operations = new ModelDiffer(new DatabaseBuilder()).Diff(sourceModel, targetModel);

            Assert.Equal(2, operations.Count);
            Assert.IsType<CreateTableOperation>(operations[0]);
            Assert.IsType<AddForeignKeyOperation>(operations[1]);

            var createTableOperation = (CreateTableOperation)operations[0];

            Assert.Equal("dbo.MyNewTable", createTableOperation.Table.Name);
            Assert.Equal(1, createTableOperation.Table.Columns.Count);
            Assert.Equal("Id", createTableOperation.Table.Columns[0].Name);
            Assert.Equal(typeof(int), createTableOperation.Table.Columns[0].ClrType);
            Assert.NotNull(createTableOperation.Table.PrimaryKey);
            Assert.Equal("MyNewPK", createTableOperation.Table.PrimaryKey.Name);
            Assert.Equal(1, createTableOperation.Table.ForeignKeys.Count);
            Assert.Equal("MyNewFK", createTableOperation.Table.ForeignKeys[0].Name);
            Assert.True(createTableOperation.Table.ForeignKeys[0].CascadeDelete);

            var addForeignKeyOperation = (AddForeignKeyOperation)operations[1];

            Assert.Equal("MyNewFK", addForeignKeyOperation.ForeignKeyName);
            Assert.Equal("dbo.MyNewTable", addForeignKeyOperation.TableName);
            Assert.Equal("dbo.MyTable1", addForeignKeyOperation.ReferencedTableName);
            Assert.Equal(new[] { "Id" }, addForeignKeyOperation.ColumnNames);
            Assert.Equal(new[] { "Id" }, addForeignKeyOperation.ReferencedColumnNames);
            Assert.True(addForeignKeyOperation.CascadeDelete);
        }
开发者ID:Nyaoso,项目名称:EntityFramework,代码行数:51,代码来源:ModelDifferTest.cs

示例8: BuildModel

        private static Model BuildModel()
        {
            var model = new Model();

            var entityType = new EntityType(typeof(Banana));
            var pkProperty = entityType.AddProperty("Id", typeof(int));
            var fkProperty = entityType.AddProperty("Fk", typeof(int));

            entityType.SetKey(pkProperty);
            var fk = entityType.AddForeignKey(entityType.GetKey(), fkProperty);

            entityType.AddProperty("Name", typeof(string));

            model.AddEntityType(entityType);

            entityType.AddNavigation(new Navigation(fk, "LesserBananas", pointsToPrincipal: false));
            entityType.AddNavigation(new Navigation(fk, "TopBanana", pointsToPrincipal: true));

            return model;
        }
开发者ID:Nyaoso,项目名称:EntityFramework,代码行数:20,代码来源:RelationshipsSnapshotTest.cs

示例9: Can_get_or_add_a_foreign_key

        public void Can_get_or_add_a_foreign_key()
        {
            var customerType = new EntityType(typeof(Customer), new Model());
            var idProperty = customerType.GetOrAddProperty(Customer.IdProperty);
            var customerKey = customerType.GetOrAddKey(idProperty);
            var orderType = new EntityType(typeof(Order), new Model());
            var customerFk1 = orderType.GetOrAddProperty(Order.CustomerIdProperty);
            var customerFk2 = orderType.GetOrAddProperty("IdAgain", typeof(int), shadowProperty: true);
            var fk1 = orderType.AddForeignKey(customerFk1, customerKey);

            var fk2 = orderType.GetOrAddForeignKey(customerFk2, customerKey);

            Assert.NotNull(fk2);
            Assert.NotEqual(fk1, fk2);
            Assert.Same(fk2, orderType.GetForeignKey(customerFk2));
            Assert.Same(fk2, orderType.FindForeignKey(customerFk2));
            Assert.Equal(new[] { fk1, fk2 }, orderType.ForeignKeys.ToArray());
            Assert.Same(fk2, orderType.GetOrAddForeignKey(customerFk2, customerKey));
            Assert.Equal(new[] { fk1, fk2 }, orderType.ForeignKeys.ToArray());
        }
开发者ID:thegido,项目名称:EntityFramework,代码行数:20,代码来源:EntityTypeTest.cs

示例10: Adding_a_foreign_key_throws_if_properties_from_different_type

        public void Adding_a_foreign_key_throws_if_properties_from_different_type()
        {
            var entityType1 = new EntityType(typeof(Customer), new Model());
            var entityType2 = new EntityType(typeof(Order), new Model());
            var idProperty = entityType2.GetOrAddProperty(Customer.IdProperty);

            Assert.Equal(
                Strings.ForeignKeyPropertiesWrongEntity("{'" + Customer.IdProperty.Name + "'}", typeof(Customer).FullName),
                Assert.Throws<ArgumentException>(() => entityType1.AddForeignKey(new[] { idProperty }, entityType2.GetOrAddKey(idProperty))).Message);
        }
开发者ID:thegido,项目名称:EntityFramework,代码行数:10,代码来源:EntityTypeTest.cs

示例11: Adding_a_foreign_key_throws_if_duplicate

        public void Adding_a_foreign_key_throws_if_duplicate()
        {
            var customerType = new EntityType(typeof(Customer), new Model());
            var customerKey = customerType.GetOrAddKey(customerType.GetOrAddProperty(Customer.IdProperty));
            var orderType = new EntityType(typeof(Order), new Model());
            var customerFk1 = orderType.GetOrAddProperty(Order.CustomerIdProperty);
            orderType.AddForeignKey(customerFk1, customerKey);

            Assert.Equal(
                Strings.DuplicateForeignKey("{'" + Order.CustomerIdProperty.Name + "'}", typeof(Order).FullName),
                Assert.Throws<InvalidOperationException>(() => orderType.AddForeignKey(customerFk1, customerKey)).Message);
        }
开发者ID:thegido,项目名称:EntityFramework,代码行数:12,代码来源:EntityTypeTest.cs

示例12: Navigations_are_ordered_by_name

        public void Navigations_are_ordered_by_name()
        {
            var customerType = new EntityType(typeof(Customer), new Model());
            var customerKey = customerType.GetOrAddKey(customerType.GetOrAddProperty(Customer.IdProperty));

            var orderType = new EntityType(typeof(Order), new Model());
            var customerForeignKeyProperty = orderType.AddProperty(Order.CustomerIdProperty);
            var customerForeignKey = orderType.AddForeignKey(customerForeignKeyProperty, customerKey);

            var specialOrderType = new EntityType(typeof(SpecialOrder), new Model());
            var specialCustomerForeignKeyProperty = specialOrderType.AddProperty(Order.CustomerIdProperty);
            var specialCustomerForeignKey = specialOrderType.AddForeignKey(specialCustomerForeignKeyProperty, customerKey);

            var navigation2 = customerType.AddNavigation("Orders", customerForeignKey, pointsToPrincipal: false);
            var navigation1 = customerType.AddNavigation("DerivedOrders", specialCustomerForeignKey, pointsToPrincipal: false);

            Assert.True(new[] { navigation1, navigation2 }.SequenceEqual(customerType.Navigations));
        }
开发者ID:thegido,项目名称:EntityFramework,代码行数:18,代码来源:EntityTypeTest.cs

示例13: FK_properties_are_marked_as_requiring_original_values

        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);
        }
开发者ID:Nyaoso,项目名称:EntityFramework,代码行数:11,代码来源:EntityTypeTest.cs

示例14: FK_back_pointer_is_fixed_up_as_FK_is_added

        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);
        }
开发者ID:Nyaoso,项目名称:EntityFramework,代码行数:19,代码来源:EntityTypeTest.cs

示例15: BuildModel

        private static IModel BuildModel()
        {
            var model = new Model();
            var builder = new ModelBuilder(model);

            builder.Annotation("ModelAnnotation1", "ModelValue1");
            builder.Annotation("ModelAnnotation2", "ModelValue2");

            var entityType1 = new EntityType(typeof(KoolEntity1));
            var property = entityType1.AddProperty("Id1", typeof(int));
            entityType1.SetKey(property);
            entityType1.AddProperty("Id2", typeof(Guid));
            entityType1.AddProperty("KoolEntity2Id", typeof(int));
            model.AddEntityType(entityType1);

            var entityType2 = new EntityType(typeof(KoolEntity2));
            entityType2.AddProperty("KoolEntity1Id1", typeof(int));
            entityType2.AddProperty("KoolEntity1Id2", typeof(Guid));
            entityType2.AddProperty("KoolEntity3Id", typeof(int));
            model.AddEntityType(entityType2);

            var entityType3 = new EntityType(typeof(KoolEntity3));
            entityType3.AddProperty("KoolEntity4Id", typeof(int));
            model.AddEntityType(entityType3);

            var entityType4 = new EntityType(typeof(KoolEntity4));
            model.AddEntityType(entityType4);

            var entityType5 = new EntityType(typeof(KoolEntity5));
            model.AddEntityType(entityType5);

            var entityType6 = new EntityType(typeof(KoolEntity6));
            entityType6.AddProperty("Kool5Id", typeof(int));
            model.AddEntityType(entityType6);

            for (var i = 7; i <= 20; i++)
            {
                var type = Type.GetType("Microsoft.Data.Entity.FunctionalTests.Metadata.KoolEntity" + i);

                Assert.NotNull(type);

                model.AddEntityType(new EntityType(type));
            }

            for (var i = 2; i <= 20; i++)
            {
                var type = Type.GetType("Microsoft.Data.Entity.FunctionalTests.Metadata.KoolEntity" + i);

                var entityType = model.GetEntityType(type);
                var id = entityType.AddProperty(entityType.Type.GetProperty("Id"));
                entityType.SetKey(id);
            }

            for (var i = 1; i <= 20; i++)
            {
                var type = Type.GetType("Microsoft.Data.Entity.FunctionalTests.Metadata.KoolEntity" + i);

                var entityType = model.GetEntityType(type);

                entityType.Annotations.Add(new Annotation("Annotation1", "Value1"));
                entityType.Annotations.Add(new Annotation("Annotation2", "Value2"));

                var foo = entityType.AddProperty(entityType.Type.GetProperty("Foo" + i));

                foo.Annotations.Add(new Annotation("Foo" + i + "Annotation1", "Foo" + i + "Value1"));
                foo.Annotations.Add(new Annotation("Foo" + i + "Annotation2", "Foo" + i + "Value2"));

                var goo = entityType.AddProperty(entityType.Type.GetProperty("Goo" + i));
            }

            var fk11 = entityType1.AddForeignKey(entityType2.GetKey(), new[] { entityType1.GetProperty("KoolEntity2Id") });
            var fk21 = entityType2.AddForeignKey(entityType1.GetKey(), new[] { entityType2.GetProperty("KoolEntity1Id1") });
            var fk22 = entityType2.AddForeignKey(entityType3.GetKey(), new[] { entityType2.GetProperty("KoolEntity3Id") });
            var fk31 = entityType3.AddForeignKey(entityType4.GetKey(), new[] { entityType3.GetProperty("KoolEntity4Id") });
            var fk61 = entityType6.AddForeignKey(entityType5.GetKey(), new[] { entityType6.GetProperty("Kool5Id") });

            entityType1.AddNavigation(new Navigation(fk11, "NavTo2", pointsToPrincipal: true));
            entityType1.AddNavigation(new Navigation(fk21, "NavTo2s", pointsToPrincipal: false));
            entityType2.AddNavigation(new Navigation(fk21, "NavTo1", pointsToPrincipal: true));
            entityType2.AddNavigation(new Navigation(fk11, "NavTo1s", pointsToPrincipal: false));
            entityType2.AddNavigation(new Navigation(fk22, "NavTo3", pointsToPrincipal: true));
            entityType3.AddNavigation(new Navigation(fk22, "NavTo2s", pointsToPrincipal: false));
            entityType3.AddNavigation(new Navigation(fk31, "NavTo4", pointsToPrincipal: true));
            entityType4.AddNavigation(new Navigation(fk31, "NavTo3s", pointsToPrincipal: false));
            entityType5.AddNavigation(new Navigation(fk61, "Kool6s", pointsToPrincipal: false));
            entityType6.AddNavigation(new Navigation(fk61, "Kool5", pointsToPrincipal: true));

            return model;
        }
开发者ID:Nyaoso,项目名称:EntityFramework,代码行数:89,代码来源:CompiledModelTest.cs


注:本文中的EntityType.AddForeignKey方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。