public void Constructor_throws_when_principal_and_depedent_property_types_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(string)); var property2 = principalEntityType.AddProperty("Id1", typeof(int)); var property3 = principalEntityType.AddProperty("Id2", typeof(int)); principalEntityType.GetOrSetPrimaryKey( new[] { property2, property3 }); Assert.Equal( CoreStrings.ForeignKeyTypeMismatch("{'P1', 'P2'}", "D", "{'Id1', 'Id2'}", "P"), Assert.Throws <InvalidOperationException>( () => dependentEntityType.AddForeignKey( new[] { dependentProperty1, dependentProperty2 }, principalEntityType.FindPrimaryKey(), principalEntityType)) .Message); }
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()); }
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); }
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()); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }