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


C# Model.GetEntityType方法代码示例

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


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

示例1: Finds_existing_navigation_to_principal_and_uses_associated_FK

            public virtual void Finds_existing_navigation_to_principal_and_uses_associated_FK()
            {
                var model = new Model();
                var modelBuilder = CreateModelBuilder(model);
                modelBuilder.Entity<Customer>();
                modelBuilder
                    .Entity<Order>().HasOne(o => o.Customer).WithMany()
                    .ForeignKey(c => c.CustomerId);
                modelBuilder.Ignore<OrderDetails>();
                modelBuilder.Ignore<CustomerDetails>();

                var dependentType = model.GetEntityType(typeof(Order));
                var principalType = model.GetEntityType(typeof(Customer));

                var principalKey = principalType.GetKeys().Single();
                var dependentKey = dependentType.GetKeys().Single();

                modelBuilder.Entity<Order>().HasOne(e => e.Customer).WithMany(e => e.Orders);

                var newFk = dependentType.Navigations.Single().ForeignKey;
                Assert.Equal(nameof(Order.CustomerId), newFk.Properties.Single().Name);
                Assert.Same(newFk, principalType.Navigations.Single().ForeignKey);
                Assert.Empty(principalType.GetForeignKeys());
                Assert.Same(principalKey, principalType.GetKeys().Single());
                Assert.Same(dependentKey, dependentType.GetKeys().Single());
                Assert.Same(principalKey, principalType.GetPrimaryKey());
                Assert.Same(dependentKey, dependentType.GetPrimaryKey());
            }
开发者ID:491134648,项目名称:EntityFramework,代码行数:28,代码来源:ManyToOneTestBase.cs

示例2: Replaces_existing_navigation_to_principal

            public virtual void Replaces_existing_navigation_to_principal()
            {
                var model = new Model();
                var modelBuilder = CreateModelBuilder(model);
                modelBuilder.Entity<CustomerDetails>().Reference(c => c.Customer).InverseReference();
                modelBuilder.Ignore<Order>();

                var dependentType = model.GetEntityType(typeof(CustomerDetails));
                var principalType = model.GetEntityType(typeof(Customer));
                var expectedPrincipalProperties = principalType.Properties.ToList();
                var expectedDependentProperties = dependentType.Properties.ToList();

                var principalKey = principalType.GetKeys().Single();
                var dependentKey = dependentType.GetKeys().Single();

                modelBuilder.Entity<Customer>().Reference(e => e.Details).InverseReference(e => e.Customer);

                var fk = dependentType.GetForeignKeys().Single();
                Assert.Same(fk.DependentToPrincipal, dependentType.Navigations.Single());
                Assert.Same(fk.PrincipalToDependent, principalType.Navigations.Single());
                AssertEqual(expectedPrincipalProperties, principalType.Properties);
                AssertEqual(expectedDependentProperties, dependentType.Properties);
                Assert.Empty(principalType.GetForeignKeys());
                Assert.Same(principalKey, principalType.GetKeys().Single());
                Assert.Same(dependentKey, dependentType.GetKeys().Single());
                Assert.Same(principalKey, principalType.GetPrimaryKey());
                Assert.Same(dependentKey, dependentType.GetPrimaryKey());
            }
开发者ID:JamesWang007,项目名称:EntityFramework,代码行数:28,代码来源:OneToOneTestBase.cs

示例3: Finds_existing_navigations_and_uses_associated_FK

            public virtual void Finds_existing_navigations_and_uses_associated_FK()
            {
                var model = new Model();
                var modelBuilder = CreateModelBuilder(model);
                modelBuilder.Entity<Customer>();
                modelBuilder
                    .Entity<CustomerDetails>().HasOne(d => d.Customer).WithOne(c => c.Details)
                    .ForeignKey<CustomerDetails>(c => c.Id);
                modelBuilder.Ignore<Order>();

                var dependentType = model.GetEntityType(typeof(CustomerDetails));
                var principalType = model.GetEntityType(typeof(Customer));
                var fk = dependentType.GetForeignKeys().Single();

                var navToPrincipal = dependentType.GetNavigation(nameof(CustomerDetails.Customer));
                var navToDependent = principalType.GetNavigation(nameof(Customer.Details));

                var principalKey = principalType.GetKeys().Single();
                var dependentKey = dependentType.GetKeys().Single();

                modelBuilder.Entity<Customer>().HasOne(e => e.Details).WithOne(e => e.Customer);

                Assert.Equal(1, dependentType.GetForeignKeys().Count());
                Assert.Same(navToPrincipal, dependentType.Navigations.Single());
                Assert.Same(navToDependent, principalType.Navigations.Single());
                Assert.Same(fk.PrincipalKey, principalType.Navigations.Single().ForeignKey.PrincipalKey);
                AssertEqual(new[] { "AlternateKey", principalKey.Properties.Single().Name, Customer.NameProperty.Name }, principalType.Properties.Select(p => p.Name));
                AssertEqual(new[] { dependentKey.Properties.Single().Name }, dependentType.Properties.Select(p => p.Name));
                Assert.Empty(principalType.GetForeignKeys());
                Assert.Same(principalKey, principalType.GetKeys().Single());
                Assert.Same(dependentKey, dependentType.GetKeys().Single());
                Assert.Same(principalKey, principalType.GetPrimaryKey());
                Assert.Same(dependentKey, dependentType.GetPrimaryKey());
            }
开发者ID:491134648,项目名称:EntityFramework,代码行数:34,代码来源:OneToOneTestBase.cs

示例4: OneToMany_finds_existing_nav_to_principal_and_uses_associated_FK

        public void OneToMany_finds_existing_nav_to_principal_and_uses_associated_FK()
        {
            var model = new Model();
            var modelBuilder = new ModelBuilder(new ConventionSet(), model);
            modelBuilder.Entity<Customer>().Key(c => c.Id);
            modelBuilder.Entity<Order>().Metadata.AddForeignKey(
                model.GetEntityType(typeof(Order)).GetProperty("CustomerId"),
                model.GetEntityType(typeof(Customer)).GetPrimaryKey());

            var dependentType = model.GetEntityType(typeof(Order));
            var principalType = model.GetEntityType(typeof(Customer));
            var fk = dependentType.GetForeignKeys().Single();

            var navigation = dependentType.AddNavigation("Customer", fk, true);

            var principalPropertyCount = principalType.PropertyCount;
            var dependentPropertyCount = dependentType.PropertyCount;
            var principalKey = principalType.GetKeys().Single();
            var dependentKey = dependentType.GetKeys().Single();

            modelBuilder.Entity(typeof(Customer)).Collection(typeof(Order), "Orders").InverseReference("Customer");

            Assert.Same(fk, dependentType.GetForeignKeys().Single());
            Assert.Same(navigation, dependentType.Navigations.Single());
            Assert.Equal("Orders", principalType.Navigations.Single().Name);
            Assert.Same(fk, dependentType.Navigations.Single().ForeignKey);
            Assert.Same(fk, principalType.Navigations.Single().ForeignKey);
            Assert.Equal(principalPropertyCount, principalType.PropertyCount);
            Assert.Equal(dependentPropertyCount, dependentType.PropertyCount);
            Assert.Empty(principalType.GetForeignKeys());
            Assert.Same(principalKey, principalType.GetKeys().Single());
            Assert.Same(dependentKey, dependentType.GetKeys().Single());
            Assert.Same(principalKey, principalType.GetPrimaryKey());
            Assert.Same(dependentKey, dependentType.GetPrimaryKey());
        }
开发者ID:aishaloshik,项目名称:EntityFramework,代码行数:35,代码来源:NonGenericRelationshipBuilderTest.cs

示例5: Finds_existing_navigation_to_principal_and_uses_associated_FK

            public virtual void Finds_existing_navigation_to_principal_and_uses_associated_FK()
            {
                var model = new Model();
                var modelBuilder = CreateModelBuilder(model);
                modelBuilder.Entity<Customer>();
                modelBuilder
                    .Entity<Order>().Reference(c => c.Customer).InverseCollection()
                    .ForeignKey(c => c.CustomerId);
                modelBuilder.Ignore<OrderDetails>();
                modelBuilder.Ignore<CustomerDetails>();

                var dependentType = model.GetEntityType(typeof(Order));
                var principalType = model.GetEntityType(typeof(Customer));
                var fk = dependentType.GetForeignKeys().Single();

                var navigation = dependentType.GetNavigation("Customer");

                var principalKey = principalType.GetKeys().Single();
                var dependentKey = dependentType.GetKeys().Single();

                modelBuilder.Entity<Customer>().Collection(e => e.Orders).InverseReference(e => e.Customer);

                Assert.Equal(1, dependentType.GetForeignKeys().Count());
                Assert.Equal("Orders", principalType.Navigations.Single().Name);
                Assert.Same(fk.PrincipalKey, principalType.Navigations.Single().ForeignKey.PrincipalKey);
                AssertEqual(new[] { "AlternateKey", principalKey.Properties.Single().Name, Customer.NameProperty.Name }, principalType.Properties.Select(p => p.Name));
                AssertEqual(new[] { "AnotherCustomerId", fk.Properties.Single().Name, dependentKey.Properties.Single().Name }, dependentType.Properties.Select(p => p.Name));
                Assert.Empty(principalType.GetForeignKeys());
                Assert.Same(principalKey, principalType.GetKeys().Single());
                Assert.Same(dependentKey, dependentType.GetKeys().Single());
                Assert.Same(principalKey, principalType.GetPrimaryKey());
                Assert.Same(dependentKey, dependentType.GetPrimaryKey());
            }
开发者ID:rbenhassine2,项目名称:EntityFramework,代码行数:33,代码来源:OneToManyTestBase.cs

示例6: NotMappedAttribute_removes_ambiguity_in_conventional_relationship_building

            public virtual void NotMappedAttribute_removes_ambiguity_in_conventional_relationship_building()
            {
                var model = new Model();
                var modelBuilder = CreateModelBuilder(model);
                modelBuilder.Entity<Book>();

                Assert.Contains("Details", model.GetEntityType(typeof(Book)).Navigations.Select(nav => nav.Name));
                Assert.Contains("AnotherBook", model.GetEntityType(typeof(BookDetails)).Navigations.Select(nav => nav.Name));
                Assert.DoesNotContain("Book", model.GetEntityType(typeof(BookDetails)).Navigations.Select(nav => nav.Name));
            }
开发者ID:rbenhassine2,项目名称:EntityFramework,代码行数:10,代码来源:DataAnnotationsTestBase.cs

示例7: InversePropertyAttribute_removes_ambiguity_in_conventional_relationalship_building

            public virtual void InversePropertyAttribute_removes_ambiguity_in_conventional_relationalship_building()
            {
                var model = new Model();
                var modelBuilder = CreateModelBuilder(model);
                modelBuilder.Entity<Book>();

                Assert.Contains("Book", model.GetEntityType(typeof(BookLabel)).Navigations.Select(nav => nav.Name));
                Assert.Equal("Label", model.GetEntityType(typeof(BookLabel)).FindNavigation("Book").ForeignKey.PrincipalToDependent.Name);

                Assert.Contains("AlternateLabel", model.GetEntityType(typeof(Book)).Navigations.Select(nav => nav.Name));
                Assert.Null(model.GetEntityType(typeof(Book)).FindNavigation("AlternateLabel").ForeignKey.PrincipalToDependent);
            }
开发者ID:rbenhassine2,项目名称:EntityFramework,代码行数:12,代码来源:DataAnnotationsTestBase.cs

示例8: ForeignKeyAttribute_creates_two_relationships_if_applied_on_property_on_both_side

            public virtual void ForeignKeyAttribute_creates_two_relationships_if_applied_on_property_on_both_side()
            {
                var model = new Model();
                var modelBuilder = CreateModelBuilder(model);
                modelBuilder.Entity<Post>();

                Assert.Null(model.GetEntityType(typeof(Post)).FindNavigation("Author").ForeignKey.PrincipalToDependent);
                Assert.Equal("AuthorCode", model.GetEntityType(typeof(Post)).FindNavigation("Author").ForeignKey.Properties.First().Name);

                Assert.Null(model.GetEntityType(typeof(Author)).FindNavigation("Post").ForeignKey.PrincipalToDependent);
                Assert.Equal("PostNumber", model.GetEntityType(typeof(Author)).FindNavigation("Post").ForeignKey.Properties.First().Name);
            }
开发者ID:rbenhassine2,项目名称:EntityFramework,代码行数:12,代码来源:DataAnnotationsTestBase.cs

示例9: Can_get_entity_by_type

        public void Can_get_entity_by_type()
        {
            var model = new Model();
            var entityType = new EntityType(typeof(Customer));
            model.AddEntityType(entityType);

            Assert.Same(entityType, model.GetEntityType(typeof(Customer)));
            Assert.Same(entityType, model.TryGetEntityType(typeof(Customer)));
            Assert.Null(model.TryGetEntityType(typeof(string)));

            Assert.Equal(
                Strings.FormatEntityTypeNotFound("String"),
                Assert.Throws<ModelItemNotFoundException>(() => model.GetEntityType(typeof(string))).Message);
        }
开发者ID:Nyaoso,项目名称:EntityFramework,代码行数:14,代码来源:ModelTest.cs

示例10: Model

            public virtual void ForeignKeyAttribute_does_not_set_foreign_key_properties_when_names_on_property_and_navigation_do_not_match()
            {
                var model = new Model();
                var modelBuilder = CreateModelBuilder(model);
                modelBuilder.Entity<Author>();

                Assert.Equal("AuthorId", model.GetEntityType(typeof(AuthorDetails)).FindNavigation("Author").ForeignKey.Properties.First().Name);
            }
开发者ID:rbenhassine2,项目名称:EntityFramework,代码行数:8,代码来源:DataAnnotationsTestBase.cs

示例11: Can_get_entity_builder_for_clr_type_non_generic

        public void Can_get_entity_builder_for_clr_type_non_generic()
        {
            var model = new Model();
            var modelBuilder = new BasicModelBuilder(model);

            var entityBuilder = modelBuilder.Entity(typeof(Customer));

            Assert.NotNull(entityBuilder);
            Assert.Equal(typeof(Customer).FullName, model.GetEntityType(typeof(Customer)).Name);
        }
开发者ID:thegido,项目名称:EntityFramework,代码行数:10,代码来源:BasicModelBuilderTest.cs

示例12: Can_get_entity_builder_for_clr_type

            public virtual void Can_get_entity_builder_for_clr_type()
            {
                var model = new Model();
                var modelBuilder = CreateModelBuilder(model);

                var entityBuilder = modelBuilder.Entity<Customer>();

                Assert.NotNull(entityBuilder);
                Assert.Equal(typeof(Customer).FullName, model.GetEntityType(typeof(Customer)).Name);
            }
开发者ID:491134648,项目名称:EntityFramework,代码行数:10,代码来源:NonRelationshipTestBase.cs

示例13: Can_set_entity_key_from_clr_property

        public void Can_set_entity_key_from_clr_property()
        {
            var model = new Model();
            var modelBuilder = new ModelBuilder(model);

            modelBuilder.Entity<Customer>().Key(e => e.Id);

            var entity = model.GetEntityType(typeof(Customer));

            Assert.Equal(1, entity.GetKey().Properties.Count());
            Assert.Equal("Id", entity.GetKey().Properties.First().Name);
        }
开发者ID:Nyaoso,项目名称:EntityFramework,代码行数:12,代码来源:ModelBuilderTest.cs

示例14: Can_set_entity_key_from_clr_property

            public virtual void Can_set_entity_key_from_clr_property()
            {
                var model = new Model();
                var modelBuilder = CreateModelBuilder(model);

                modelBuilder.Entity<Customer>().HasKey(b => b.Id);

                var entity = model.GetEntityType(typeof(Customer));

                Assert.Equal(1, entity.GetPrimaryKey().Properties.Count);
                Assert.Equal(Customer.IdProperty.Name, entity.GetPrimaryKey().Properties.First().Name);
            }
开发者ID:491134648,项目名称:EntityFramework,代码行数:12,代码来源:NonRelationshipTestBase.cs

示例15: Finds_existing_navigations_and_uses_associated_FK

            public virtual void Finds_existing_navigations_and_uses_associated_FK()
            {
                var model = new Model();
                var modelBuilder = CreateModelBuilder(model);
                modelBuilder.Entity<Customer>()
                    .HasMany(e => e.Orders).WithOne(e => e.Customer)
                    .ForeignKey(e => e.CustomerId);
                modelBuilder.Entity<Order>();
                modelBuilder.Ignore<OrderDetails>();
                modelBuilder.Ignore<CustomerDetails>();

                var dependentType = model.GetEntityType(typeof(Order));
                var principalType = model.GetEntityType(typeof(Customer));
                var fk = dependentType.GetForeignKeys().Single();

                var navToPrincipal = dependentType.GetOrAddNavigation("Customer", fk, pointsToPrincipal: true);
                var navToDependent = principalType.GetOrAddNavigation("Orders", fk, pointsToPrincipal: false);

                var principalKey = principalType.GetKeys().Single();
                var dependentKey = dependentType.GetKeys().Single();

                modelBuilder.Entity<Order>().HasOne(e => e.Customer).WithMany(e => e.Orders);

                Assert.Same(fk, dependentType.GetForeignKeys().Single());
                Assert.Same(navToPrincipal, dependentType.Navigations.Single());
                Assert.Same(navToDependent, principalType.Navigations.Single());
                Assert.Same(fk, dependentType.Navigations.Single().ForeignKey);
                Assert.Same(fk, principalType.Navigations.Single().ForeignKey);
                AssertEqual(new[] { "AlternateKey", principalKey.Properties.Single().Name, Customer.NameProperty.Name }, principalType.Properties.Select(p => p.Name));
                AssertEqual(new[] { "AnotherCustomerId", fk.Properties.Single().Name, dependentKey.Properties.Single().Name }, dependentType.Properties.Select(p => p.Name));
                Assert.Empty(principalType.GetForeignKeys());
                Assert.Same(principalKey, principalType.GetKeys().Single());
                Assert.Same(dependentKey, dependentType.GetKeys().Single());
                Assert.Same(principalKey, principalType.GetPrimaryKey());
                Assert.Same(dependentKey, dependentType.GetPrimaryKey());
            }
开发者ID:491134648,项目名称:EntityFramework,代码行数:36,代码来源:ManyToOneTestBase.cs


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