public void Generate_should_map_scalar_properties_to_columns() { var databaseMapping = CreateEmptyModel(); var entityType = new EntityType { Name = "E" }; var property = EdmProperty.Primitive("P1", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); entityType.AddMember(property); var property1 = EdmProperty.Primitive("P2", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); entityType.AddMember(property1); var entitySet = databaseMapping.Model.AddEntitySet("ESet", entityType); var type = typeof(object); entityType.Annotations.SetClrType(type); new EntityTypeMappingGenerator(ProviderRegistry.Sql2008_ProviderManifest).Generate(entityType, databaseMapping); var entityTypeMappingFragment = databaseMapping.GetEntitySetMapping(entitySet).EntityTypeMappings.Single().MappingFragments.Single(); Assert.Equal(2, entityTypeMappingFragment.ColumnMappings.Count()); Assert.Equal(2, entityTypeMappingFragment.Table.Properties.Count()); }
public void Can_get_list_of_declared_members() { var entityType = new EntityType(); Assert.Empty(entityType.DeclaredMembers); var property1 = new NavigationProperty("N", TypeUsage.Create(new EntityType())); var property2 = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); entityType.AddMember(property1); entityType.AddMember(property2); Assert.Equal(2, entityType.DeclaredMembers.Count); entityType.RemoveMember(property1); entityType.RemoveMember(property2); var baseType = new EntityType(); baseType.AddMember(property1); baseType.AddMember(property2); entityType.BaseType = baseType; Assert.Empty(entityType.DeclaredMembers); Assert.Equal(2, entityType.Members.Count); }
public void Apply_should_match_id_ahead_of_type_and_id() { var entityType = new EntityType("Foo", "N", DataSpace.CSpace); var property = EdmProperty.Primitive("FooId", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); entityType.AddMember(property); var property1 = EdmProperty.Primitive("Id", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); entityType.AddMember(property1); var idProperty = property1; (new IdKeyDiscoveryConvention()).Apply(entityType, new DbModel(new EdmModel(DataSpace.CSpace), null)); Assert.Equal(1, entityType.KeyProperties.Count); Assert.True(entityType.KeyProperties.Contains(idProperty)); }
public void Generate_should_flatten_complex_properties_to_columns() { var databaseMapping = CreateEmptyModel(); var entityType = new EntityType("E", "N", DataSpace.CSpace); var complexType = new ComplexType("C"); var property = EdmProperty.CreatePrimitive("P1", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); complexType.AddMember(property); entityType.AddComplexProperty("C1", complexType); var property1 = EdmProperty.CreatePrimitive("P2", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); entityType.AddMember(property1); var entitySet = databaseMapping.Model.AddEntitySet("ESet", entityType); var type = typeof(object); entityType.GetMetadataProperties().SetClrType(type); new TableMappingGenerator(ProviderRegistry.Sql2008_ProviderManifest).Generate(entityType, databaseMapping); var entityTypeMappingFragment = databaseMapping.GetEntitySetMapping(entitySet).EntityTypeMappings.Single().MappingFragments.Single(); Assert.Equal(2, entityTypeMappingFragment.ColumnMappings.Count()); Assert.Equal(2, entityTypeMappingFragment.Table.Properties.Count()); }
public void BuildConstraintExceptionMessage_returns_message_for_composite_constraint() { var primitiveType = PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32); var principalType = new EntityType("Principal", "N", DataSpace.CSpace); var principalProperties = new EdmProperty[3]; var dependentType = new EntityType("Dependent", "N", DataSpace.CSpace); var dependentProperties = new EdmProperty[principalProperties.Length]; for (var i = 0; i < principalProperties.Length; i++) { principalProperties[i] = EdmProperty.CreatePrimitive("P" + i, primitiveType); principalType.AddMember(principalProperties[i]); dependentProperties[i] = EdmProperty.CreatePrimitive("D" + i, primitiveType); dependentType.AddMember(dependentProperties[i]); } var referentialConstraint = new ReferentialConstraint( new AssociationEndMember("P", principalType), new AssociationEndMember("D", dependentType), principalProperties, dependentProperties); Assert.Equal( Strings.RelationshipManager_InconsistentReferentialConstraintProperties( "Principal.P0, Principal.P1, Principal.P2", "Dependent.D0, Dependent.D1, Dependent.D2"), referentialConstraint.BuildConstraintExceptionMessage()); }
internal static void CheckAndAddMembers(IEnumerable <EdmMember> members, EntityType entityType) { foreach (EdmMember member in members) { if (member == null) { throw new ArgumentException(Strings.ADP_CollectionParameterElementIsNull((object)nameof(members))); } entityType.AddMember(member); } }
public void Apply_should_set_correct_defaults_for_unconfigured_strings() { var entityType = new EntityType(); var property = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); entityType.AddMember(property); ((IEdmConvention<EntityType>)new SqlCePropertyMaxLengthConvention()) .Apply(entityType, CreateEdmModel()); Assert.Equal(4000, property.MaxLength); }
public void Apply_should_match_type_prefixed_id() { var entityType = new EntityType("Foo", "N", DataSpace.CSpace); var property1 = EdmProperty.Primitive("FooId", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); entityType.AddMember(property1); var property = property1; ((IEdmConvention<EntityType>)new IdKeyDiscoveryConvention()).Apply(entityType, new EdmModel(DataSpace.CSpace)); Assert.True(entityType.KeyProperties.Contains(property)); }
public void Properties_list_should_be_live_on_reread() { var entityType = new EntityType(); Assert.Empty(entityType.Properties); var property = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); entityType.AddMember(property); Assert.Equal(1, entityType.Properties.Count); }
public void Apply_should_set_correct_defaults_for_unconfigured_binary() { var entityType = new EntityType("E", "N", DataSpace.CSpace); var property = EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); entityType.AddMember(property); (new SqlCePropertyMaxLengthConvention()) .Apply(entityType, CreateDbModel()); Assert.Null(property.IsUnicode); Assert.Equal(4000, property.MaxLength); }
public void Identity_uniquified_when_duplicate_name_set() { var primitiveType = PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32); var property1 = EdmProperty.Primitive("Foo", primitiveType); var property2 = EdmProperty.Primitive("Bar", primitiveType); var property3 = EdmProperty.Primitive("Boo", primitiveType); var entityType = new EntityType("T","S",DataSpace.CSpace); entityType.AddMember(property1); entityType.AddMember(property2); entityType.AddMember(property3); property2.Name = "Foo"; property3.Name = "Foo"; Assert.Equal("Foo1", property2.Identity); Assert.Equal("Foo2", property3.Identity); Assert.Equal(3, entityType.Properties.Count); }
public void Identity_uniquified_when_duplicate_name_set() { var primitiveType = PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32); var property1 = EdmProperty.Primitive("Foo", primitiveType); var property2 = EdmProperty.Primitive("Bar", primitiveType); var property3 = EdmProperty.Primitive("Boo", primitiveType); var entityType = new EntityType("T", "S", DataSpace.CSpace); entityType.AddMember(property1); entityType.AddMember(property2); entityType.AddMember(property3); property2.Name = "Foo"; property3.Name = "Foo"; Assert.Equal("Foo1", property2.Identity); Assert.Equal("Foo2", property3.Identity); Assert.Equal(3, entityType.Properties.Count); }
public void Apply_should_ignore_case() { var entityType = new EntityType("Foo", "N", DataSpace.CSpace); var property1 = EdmProperty.Primitive("foOid", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); entityType.AddMember(property1); var property = property1; (new IdKeyDiscoveryConvention()).Apply(entityType, new DbModel(new EdmModel(DataSpace.CSpace), null)); Assert.True(entityType.KeyProperties.Contains(property)); }
public void Apply_should_set_correct_defaults_for_string_keys() { var entityType = new EntityType("E", "N", DataSpace.CSpace); var property = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); entityType.AddMember(property); entityType.AddKeyMember(property); ((IEdmConvention<EntityType>)new PropertyMaxLengthConvention()) .Apply(entityType, new EdmModel(DataSpace.CSpace)); Assert.Equal(128, property.MaxLength); }
public void Apply_should_make_key_not_nullable() { var entityType = new EntityType("E", "N", DataSpace.CSpace); var property1 = EdmProperty.Primitive("Id", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); entityType.AddMember(property1); var property = property1; (new IdKeyDiscoveryConvention()).Apply(entityType, new DbModel(new EdmModel(DataSpace.CSpace), null)); Assert.False(property.Nullable); }
public void Apply_should_set_correct_defaults_for_unicode_fixed_length_strings() { var entityType = new EntityType("E", "N", DataSpace.CSpace); var property = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); property.IsFixedLength = true; entityType.AddMember(property); ((IEdmConvention<EntityType>)new SqlCePropertyMaxLengthConvention()) .Apply(entityType, CreateEdmModel()); Assert.Equal(4000, property.MaxLength); }
public void GetPrimitiveProperties_should_return_only_primitive_properties() { var entityType = new EntityType("E", "N", DataSpace.CSpace); var property1 = EdmProperty.Primitive("Foo", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); entityType.AddMember(property1); var property = property1; entityType.AddComplexProperty("Bar", new ComplexType("C")); Assert.Equal(1, entityType.GetDeclaredPrimitiveProperties().Count()); Assert.True(entityType.GetDeclaredPrimitiveProperties().Contains(property)); }
public void Apply_should_match_simple_id() { var entityType = new EntityType("E", "N", DataSpace.CSpace); var property1 = EdmProperty.CreatePrimitive("Id", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); entityType.AddMember(property1); var property = property1; (new IdKeyDiscoveryConvention()).Apply(entityType, new DbModel(new EdmModel(DataSpace.CSpace), null)); Assert.True(entityType.KeyProperties.Contains(property)); }
public static void Adding_a_NavigationProperty_to_an_EntityType_can_be_forced_when_read_only() { var typeUsage = TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); var associationType = new AssociationType("AssociationType", "Namespace", true, DataSpace.CSpace); var source = new EntityType("Source", "Namespace", DataSpace.CSpace); var target = new EntityType("Target", "Namespace", DataSpace.CSpace); var sourceEnd = new AssociationEndMember("SourceEnd", source); var targetEnd = new AssociationEndMember("TargetEnd", target); var navigationProperty = NavigationProperty.Create( "NavigationProperty", typeUsage, associationType, sourceEnd, targetEnd, null); source.SetReadOnly(); Assert.True(source.IsReadOnly); Assert.Equal( Resources.Strings.OperationOnReadOnlyItem, Assert.Throws <InvalidOperationException>( () => source.AddMember(navigationProperty)).Message); Assert.Equal(0, source.Members.Count); source.AddNavigationProperty(navigationProperty); Assert.True(source.IsReadOnly); Assert.Equal(1, source.Members.Count); Assert.Same(navigationProperty, source.Members[0]); Assert.Equal( Resources.Strings.OperationOnReadOnlyItem, Assert.Throws <InvalidOperationException>( () => source.AddMember(navigationProperty)).Message); }
public void Apply_should_set_correct_defaults_for_unconfigured_binary() { var entityType = new EntityType("E", "N", DataSpace.CSpace); var property = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Binary)); entityType.AddMember(property); ((IEdmConvention<EntityType>)new PropertyMaxLengthConvention()) .Apply(entityType, new EdmModel(DataSpace.CSpace)); Assert.Null(property.IsUnicode); Assert.Equal(false, property.IsFixedLength); Assert.Null(property.MaxLength); }
private static void LoadEntityTypePhase2( SchemaEntityType element, DbProviderManifest providerManifest, Converter.ConversionCache convertedItemCache, Dictionary <SchemaElement, GlobalItem> newGlobalItems) { EntityType newGlobalItem = (EntityType)newGlobalItems[(SchemaElement)element]; foreach (System.Data.Entity.Core.SchemaObjectModel.NavigationProperty navigationProperty in element.NavigationProperties) { newGlobalItem.AddMember((EdmMember)Converter.ConvertToNavigationProperty(newGlobalItem, navigationProperty, providerManifest, convertedItemCache, newGlobalItems)); } }
public void Apply_should_set_correct_defaults_for_unconfigured_strings() { var entityType = new EntityType("E", "N", DataSpace.CSpace); var property = EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); entityType.AddMember(property); (new PropertyMaxLengthConvention()) .Apply(entityType, new DbModel(new EdmModel(DataSpace.CSpace), null)); Assert.Equal(true, property.IsUnicode); Assert.Equal(false, property.IsFixedLength); Assert.Null(property.MaxLength); Assert.Equal(true, property.IsMaxLength); }
public void Apply_should_set_correct_defaults_for_non_unicode_fixed_length_strings() { var entityType = new EntityType("E", "N", DataSpace.CSpace); var property = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); property.IsFixedLength = true; property.IsUnicode = false; entityType.AddMember(property); (new PropertyMaxLengthConvention()) .Apply(entityType, new DbModel(new EdmModel(DataSpace.CSpace), null)); Assert.Equal(128, property.MaxLength); Assert.False(property.IsMaxLength); }
/// <summary> /// Checks for each property to be non-null and then adds it to the member collection /// </summary> /// <param name="members"> members for this type </param> /// <param name="entityType"> the membersCollection to which the members should be added </param> internal static void CheckAndAddMembers( IEnumerable <EdmMember> members, EntityType entityType) { foreach (var member in members) { // Check for each property to be non-null if (null == member) { throw new ArgumentException(Strings.ADP_CollectionParameterElementIsNull("members")); } // Add the property to the member collection entityType.AddMember(member); } }
internal bool MapIfNotNavigationProperty( PropertyInfo propertyInfo, EntityType entityType, Func<EntityTypeConfiguration> entityTypeConfiguration) { DebugCheck.NotNull(propertyInfo); DebugCheck.NotNull(entityType); var property = MapPrimitiveOrComplexOrEnumProperty(propertyInfo, entityTypeConfiguration); if (property != null) { entityType.AddMember(property); return true; } return false; }
public void Can_remove_member() { var entityType = new EntityType("E", "N", DataSpace.CSpace); Assert.Empty(entityType.Members); var property = EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); entityType.AddMember(property); Assert.Equal(1, entityType.Members.Count); entityType.RemoveMember(property); Assert.Empty(entityType.Members); }
public void Can_remove_member() { var entityType = new EntityType("E", "N", DataSpace.CSpace); Assert.Empty(entityType.Members); var property = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); entityType.AddMember(property); Assert.Equal(1, entityType.Members.Count); entityType.RemoveMember(property); Assert.Empty(entityType.Members); }
public void EdmNavigationProperty_BadNavigationPropertyBadFromRoleType() { var parentEntity = new EntityType("P", "N", DataSpace.CSpace); var targetEntity = new EntityType("T", "N", DataSpace.CSpace); var sourceEntity = new EntityType("S", "N", DataSpace.CSpace); var associationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace) { SourceEnd = new AssociationEndMember("S", sourceEntity), TargetEnd = new AssociationEndMember("T", targetEntity) }; var navigationProperty = new NavigationProperty("N", TypeUsage.Create(targetEntity)) { RelationshipType = associationType }; parentEntity.AddMember(navigationProperty); var model = new EdmModel(DataSpace.CSpace); model.AddItem(parentEntity); var validationContext = new EdmModelValidationContext(model, true); DataModelErrorEventArgs errorEventArgs = null; validationContext.OnError += (_, e) => errorEventArgs = e; EdmModelSemanticValidationRules .EdmNavigationProperty_BadNavigationPropertyBadFromRoleType .Evaluate(validationContext, navigationProperty); Assert.NotNull(errorEventArgs); Assert.Same(navigationProperty, errorEventArgs.Item); Assert.Equal( Strings.BadNavigationPropertyBadFromRoleType( navigationProperty.Name, sourceEntity.Name, navigationProperty.GetFromEnd().Name, navigationProperty.Association.Name, parentEntity.Name), errorEventArgs.ErrorMessage); }
public void Map( PropertyInfo propertyInfo, EntityType entityType, Func<EntityTypeConfiguration> entityTypeConfiguration) { DebugCheck.NotNull(propertyInfo); DebugCheck.NotNull(entityType); var property = MapPrimitiveOrComplexOrEnumProperty(propertyInfo, entityTypeConfiguration); if (property != null) { entityType.AddMember(property); } else { new NavigationPropertyMapper(_typeMapper).Map(propertyInfo, entityType, entityTypeConfiguration); } }
public void EdmNavigationProperty_BadNavigationPropertyBadFromRoleType() { var parentEntity = new EntityType("P", "N", DataSpace.CSpace); var targetEntity = new EntityType("T", "N", DataSpace.CSpace); var sourceEntity = new EntityType("S", "N", DataSpace.CSpace); var associationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace) { SourceEnd = new AssociationEndMember("S", sourceEntity), TargetEnd = new AssociationEndMember("T", targetEntity) }; var navigationProperty = new NavigationProperty("N", TypeUsage.Create(targetEntity)) { RelationshipType = associationType }; parentEntity.AddMember(navigationProperty); var model = new EdmModel(DataSpace.CSpace); model.AddItem(parentEntity); var validationContext = new EdmModelValidationContext(model, true); DataModelErrorEventArgs errorEventArgs = null; validationContext.OnError += (_, e) => errorEventArgs = e; EdmModelSemanticValidationRules .EdmNavigationProperty_BadNavigationPropertyBadFromRoleType .Evaluate(validationContext, navigationProperty); Assert.NotNull(errorEventArgs); Assert.Same(navigationProperty, errorEventArgs.Item); Assert.Equal( Strings.BadNavigationPropertyBadFromRoleType( navigationProperty.Name, sourceEntity.Name, navigationProperty.GetFromEnd().Name, navigationProperty.Association.Name, parentEntity.Name), errorEventArgs.ErrorMessage); }
public void IsKeyMember_should_return_true_when_part_of_key() { var primitiveType = PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32); var property = EdmProperty.Primitive("P", primitiveType); Assert.False(property.IsKeyMember); var entityType = new EntityType("E", "N", DataSpace.CSpace); entityType.AddMember(property); Assert.False(property.IsKeyMember); entityType.AddKeyMember(property); Assert.True(property.IsKeyMember); }
public void Configure_should_add_properties_to_dependent_properties() { var mockPropertyInfo = new MockPropertyInfo(typeof(int), "P"); var constraintConfiguration = new ForeignKeyConstraintConfiguration(new[] { mockPropertyInfo.Object }); var entityType = new EntityType("E", "N", DataSpace.CSpace); var property1 = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); entityType.AddMember(property1); var property = property1; property.SetClrPropertyInfo(mockPropertyInfo); var associationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace); associationType.SourceEnd = new AssociationEndMember("S", entityType); associationType.TargetEnd = new AssociationEndMember("T", new EntityType("E", "N", DataSpace.CSpace)); constraintConfiguration.Configure( associationType, associationType.SourceEnd, new EntityTypeConfiguration(typeof(object))); Assert.Equal(1, associationType.Constraint.ToProperties.Count); }
public void Configure_should_configure_properties() { var entityType = new EntityType { Name = "E" }; var property1 = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); entityType.AddMember(property1); var property = property1; var entityTypeConfiguration = new EntityTypeConfiguration(typeof(object)); var mockPropertyConfiguration = new Mock<PrimitivePropertyConfiguration>(); var mockPropertyInfo = new MockPropertyInfo(); property.SetClrPropertyInfo(mockPropertyInfo); entityTypeConfiguration.Property(new PropertyPath(mockPropertyInfo), () => mockPropertyConfiguration.Object); entityTypeConfiguration.Configure(entityType, new EdmModel()); mockPropertyConfiguration.Verify(p => p.Configure(property)); }
public static void AddColumn(EntityType table, EdmProperty column) { DebugCheck.NotNull(table); DebugCheck.NotNull(column); if (!table.Properties.Contains(column)) { var configuration = column.GetConfiguration() as PrimitivePropertyConfiguration; if ((configuration == null) || string.IsNullOrWhiteSpace(configuration.ColumnName)) { var preferredName = column.GetPreferredName() ?? column.Name; column.SetUnpreferredUniqueName(column.Name); column.Name = table.Properties.UniquifyName(preferredName); } table.AddMember(column); } }
public void Item_setter_sets_member_of_base_type() { var baseEntityType = new EntityType("B", "N", DataSpace.CSpace); var entityType = new EntityType("E", "N", DataSpace.CSpace); entityType.BaseType = baseEntityType; var primitiveType = PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32); var property1 = EdmProperty.CreatePrimitive("P1", primitiveType); var property2 = EdmProperty.CreatePrimitive("P2", primitiveType); var property3 = EdmProperty.CreatePrimitive("P3", primitiveType); entityType.AddMember(property2); baseEntityType.AddMember(property1); Assert.Equal(1, baseEntityType.Members.Count); Assert.Equal(2, entityType.Members.Count); Assert.Equal(0, baseEntityType.Members.IndexOf(property1)); Assert.Equal(0, entityType.Members.IndexOf(property1)); Assert.Equal(1, entityType.Members.IndexOf(property2)); entityType.Members.Source[0] = property3; Assert.False(baseEntityType.Members.Contains(property1)); Assert.False(entityType.Members.Contains(property1)); Assert.Equal(1, baseEntityType.Members.Count); Assert.Equal(2, entityType.Members.Count); Assert.Equal(0, baseEntityType.Members.IndexOf(property3)); Assert.Equal(0, entityType.Members.IndexOf(property3)); Assert.Equal(1, entityType.Members.IndexOf(property2)); baseEntityType.Members.Source[0] = property1; Assert.False(baseEntityType.Members.Contains(property3)); Assert.False(entityType.Members.Contains(property3)); Assert.Equal(1, baseEntityType.Members.Count); Assert.Equal(2, entityType.Members.Count); Assert.Equal(0, baseEntityType.Members.IndexOf(property1)); Assert.Equal(0, entityType.Members.IndexOf(property1)); Assert.Equal(1, entityType.Members.IndexOf(property2)); }
public void Apply_should_move_declared_keys_head_of_declared_properties_list() { var entityType = new EntityType("E", "N", DataSpace.CSpace); var type = typeof(SimpleEntity); entityType.GetMetadataProperties().SetClrType(type); var property1 = EdmProperty.CreatePrimitive("PrivateProperty", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); entityType.AddMember(property1); var property2 = EdmProperty.CreatePrimitive("InheritedPropertyB", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); entityType.AddMember(property2); var property3 = EdmProperty.CreatePrimitive("InheritedPropertyA", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); entityType.AddMember(property3); var property4 = EdmProperty.CreatePrimitive("PropertyB", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); entityType.AddMember(property4); var property5 = EdmProperty.CreatePrimitive("PropertyA", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); entityType.AddMember(property5); var property6 = EdmProperty.CreatePrimitive("Key", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); entityType.AddMember(property6); entityType.AddKeyMember(property6); new DeclaredPropertyOrderingConvention().Apply(entityType, new DbModel(new EdmModel(DataSpace.CSpace), null)); Assert.True( entityType.DeclaredProperties.Select(e => e.Name) .SequenceEqual( new[] { "Key", "PrivateProperty", "PropertyA", "PropertyB", "InheritedPropertyA", "InheritedPropertyB" })); }
public void BuildConstraintExceptionMessage_returns_message_for_single_property_constraint() { var principalType = new EntityType("Principal", "N", DataSpace.CSpace); var principalProperty = EdmProperty.CreatePrimitive("P1", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)); principalType.AddMember(principalProperty); var dependentType = new EntityType("Dependent", "N", DataSpace.CSpace); var dependentProperty = EdmProperty.CreatePrimitive("D1", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)); dependentType.AddMember(dependentProperty); var referentialConstraint = new ReferentialConstraint( new AssociationEndMember("P", principalType), new AssociationEndMember("D", dependentType), new[] { principalProperty }, new[] { dependentProperty }); Assert.Equal( Strings.RelationshipManager_InconsistentReferentialConstraintProperties("Principal.P1", "Dependent.D1"), referentialConstraint.BuildConstraintExceptionMessage()); }
public void Item_setter_sets_member_of_base_type() { var baseEntityType = new EntityType("B", "N", DataSpace.CSpace); var entityType = new EntityType("E", "N", DataSpace.CSpace); entityType.BaseType = baseEntityType; var primitiveType = PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32); var property1 = EdmProperty.CreatePrimitive("P1", primitiveType); var property2 = EdmProperty.CreatePrimitive("P2", primitiveType); var property3 = EdmProperty.CreatePrimitive("P3", primitiveType); entityType.AddMember(property2); baseEntityType.AddMember(property1); Assert.Equal(1, baseEntityType.Members.Count); Assert.Equal(2, entityType.Members.Count); Assert.Equal(0, baseEntityType.Members.IndexOf(property1)); Assert.Equal(0, entityType.Members.IndexOf(property1)); Assert.Equal(1, entityType.Members.IndexOf(property2)); entityType.Members.Source[0] = property3; Assert.False(baseEntityType.Members.Contains(property1)); Assert.False(entityType.Members.Contains(property1)); Assert.Equal(1, baseEntityType.Members.Count); Assert.Equal(2, entityType.Members.Count); Assert.Equal(0, baseEntityType.Members.IndexOf(property3)); Assert.Equal(0, entityType.Members.IndexOf(property3)); Assert.Equal(1, entityType.Members.IndexOf(property2)); baseEntityType.Members.Source[0] = property1; Assert.False(baseEntityType.Members.Contains(property3)); Assert.False(entityType.Members.Contains(property3)); Assert.Equal(1, baseEntityType.Members.Count); Assert.Equal(2, entityType.Members.Count); Assert.Equal(0, baseEntityType.Members.IndexOf(property1)); Assert.Equal(0, entityType.Members.IndexOf(property1)); Assert.Equal(1, entityType.Members.IndexOf(property2)); }
public void Configure_should_propagate_end_kind_to_keys_when_required() { var mockPropertyInfo = new MockPropertyInfo(typeof(int), "P"); var constraintConfiguration = new ForeignKeyConstraintConfiguration(new[] { mockPropertyInfo.Object }); var entityType = new EntityType("E", "N", DataSpace.CSpace); var property1 = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); entityType.AddMember(property1); var property = property1; property.Nullable = true; property.SetClrPropertyInfo(mockPropertyInfo); var associationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace); associationType.SourceEnd = new AssociationEndMember("S", entityType); associationType.TargetEnd = new AssociationEndMember("T", new EntityType("E", "N", DataSpace.CSpace)); associationType.TargetEnd.RelationshipMultiplicity = RelationshipMultiplicity.One; constraintConfiguration.Configure( associationType, associationType.SourceEnd, new EntityTypeConfiguration(typeof(object))); Assert.Equal(1, associationType.Constraint.ToProperties.Count); Assert.Equal(false, property.Nullable); }
public void NavigationProperties_is_thread_safe() { var entityType = new EntityType("E", "N", DataSpace.CSpace); var property = new NavigationProperty("N", TypeUsage.Create(new EntityType("E", "N", DataSpace.CSpace))); entityType.AddMember(property); const int cycles = 100; const int threadCount = 30; Action readNavigationProperties = () => { for (var i = 0; i < cycles; ++i) { var navProps = entityType.NavigationProperties; //touching Members.Source triggers a reset to NavigationProperties var sourceCount = entityType.Members.Source.Count; Assert.True(sourceCount == 1); var navigationPropertiesAfterReset = entityType.NavigationProperties; Assert.True(navProps != null, "First reference to NavigationProperties should not be null"); Assert.True(navigationPropertiesAfterReset != null, "Second reference to NavigationProperties should not be null"); Assert.False(ReferenceEquals(navProps, navigationPropertiesAfterReset), "The NavigationProperties instances should be different"); } }; var tasks = new List <Thread>(); for (var i = 0; i < threadCount; ++i) { tasks.Add(new Thread(new ThreadStart(readNavigationProperties))); } tasks.ForEach(t => t.Start()); tasks.ForEach(t => t.Join()); }
public void Can_get_list_of_declared_navigation_properties() { var entityType = new EntityType("E", "N", DataSpace.CSpace); Assert.Empty(entityType.DeclaredNavigationProperties); var property = new NavigationProperty("N", TypeUsage.Create(new EntityType("E", "N", DataSpace.CSpace))); entityType.AddMember(property); Assert.Equal(1, entityType.DeclaredNavigationProperties.Count); entityType.RemoveMember(property); var baseType = new EntityType("E", "N", DataSpace.CSpace); baseType.AddMember(property); entityType.BaseType = baseType; Assert.Empty(entityType.DeclaredNavigationProperties); Assert.Equal(1, entityType.Members.Count); }
public void Can_get_list_of_declared_properties() { var entityType = new EntityType("E", "N", DataSpace.CSpace); Assert.Empty(entityType.DeclaredProperties); var property = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); entityType.AddMember(property); Assert.Equal(1, entityType.DeclaredProperties.Count); entityType.RemoveMember(property); var baseType = new EntityType("E", "N", DataSpace.CSpace); baseType.AddMember(property); entityType.BaseType = baseType; Assert.Empty(entityType.DeclaredProperties); Assert.Equal(1, entityType.Members.Count); }
public static void Adding_a_NavigationProperty_to_an_EntityType_can_be_forced_when_read_only() { var typeUsage = TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); var associationType = new AssociationType("AssociationType", "Namespace", true, DataSpace.CSpace); var source = new EntityType("Source", "Namespace", DataSpace.CSpace); var target = new EntityType("Target", "Namespace", DataSpace.CSpace); var sourceEnd = new AssociationEndMember("SourceEnd", source); var targetEnd = new AssociationEndMember("TargetEnd", target); var navigationProperty = NavigationProperty.Create( "NavigationProperty", typeUsage, associationType, sourceEnd, targetEnd); source.SetReadOnly(); Assert.True(source.IsReadOnly); Assert.Equal( Resources.Strings.OperationOnReadOnlyItem, Assert.Throws<InvalidOperationException>( () => source.AddMember(navigationProperty)).Message); Assert.Equal(0, source.Members.Count); source.AddNavigationProperty(navigationProperty); Assert.True(source.IsReadOnly); Assert.Equal(1, source.Members.Count); Assert.Same(navigationProperty, source.Members[0]); Assert.Equal( Resources.Strings.OperationOnReadOnlyItem, Assert.Throws<InvalidOperationException>( () => source.AddMember(navigationProperty)).Message); }
public void Can_get_list_of_declared_properties() { var entityType = new EntityType(); Assert.Empty(entityType.DeclaredProperties); var property = EdmProperty.Primitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); entityType.AddMember(property); Assert.Equal(1, entityType.DeclaredProperties.Count); entityType.RemoveMember(property); var baseType = new EntityType(); baseType.AddMember(property); entityType.BaseType = baseType; Assert.Empty(entityType.DeclaredProperties); Assert.Equal(1, entityType.Members.Count); }
public void Can_get_list_of_declared_navigation_properties() { var entityType = new EntityType(); Assert.Empty(entityType.DeclaredNavigationProperties); var property = new NavigationProperty("N", TypeUsage.Create(new EntityType())); entityType.AddMember(property); Assert.Equal(1, entityType.DeclaredNavigationProperties.Count); entityType.RemoveMember(property); var baseType = new EntityType(); baseType.AddMember(property); entityType.BaseType = baseType; Assert.Empty(entityType.DeclaredNavigationProperties); Assert.Equal(1, entityType.Members.Count); }
public void BuildConstraintExceptionMessage_returns_message_for_single_property_constraint() { var principalType = new EntityType("Principal", "N", DataSpace.CSpace); var principalProperty = EdmProperty.CreatePrimitive("P1", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)); principalType.AddMember(principalProperty); var dependentType = new EntityType("Dependent", "N", DataSpace.CSpace); var dependentProperty = EdmProperty.CreatePrimitive("D1", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)); dependentType.AddMember(dependentProperty); var referentialConstraint = new ReferentialConstraint( new AssociationEndMember("P", principalType), new AssociationEndMember("D", dependentType), new[] { principalProperty }, new[] { dependentProperty }); Assert.Equal( Strings.RelationshipManager_InconsistentReferentialConstraintProperties("Principal.P1", "Dependent.D1"), referentialConstraint.BuildConstraintExceptionMessage()); }
public void WriteFunctionImportMappingElement_writes_result_mapping_for_non_composable_functions_mapped_explicitly_to_EntityType() { var typeUsage = TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)); var entityTypeProperty1 = new EdmProperty("ETProperty1", typeUsage); var entityTypeProperty2 = new EdmProperty("ETProperty2", typeUsage); var entityType = new EntityType("ET", "Ns", DataSpace.CSpace); entityType.AddMember(entityTypeProperty1); entityType.AddMember(entityTypeProperty2); var functionImport = new EdmFunction( "f_c", "Ns", DataSpace.CSpace, new EdmFunctionPayload { IsComposable = false, IsFunctionImport = true, ReturnParameters = new[] { new FunctionParameter( "ReturnValue", TypeUsage.CreateDefaultTypeUsage(entityType.GetCollectionType()), ParameterMode.ReturnValue) }, }); typeUsage = ProviderRegistry.Sql2008_ProviderManifest.GetStoreType(typeUsage); var rowTypeProperty1 = new EdmProperty("RTProperty1", typeUsage); var rowTypeProperty2 = new EdmProperty("RTProperty2", typeUsage); var rowType = new RowType(new[] { rowTypeProperty1, rowTypeProperty2 }); var storeFunction = new EdmFunction( "f_s", "Ns.Store", DataSpace.SSpace, new EdmFunctionPayload { IsComposable = false, ReturnParameters = new[] { new FunctionParameter( "Return", TypeUsage.CreateDefaultTypeUsage(rowType), ParameterMode.ReturnValue) }, }); var functionImportResultMapping = new FunctionImportResultMapping(); functionImportResultMapping.AddTypeMapping( new FunctionImportEntityTypeMapping( new EntityType[0], new [] { entityType }, new Collections.ObjectModel.Collection <FunctionImportReturnTypePropertyMapping>() { new FunctionImportReturnTypeScalarPropertyMapping("ETProperty1", "RTProperty1"), new FunctionImportReturnTypeScalarPropertyMapping("ETProperty2", "RTProperty2") }, new FunctionImportEntityTypeMappingCondition[] { new FunctionImportEntityTypeMappingConditionIsNull("RTProperty1", false), new FunctionImportEntityTypeMappingConditionValue("RTProperty2", 4), new FunctionImportEntityTypeMappingConditionValue("FakeCondition", true) } )); var mappingItemCollection = new StorageMappingItemCollection( new EdmItemCollection(EdmModel.CreateConceptualModel()), new StoreItemCollection( EdmModel.CreateStoreModel(ProviderRegistry.Sql2008_ProviderInfo, ProviderRegistry.Sql2008_ProviderManifest)), new string[0]); var containerMapping = new EntityContainerMapping( new EntityContainer("C", DataSpace.CSpace), new EntityContainer("S", DataSpace.SSpace), mappingItemCollection, false); var functionImportMapping = new FunctionImportMappingNonComposable( functionImport, storeFunction, new [] { functionImportResultMapping }, containerMapping); containerMapping.AddFunctionImportMapping(functionImportMapping); var fixture = new Fixture(); fixture.Writer.WriteFunctionImportMappingElement(functionImportMapping); Assert.Equal( @"<FunctionImportMapping FunctionName=""Ns.Store.f_s"" FunctionImportName=""f_c""> <ResultMapping> <EntityTypeMapping TypeName=""Ns.ET""> <ScalarProperty Name=""ETProperty1"" ColumnName=""RTProperty1"" /> <ScalarProperty Name=""ETProperty2"" ColumnName=""RTProperty2"" /> <Condition ColumnName=""RTProperty1"" IsNull=""false"" /> <Condition ColumnName=""RTProperty2"" Value=""4"" /> <Condition ColumnName=""FakeCondition"" Value=""1"" /> </EntityTypeMapping> </ResultMapping> </FunctionImportMapping>", fixture.ToString()); }
public void WriteEntityContainerMappingElement_should_write_function_import_elements_mapped_to_EntityType() { var typeUsage = TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)); var entityTypeProperty1 = new EdmProperty("ETProperty1", typeUsage); var entityTypeProperty2 = new EdmProperty("ETProperty2", typeUsage); var entityType = new EntityType("ET", "Ns", DataSpace.CSpace); entityType.AddMember(entityTypeProperty1); entityType.AddMember(entityTypeProperty2); var functionImport = new EdmFunction( "f_c", "Ns", DataSpace.CSpace, new EdmFunctionPayload { IsComposable = true, IsFunctionImport = true, ReturnParameters = new[] { new FunctionParameter( "ReturnValue", TypeUsage.CreateDefaultTypeUsage(entityType.GetCollectionType()), ParameterMode.ReturnValue) }, Parameters = new[] { new FunctionParameter("param", typeUsage, ParameterMode.Out) } }); typeUsage = ProviderRegistry.Sql2008_ProviderManifest.GetStoreType(typeUsage); var rowTypeProperty1 = new EdmProperty("RTProperty1", typeUsage); var rowTypeProperty2 = new EdmProperty("RTProperty2", typeUsage); var rowType = new RowType(new[] { rowTypeProperty1, rowTypeProperty2 }); var storeFunction = new EdmFunction( "f_s", "Ns.Store", DataSpace.SSpace, new EdmFunctionPayload { ReturnParameters = new[] { new FunctionParameter( "Return", TypeUsage.CreateDefaultTypeUsage(rowType), ParameterMode.ReturnValue) }, Parameters = new[] { new FunctionParameter("param", typeUsage, ParameterMode.Out) } }); var structuralTypeMapping = new Tuple <StructuralType, List <ConditionPropertyMapping>, List <PropertyMapping> >( entityType, new List <ConditionPropertyMapping>(), new List <PropertyMapping>()); structuralTypeMapping.Item2.Add(new ConditionPropertyMapping(rowTypeProperty1, null, false)); structuralTypeMapping.Item3.Add(new ScalarPropertyMapping(entityTypeProperty1, rowTypeProperty1)); structuralTypeMapping.Item3.Add(new ScalarPropertyMapping(entityTypeProperty2, rowTypeProperty2)); var functionImportMapping = new FunctionImportMappingComposable( functionImport, storeFunction, new List <Tuple <StructuralType, List <ConditionPropertyMapping>, List <PropertyMapping> > > { structuralTypeMapping }); var containerMapping = new EntityContainerMapping(new EntityContainer("C", DataSpace.SSpace)); containerMapping.AddFunctionImportMapping(functionImportMapping); var fixture = new Fixture(); fixture.Writer.WriteEntityContainerMappingElement(containerMapping); Assert.Equal( @"<EntityContainerMapping StorageEntityContainer="""" CdmEntityContainer=""C""> <FunctionImportMapping FunctionName=""Ns.Store.f_s"" FunctionImportName=""f_c""> <ResultMapping> <EntityTypeMapping TypeName=""Ns.ET""> <Condition IsNull=""false"" ColumnName=""RTProperty1"" /> <ScalarProperty Name=""ETProperty1"" ColumnName=""RTProperty1"" /> <ScalarProperty Name=""ETProperty2"" ColumnName=""RTProperty2"" /> </EntityTypeMapping> </ResultMapping> </FunctionImportMapping> </EntityContainerMapping>", fixture.ToString()); }