コード例 #1
0
        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.ClientSetNull, foreignKey.DeleteBehavior);

            foreignKey.DeleteBehavior = DeleteBehavior.Cascade;

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

            foreignKey.DeleteBehavior = DeleteBehavior.Restrict;

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

            foreignKey.DeleteBehavior = DeleteBehavior.SetNull;

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

            foreignKey.DeleteBehavior = DeleteBehavior.ClientSetNull;

            Assert.Equal(DeleteBehavior.ClientSetNull, foreignKey.DeleteBehavior);
        }
コード例 #2
0
ファイル: KeyTest.cs プロジェクト: mpenchev86/EntityFramework
        public void Validates_properties_from_same_entity()
        {
            var entityType1 = new Model().AddEntityType(typeof(Customer));
            var entityType2 = new Model().AddEntityType(typeof(Order));
            var property1   = entityType1.GetOrAddProperty(Customer.IdProperty);
            var property2   = entityType2.GetOrAddProperty(Order.NameProperty);

            Assert.Equal(CoreStrings.KeyPropertiesWrongEntity($"{{'{property1.Name}', '{property2.Name}'}}", entityType1.DisplayName()),
                         Assert.Throws <ArgumentException>(
                             () => entityType1.AddKey(new[] { property1, property2 })).Message);
        }
コード例 #3
0
ファイル: KeyTest.cs プロジェクト: ChuYuzhi/EntityFramework
        public void Validates_properties_from_same_entity()
        {
            var entityType1 = new Model().AddEntityType(typeof(Customer));
            var entityType2 = new Model().AddEntityType(typeof(Order));
            var property1 = entityType1.GetOrAddProperty(Customer.IdProperty);
            var property2 = entityType2.GetOrAddProperty(Order.NameProperty);

            Assert.Equal(CoreStrings.KeyPropertiesWrongEntity($"{{'{property1.Name}', '{property2.Name}'}}", entityType1.DisplayName()),
                Assert.Throws<InvalidOperationException>(
                    () => entityType1.AddKey(new[] { property1, property2 })).Message);
        }
コード例 #4
0
ファイル: KeyTest.cs プロジェクト: ChuYuzhi/EntityFramework
        public void Can_create_key_from_properties()
        {
            var entityType = new Model().AddEntityType(typeof(Customer));
            var property1 = entityType.GetOrAddProperty(Customer.IdProperty);
            var property2 = entityType.GetOrAddProperty(Customer.NameProperty);
            property2.IsNullable = false;

            var key = entityType.AddKey(new[] { property1, property2 }, ConfigurationSource.Convention);

            Assert.True(new[] { property1, property2 }.SequenceEqual(key.Properties));
            Assert.Equal(ConfigurationSource.Convention, key.GetConfigurationSource());
        }
コード例 #5
0
        public void Can_create_key_from_properties()
        {
            var entityType = new Model().AddEntityType(typeof(Customer));
            var property1  = entityType.GetOrAddProperty(Customer.IdProperty);
            var property2  = entityType.GetOrAddProperty(Customer.NameProperty);

            property2.IsNullable = false;

            var key = entityType.AddKey(new[] { property1, property2 }, ConfigurationSource.Convention);

            Assert.True(new[] { property1, property2 }.SequenceEqual(key.Properties));
            Assert.Equal(ConfigurationSource.Convention, key.GetConfigurationSource());
        }
コード例 #6
0
        public void Setting_ClrType_throws_when_referenced()
        {
            var entityType        = new Model().AddEntityType(typeof(object));
            var principalProperty = entityType.AddProperty("Kake");

            principalProperty.IsNullable = false;
            var key = entityType.AddKey(principalProperty);
            var dependentProperty = entityType.AddProperty("Alaska");

            entityType.AddForeignKey(dependentProperty, key, entityType);

            Assert.Equal(CoreStrings.PropertyClrTypeCannotBeChangedWhenReferenced("Kake", "{'Alaska'}", "object"),
                         Assert.Throws <InvalidOperationException>(() =>
                                                                   principalProperty.ClrType = typeof(int)).Message);
            Assert.Equal(typeof(string), ((IProperty)principalProperty).ClrType);
        }
コード例 #7
0
        public void Can_change_cascade_ownership()
        {
            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.False(foreignKey.IsOwnership);

            foreignKey.IsOwnership = true;

            Assert.True(foreignKey.IsOwnership);
        }
コード例 #8
0
        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);
        }
コード例 #9
0
        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);
        }
コード例 #10
0
        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);
        }