public void Can_remove_complex_column_mapping() { var mappingFragment = new MappingFragment( new EntitySet(), new EntityTypeMapping( new EntitySetMapping( new EntitySet(), new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace)))), false); Assert.Empty(mappingFragment.ColumnMappings); var columnProperty = new EdmProperty("C", TypeUsage.Create(new PrimitiveType() { DataSpace = DataSpace.SSpace })); var property1 = EdmProperty.CreateComplex("P1", new ComplexType("CT")); var property2 = new EdmProperty("P2"); var columnMappingBuilder = new ColumnMappingBuilder(columnProperty, new[] { property1, property2 }); mappingFragment.AddColumnMapping(columnMappingBuilder); Assert.Same(columnMappingBuilder, mappingFragment.ColumnMappings.Single()); Assert.NotEmpty(mappingFragment.PropertyMappings); mappingFragment.RemoveColumnMapping(columnMappingBuilder); Assert.Empty(mappingFragment.ColumnMappings); Assert.Empty(mappingFragment.PropertyMappings); }
public void Can_get_flattened_properties_for_nested_mapping() { var mappingFragment = new MappingFragment( new EntitySet(), new EntityTypeMapping( new EntitySetMapping( new EntitySet(), new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace)))), false); Assert.Empty(mappingFragment.ColumnMappings); var columnProperty = new EdmProperty("C", TypeUsage.Create(new PrimitiveType() { DataSpace = DataSpace.SSpace })); var property1 = EdmProperty.CreateComplex("P1", new ComplexType("CT")); var property2 = new EdmProperty("P2"); var columnMappingBuilder1 = new ColumnMappingBuilder(columnProperty, new[] { property1, property2 }); mappingFragment.AddColumnMapping(columnMappingBuilder1); var columnMappingBuilder2 = new ColumnMappingBuilder(columnProperty, new[] { property2 }); mappingFragment.AddColumnMapping(columnMappingBuilder2); var columnMappingBuilders = mappingFragment.FlattenedProperties.ToList(); Assert.Equal(2, columnMappingBuilders.Count()); Assert.True(columnMappingBuilder1.PropertyPath.SequenceEqual(columnMappingBuilders.First().PropertyPath)); Assert.True(columnMappingBuilder2.PropertyPath.SequenceEqual(columnMappingBuilders.Last().PropertyPath)); }
public void GetPropertyMapping_should_return_mapping_with_path() { var entityTypeMapping = new EntityTypeMapping(null); var propertyFoo = EdmProperty.CreateComplex("Foo", new ComplexType("CT")); var propertyBar = EdmProperty.CreatePrimitive("Bar", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); var entityPropertyMapping = new ColumnMappingBuilder( new EdmProperty("C", TypeUsage.Create(new PrimitiveType() { DataSpace = DataSpace.SSpace })), new[] { propertyFoo, propertyBar, }); var entityTypeMappingFragment = new MappingFragment(new EntitySet(), entityTypeMapping, false); entityTypeMappingFragment.AddColumnMapping(entityPropertyMapping); entityTypeMapping.AddFragment(entityTypeMappingFragment); Assert.Same(entityPropertyMapping, entityTypeMapping.GetPropertyMapping(propertyFoo, propertyBar)); }
public void Can_generate_scalar_and_complex_properties_when_update() { var functionParameterMappingGenerator = new FunctionParameterMappingGenerator(ProviderRegistry.Sql2008_ProviderManifest); var property0 = new EdmProperty("P0"); var property1 = new EdmProperty("P1"); var property2 = new EdmProperty("P2"); property2.SetStoreGeneratedPattern(StoreGeneratedPattern.Computed); property2.ConcurrencyMode = ConcurrencyMode.Fixed; var complexType = new ComplexType("CT", "N", DataSpace.CSpace); complexType.AddMember(property1); var complexProperty = EdmProperty.CreateComplex("C", complexType); var parameterBindings = functionParameterMappingGenerator .Generate( ModificationOperator.Update, new[] { property0, complexProperty, property2 }, new[] { new ColumnMappingBuilder(new EdmProperty("C0"), new[] { property0 }), new ColumnMappingBuilder(new EdmProperty("C_P1"), new[] { complexProperty, property1 }), new ColumnMappingBuilder(new EdmProperty("C2"), new[] { property2 }) }, new List <EdmProperty>(), useOriginalValues: true) .ToList(); Assert.Equal(3, parameterBindings.Count()); var parameterBinding = parameterBindings.First(); Assert.Equal("C0", parameterBinding.Parameter.Name); Assert.Same(property0, parameterBinding.MemberPath.Members.Single()); Assert.Equal("String", parameterBinding.Parameter.TypeName); Assert.Equal(ParameterMode.In, parameterBinding.Parameter.Mode); Assert.False(parameterBinding.IsCurrent); parameterBinding = parameterBindings.ElementAt(1); Assert.Equal("C_P1", parameterBinding.Parameter.Name); Assert.Same(complexProperty, parameterBinding.MemberPath.Members.First()); Assert.Same(property1, parameterBinding.MemberPath.Members.Last()); Assert.Equal("String", parameterBinding.Parameter.TypeName); Assert.Equal(ParameterMode.In, parameterBinding.Parameter.Mode); Assert.False(parameterBinding.IsCurrent); parameterBinding = parameterBindings.Last(); Assert.Equal("C2_Original", parameterBinding.Parameter.Name); Assert.Same(property2, parameterBinding.MemberPath.Members.Single()); Assert.Equal("String", parameterBinding.Parameter.TypeName); Assert.Equal(ParameterMode.In, parameterBinding.Parameter.Mode); Assert.False(parameterBinding.IsCurrent); }
public void AsComplex_should_create_property_type() { var property = EdmProperty.CreateComplex("P", new ComplexType("C")); Assert.NotNull(property.TypeUsage); Assert.True(property.IsComplexType); Assert.Equal(false, property.Nullable); }
public void Can_create_mapping_and_get_property() { var complexType = ComplexType.Create("CT", "NS", DataSpace.CSpace, new EdmMember[0], null); var property = EdmProperty.CreateComplex("P", complexType); var mapping = new ComplexPropertyMapping(property); Assert.Same(property, mapping.Property); }
public static EdmProperty AddComplexProperty( this ComplexType complexType, string name, ComplexType targetComplexType) { EdmProperty complex = EdmProperty.CreateComplex(name, targetComplexType); complexType.AddMember((EdmMember)complex); return(complex); }
public static EdmProperty AddComplexProperty( this EntityType entityType, string name, ComplexType complexType) { EdmProperty complex = EdmProperty.CreateComplex(name, complexType); entityType.AddMember((EdmMember)complex); return(complex); }
public void GetComplexParameterBindings_should_return_all_complex_parameter_bindings_for_type() { var databaseMapping = new DbDatabaseMapping() .Initialize( new EdmModel(DataSpace.CSpace), new EdmModel(DataSpace.SSpace)); var entityType = new EntityType("E", "N", DataSpace.CSpace); var entitySet = databaseMapping.Model.AddEntitySet("ES", entityType); var entitySetMapping = databaseMapping.AddEntitySetMapping(entitySet); var complexType1 = new ComplexType(); complexType1.GetMetadataProperties().SetClrType(typeof(string)); var complexType2 = new ComplexType(); complexType2.GetMetadataProperties().SetClrType(typeof(object)); var storageModificationFunctionMapping = new ModificationFunctionMapping( entitySet, entityType, new EdmFunction("F", "N", DataSpace.SSpace), new[] { new ModificationFunctionParameterBinding( new FunctionParameter(), new ModificationFunctionMemberPath( new EdmMember[] { EdmProperty.CreateComplex("C1", complexType1), EdmProperty.CreateComplex("C2", complexType2), new EdmProperty("M") }, null), true ) }, null, null); entitySetMapping.AddModificationFunctionMapping( new EntityTypeModificationFunctionMapping( entityType, storageModificationFunctionMapping, storageModificationFunctionMapping, storageModificationFunctionMapping)); Assert.Equal(3, databaseMapping.GetComplexParameterBindings(typeof(string)).Count()); Assert.Equal(3, databaseMapping.GetComplexParameterBindings(typeof(object)).Count()); }
public void Apply_should_ignore_non_primitive_type() { var entityType = new EntityType("E", "N", DataSpace.CSpace); var property1 = EdmProperty.CreateComplex("Id", new ComplexType("C")); entityType.AddMember(property1); var property = property1; (new IdKeyDiscoveryConvention()).Apply(entityType, new DbModel(new EdmModel(DataSpace.CSpace), null)); Assert.False(entityType.KeyProperties.Contains(property)); }
private EdmProperty MapPrimitiveOrComplexOrEnumProperty( PropertyInfo propertyInfo, Func <StructuralTypeConfiguration> structuralTypeConfiguration, bool discoverComplexTypes = false) { DebugCheck.NotNull(propertyInfo); var property = propertyInfo.AsEdmPrimitiveProperty(); if (property == null) { var propertyType = propertyInfo.PropertyType; var complexType = _typeMapper.MapComplexType(propertyType, discoverComplexTypes); if (complexType != null) { property = EdmProperty.CreateComplex(propertyInfo.Name, complexType); } else { var isNullable = propertyType.TryUnwrapNullableType(out propertyType); if (propertyType.IsEnum()) { var enumType = _typeMapper.MapEnumType(propertyType); if (enumType != null) { property = EdmProperty.CreateEnum(propertyInfo.Name, enumType); property.Nullable = isNullable; } } } } if (property != null) { property.SetClrPropertyInfo(propertyInfo); new AttributeMapper(_typeMapper.MappingContext.AttributeProvider) .Map(propertyInfo, property.GetMetadataProperties()); if (!property.IsComplexType) { _typeMapper.MappingContext.ConventionsConfiguration.ApplyPropertyConfiguration( propertyInfo, () => structuralTypeConfiguration().Property(new PropertyPath(propertyInfo)), _typeMapper.MappingContext.ModelConfiguration); } } return(property); }
public void Cannot_create_mapping_for_non_primitive_or_non_enum_property() { var modelProperty = EdmProperty.CreateComplex("p", new ComplexType("CT")); var storeColumn = new EdmProperty("c", TypeUsage.Create(new PrimitiveType() { DataSpace = DataSpace.SSpace })); Assert.Equal( Strings.StorageScalarPropertyMapping_OnlyScalarPropertiesAllowed, Assert.Throws <ArgumentException>( () => new ScalarPropertyMapping(modelProperty, storeColumn)).Message); }
public static EdmProperty AddComplexProperty( this EntityType entityType, string name, ComplexType complexType) { DebugCheck.NotNull(entityType); DebugCheck.NotEmpty(name); DebugCheck.NotNull(complexType); var property = EdmProperty.CreateComplex(name, complexType); entityType.AddMember(property); return(property); }
public void SetReadOnly_is_called_on_child_mapping_items() { var complexType = ComplexType.Create("CT", "NS", DataSpace.CSpace, new EdmMember[0], null); var property = EdmProperty.CreateComplex("P", complexType); var mapping = new ComplexPropertyMapping(property); var typeMapping = new ComplexTypeMapping(isPartial: false); mapping.AddTypeMapping(typeMapping); Assert.False(typeMapping.IsReadOnly); mapping.SetReadOnly(); Assert.True(typeMapping.IsReadOnly); }
public void GetComplexPropertyMappings_should_return_all_complex_property_mappings_for_type() { var databaseMapping = new DbDatabaseMapping() .Initialize(new EdmModel(DataSpace.CSpace), new EdmModel(DataSpace.SSpace)); var entitySet = new EntitySet { Name = "ES" }; var entitySetMapping = databaseMapping.AddEntitySetMapping(entitySet); var entityTypeMapping = new EntityTypeMapping(null); entitySetMapping.AddTypeMapping(entityTypeMapping); var entityTypeMappingFragment = new MappingFragment(entitySet, entityTypeMapping, false); entityTypeMapping.AddFragment(entityTypeMappingFragment); var complexType = new ComplexType("C"); var propertyMapping1 = new ColumnMappingBuilder( new EdmProperty("C", TypeUsage.Create(new PrimitiveType() { DataSpace = DataSpace.SSpace })), new[] { EdmProperty.CreateComplex("P1", complexType), EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)) }); var type = typeof(object); complexType.GetMetadataProperties().SetClrType(type); entityTypeMappingFragment.AddColumnMapping(propertyMapping1); var propertyMapping2 = new ColumnMappingBuilder( new EdmProperty("C", TypeUsage.Create(new PrimitiveType() { DataSpace = DataSpace.SSpace })), new List <EdmProperty> { EdmProperty.CreateComplex("P3", complexType), EdmProperty.CreatePrimitive( "P2", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)), }); entityTypeMappingFragment.AddColumnMapping(propertyMapping2); Assert.Equal(2, databaseMapping.GetComplexPropertyMappings(typeof(object)).Count()); }
public static EdmProperty AddComplexProperty( this ComplexType complexType, string name, ComplexType targetComplexType) { DebugCheck.NotNull(complexType); DebugCheck.NotNull(complexType.Properties); DebugCheck.NotEmpty(name); DebugCheck.NotNull(targetComplexType); var property = EdmProperty.CreateComplex(name, targetComplexType); complexType.AddMember(property); return(property); }
public void Cannot_add_type_mapping_when_read_only() { var complexType = ComplexType.Create("CT", "NS", DataSpace.CSpace, new EdmMember[0], null); var property = EdmProperty.CreateComplex("P", complexType); var mapping = new ComplexPropertyMapping(property); mapping.SetReadOnly(); Assert.True(mapping.IsReadOnly); var typeMapping = new ComplexTypeMapping(isPartial: false); Assert.Equal( Strings.OperationOnReadOnlyItem, Assert.Throws <InvalidOperationException>( () => mapping.AddTypeMapping(typeMapping)).Message); }
public void Can_add_get_remove_type_mappings() { var complexType = ComplexType.Create("CT", "NS", DataSpace.CSpace, new EdmMember[0], null); var property = EdmProperty.CreateComplex("P", complexType); var mapping = new ComplexPropertyMapping(property); Assert.Equal(0, mapping.TypeMappings.Count); var typeMapping = new ComplexTypeMapping(isPartial: false); mapping.AddTypeMapping(typeMapping); Assert.Equal(1, mapping.TypeMappings.Count); Assert.Same(typeMapping, mapping.TypeMappings[0]); mapping.RemoveTypeMapping(typeMapping); Assert.Equal(0, mapping.TypeMappings.Count); }
public void Generate_should_throw_when_circular_complex_property() { var functionParameterMappingGenerator = new FunctionParameterMappingGenerator(ProviderRegistry.Sql2008_ProviderManifest); var complexType = new ComplexType("CT", "N", DataSpace.CSpace); complexType.AddMember(EdmProperty.CreateComplex("C1", complexType)); Assert.Equal( Strings.CircularComplexTypeHierarchy, Assert.Throws <InvalidOperationException>( () => functionParameterMappingGenerator .Generate( ModificationOperator.Insert, new[] { EdmProperty.CreateComplex("C0", complexType) }, new ColumnMappingBuilder[0], new List <EdmProperty>()) .ToList()).Message); }
private EdmProperty MapPrimitiveOrComplexOrEnumProperty( PropertyInfo propertyInfo, Func <StructuralTypeConfiguration> structuralTypeConfiguration, bool discoverComplexTypes = false) { EdmProperty property = propertyInfo.AsEdmPrimitiveProperty(); if (property == null) { Type underlyingType = propertyInfo.PropertyType; ComplexType complexType = this._typeMapper.MapComplexType(underlyingType, discoverComplexTypes); if (complexType != null) { property = EdmProperty.CreateComplex(propertyInfo.Name, complexType); } else { bool flag = underlyingType.TryUnwrapNullableType(out underlyingType); if (underlyingType.IsEnum()) { EnumType enumType = this._typeMapper.MapEnumType(underlyingType); if (enumType != null) { property = EdmProperty.CreateEnum(propertyInfo.Name, enumType); property.Nullable = flag; } } } } if (property != null) { property.SetClrPropertyInfo(propertyInfo); new AttributeMapper(this._typeMapper.MappingContext.AttributeProvider).Map(propertyInfo, (ICollection <MetadataProperty>)property.GetMetadataProperties()); if (!property.IsComplexType) { this._typeMapper.MappingContext.ConventionsConfiguration.ApplyPropertyConfiguration(propertyInfo, (Func <PropertyConfiguration>)(() => (PropertyConfiguration)structuralTypeConfiguration().Property(new PropertyPath(propertyInfo), new OverridableConfigurationParts?())), this._typeMapper.MappingContext.ModelConfiguration); } } return(property); }
public void Can_update_complex_column_mapping() { var mappingFragment = new MappingFragment( new EntitySet(), new EntityTypeMapping( new EntitySetMapping( new EntitySet(), new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace)))), false); var property1 = EdmProperty.CreateComplex("P1", new ComplexType("CT")); var property2 = new EdmProperty("P2"); var columnMappingBuilder1 = new ColumnMappingBuilder(new EdmProperty("C", TypeUsage.Create(new PrimitiveType() { DataSpace = DataSpace.SSpace })), new[] { property1, property2 }); mappingFragment.AddColumnMapping(columnMappingBuilder1); var columnProperty = new EdmProperty("C", TypeUsage.Create(new PrimitiveType() { DataSpace = DataSpace.SSpace })); var columnMappingBuilder2 = new ColumnMappingBuilder(columnProperty, new[] { property1, property2 }); mappingFragment.AddColumnMapping(columnMappingBuilder2); var complexPropertyMapping = (ComplexPropertyMapping)mappingFragment.PropertyMappings.Single(); var typeMapping = complexPropertyMapping.TypeMappings.Single(); var scalarPropertyMapping = (ScalarPropertyMapping)typeMapping.PropertyMappings.Single(); Assert.Same(columnProperty, scalarPropertyMapping.Column); Assert.Same(property2, scalarPropertyMapping.Property); }