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


C# Model.GetOrSetPrimaryKey方法代码示例

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


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

示例1: Constructor_throws_when_principal_and_depedent_property_count_do_not_match

        public void Constructor_throws_when_principal_and_depedent_property_count_do_not_match()
        {
            var dependentEntityType = new Model().AddEntityType("D");
            var principalEntityType = new Model().AddEntityType("P");

            var dependentProperty1 = dependentEntityType.AddProperty("P1", typeof(int));
            var dependentProperty2 = dependentEntityType.AddProperty("P2", typeof(int));

            var idProperty = principalEntityType.AddProperty("Id", typeof(int));
            principalEntityType.GetOrSetPrimaryKey(idProperty);

            Assert.Equal(
                CoreStrings.ForeignKeyCountMismatch("{'P1', 'P2'}", "D", "{'Id'}", "P"),
                Assert.Throws<InvalidOperationException>(
                    () => dependentEntityType.AddForeignKey(new[] { dependentProperty1, dependentProperty2 }, principalEntityType.FindPrimaryKey(), principalEntityType)).Message);
        }
开发者ID:RickyLin,项目名称:EntityFramework,代码行数:16,代码来源:ForeignKeyTest.cs

示例2: Can_create_foreign_key

        public void Can_create_foreign_key()
        {
            var entityType = new Model().AddEntityType("E");
            var dependentProp = entityType.AddProperty("P", typeof(int));
            var principalProp = entityType.AddProperty("Id", typeof(int));
            entityType.GetOrSetPrimaryKey(principalProp);

            var foreignKey = entityType.AddForeignKey(new[] { dependentProp }, entityType.FindPrimaryKey(), entityType, ConfigurationSource.Convention);
            foreignKey.IsUnique = true;

            Assert.Same(entityType, foreignKey.PrincipalEntityType);
            Assert.Same(principalProp, foreignKey.PrincipalKey.Properties.Single());
            Assert.Same(dependentProp, foreignKey.Properties.Single());
            Assert.True(foreignKey.IsUnique);
            Assert.Same(entityType.FindPrimaryKey(), foreignKey.PrincipalKey);
            Assert.Equal(ConfigurationSource.Convention, foreignKey.GetConfigurationSource());

            foreignKey.UpdateConfigurationSource(ConfigurationSource.DataAnnotation);

            Assert.Equal(ConfigurationSource.DataAnnotation, foreignKey.GetConfigurationSource());
        }
开发者ID:RickyLin,项目名称:EntityFramework,代码行数:21,代码来源:ForeignKeyTest.cs

示例3: Can_change_cascade_delete_flag

        public void Can_change_cascade_delete_flag()
        {
            var entityType = new Model().AddEntityType("E");
            var keyProp = entityType.AddProperty("Id", typeof(int));
            var dependentProp = entityType.AddProperty("P", typeof(int));
            var principalProp = entityType.AddProperty("U", typeof(int));
            entityType.GetOrSetPrimaryKey(keyProp);
            var principalKey = entityType.AddKey(principalProp);

            var foreignKey = entityType.AddForeignKey(new[] { dependentProp }, principalKey, entityType);

            Assert.Equal(DeleteBehavior.Restrict, foreignKey.DeleteBehavior);

            foreignKey.DeleteBehavior = DeleteBehavior.Cascade;

            Assert.Equal(DeleteBehavior.Cascade, foreignKey.DeleteBehavior);

            foreignKey.DeleteBehavior = DeleteBehavior.Restrict;

            Assert.Equal(DeleteBehavior.Restrict, foreignKey.DeleteBehavior);
        }
开发者ID:RickyLin,项目名称:EntityFramework,代码行数:21,代码来源:ForeignKeyTest.cs

示例4: CreateSelfRefFK

        private ForeignKey CreateSelfRefFK(bool useAltKey = false)
        {
            var entityType = new Model().AddEntityType(typeof(SelfRef));
            var pk = entityType.GetOrSetPrimaryKey(entityType.AddProperty(SelfRef.IdProperty));
            var fkProp = entityType.AddProperty(SelfRef.SelfRefIdProperty);

            var property = entityType.AddProperty("AltId", typeof(int));
            var principalKey = useAltKey
                ? entityType.GetOrAddKey(property)
                : pk;

            var fk = entityType.AddForeignKey(new[] { fkProp }, principalKey, entityType);
            fk.IsUnique = true;
            fk.HasDependentToPrincipal(SelfRef.SelfRefPrincipalProperty);
            fk.HasPrincipalToDependent(SelfRef.SelfRefDependentProperty);
            return fk;
        }
开发者ID:RickyLin,项目名称:EntityFramework,代码行数:17,代码来源:ForeignKeyTest.cs

示例5: Setting_IsRequired_to_false_will_set_all_FK_properties_as_nullable

        public void Setting_IsRequired_to_false_will_set_all_FK_properties_as_nullable()
        {
            var entityType = new Model().AddEntityType("E");
            var property = entityType.AddProperty("Id1", typeof(int));
            var property1 = entityType.AddProperty("Id2", typeof(string));
            property1.IsNullable = false;
            entityType.GetOrSetPrimaryKey(new[]
            {
                property,
                property1
            });

            var dependentProp1 = entityType.AddProperty("P1", typeof(int?));
            var dependentProp2 = entityType.AddProperty("P2", typeof(string));

            var foreignKey = entityType.AddForeignKey(new[] { dependentProp1, dependentProp2 }, entityType.FindPrimaryKey(), entityType);
            foreignKey.IsRequired = false;

            Assert.False(foreignKey.IsRequired);
            Assert.True(dependentProp1.IsNullable);
            Assert.True(dependentProp2.IsNullable);
        }
开发者ID:RickyLin,项目名称:EntityFramework,代码行数:22,代码来源:ForeignKeyTest.cs

示例6: IsRequired_false_when_any_part_of_composite_FK_is_nullable

        public void IsRequired_false_when_any_part_of_composite_FK_is_nullable()
        {
            var entityType = new Model().AddEntityType("E");
            var property = entityType.AddProperty("Id1", typeof(int));
            var property1 = entityType.AddProperty("Id2", typeof(string));
            property1.IsNullable = false;
            entityType.GetOrSetPrimaryKey(new[]
            {
                property,
                property1
            });

            var dependentProp1 = entityType.AddProperty("P1", typeof(int));
            var dependentProp2 = entityType.AddProperty("P2", typeof(string));
            dependentProp2.IsNullable = true;

            var foreignKey = entityType.AddForeignKey(new[] { dependentProp1, dependentProp2 }, entityType.FindPrimaryKey(), entityType);

            Assert.False(foreignKey.IsRequired);

            dependentProp2.IsNullable = false;

            Assert.True(foreignKey.IsRequired);
        }
开发者ID:RickyLin,项目名称:EntityFramework,代码行数:24,代码来源:ForeignKeyTest.cs

示例7: IsRequired_and_IsUnique_null_when_dependent_property_nullable_by_default

        public void IsRequired_and_IsUnique_null_when_dependent_property_nullable_by_default()
        {
            var entityType = new Model().AddEntityType("E");
            var property = entityType.AddProperty("Id", typeof(int));
            entityType.GetOrSetPrimaryKey(property);
            var dependentProp = entityType.AddProperty("P", typeof(int?));

            var foreignKey = entityType.AddForeignKey(new[] { dependentProp }, entityType.FindPrimaryKey(), entityType);

            Assert.False(foreignKey.IsRequired);
            Assert.False(foreignKey.IsUnique);
        }
开发者ID:RickyLin,项目名称:EntityFramework,代码行数:12,代码来源:ForeignKeyTest.cs

示例8: IsRequired_true_when_dependent_property_not_nullable

        public void IsRequired_true_when_dependent_property_not_nullable()
        {
            var entityType = new Model().AddEntityType("E");
            var property = entityType.AddProperty("Id", typeof(int));
            entityType.GetOrSetPrimaryKey(property);
            var dependentProp = entityType.AddProperty("P", typeof(int));
            dependentProp.IsNullable = false;

            var foreignKey = entityType.AddForeignKey(new[] { dependentProp }, entityType.FindPrimaryKey(), entityType);

            Assert.True(foreignKey.IsRequired);
        }
开发者ID:RickyLin,项目名称:EntityFramework,代码行数:12,代码来源:ForeignKeyTest.cs

示例9: Can_create_foreign_key_with_non_pk_principal

        public void Can_create_foreign_key_with_non_pk_principal()
        {
            var entityType = new Model().AddEntityType("E");
            var keyProp = entityType.AddProperty("Id", typeof(int));
            var dependentProp = entityType.AddProperty("P", typeof(int));
            var principalProp = entityType.AddProperty("U", typeof(int));
            entityType.GetOrSetPrimaryKey(keyProp);
            var principalKey = entityType.AddKey(principalProp);

            var foreignKey = entityType.AddForeignKey(new[] { dependentProp }, principalKey, entityType);
            foreignKey.IsUnique = false;

            Assert.Same(entityType, foreignKey.PrincipalEntityType);
            Assert.Same(principalProp, foreignKey.PrincipalKey.Properties.Single());
            Assert.Same(dependentProp, foreignKey.Properties.Single());
            Assert.False(foreignKey.IsUnique);
            Assert.Same(principalKey, foreignKey.PrincipalKey);
        }
开发者ID:RickyLin,项目名称:EntityFramework,代码行数:18,代码来源:ForeignKeyTest.cs


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