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 Can_update_scalar_column_mapping() { var mappingFragment = new MappingFragment( new EntitySet(), new EntityTypeMapping( new EntitySetMapping( new EntitySet(), new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace)))), false); var property = new EdmProperty("P"); mappingFragment.AddColumnMapping(new ColumnMappingBuilder(new EdmProperty("C", TypeUsage.Create(new PrimitiveType() { DataSpace = DataSpace.SSpace })), new[] { property })); var columnProperty = new EdmProperty("C'", TypeUsage.Create(new PrimitiveType() { DataSpace = DataSpace.SSpace })); var columnMappingBuilder = new ColumnMappingBuilder(columnProperty, new[] { property }); mappingFragment.AddColumnMapping(columnMappingBuilder); var scalarPropertyMapping = (ScalarPropertyMapping)mappingFragment.PropertyMappings.Single(); Assert.Same(columnProperty, scalarPropertyMapping.Column); Assert.Same(property, scalarPropertyMapping.Property); }
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 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 void Cannot_add_invalid_column_mapping_builder() { var mappingFragment = new MappingFragment( new EntitySet(), new EntityTypeMapping( new EntitySetMapping( new EntitySet(), new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace)))), false); Assert.Equal( "columnMappingBuilder", Assert.Throws <ArgumentNullException>( () => mappingFragment.AddColumnMapping(null)).ParamName); Assert.Equal( Strings.InvalidColumnBuilderArgument("columnBuilderMapping"), Assert.Throws <ArgumentException>( () => mappingFragment.AddColumnMapping( new ColumnMappingBuilder(new EdmProperty("S"), new List <EdmProperty>()))).Message); }
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); }
public void Cannot_add_duplicate_column_mapping_builder() { var mappingFragment = new MappingFragment( new EntitySet(), new EntityTypeMapping( new EntitySetMapping( new EntitySet(), new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace)))), false); var columnMappingBuilder = new ColumnMappingBuilder(new EdmProperty("S", TypeUsage.Create(new PrimitiveType() { DataSpace = DataSpace.SSpace })), new[] { new EdmProperty("S") }); mappingFragment.AddColumnMapping(columnMappingBuilder); Assert.Equal( Strings.InvalidColumnBuilderArgument("columnBuilderMapping"), Assert.Throws <ArgumentException>( () => mappingFragment.AddColumnMapping(columnMappingBuilder)).Message); }
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)); }
// <summary> // Makes sure only the required property mappings are present // </summary> private static void ConfigureTypeMappings( TableMapping tableMapping, Dictionary<EntityType, EntityTypeMapping> rootMappings, EntityType entityType, MappingFragment propertiesTypeMappingFragment, MappingFragment conditionTypeMappingFragment) { var existingPropertyMappings = new List<ColumnMappingBuilder>( propertiesTypeMappingFragment.ColumnMappings.Where(pm => !pm.ColumnProperty.IsPrimaryKeyColumn)); var existingConditions = new List<ConditionPropertyMapping>(propertiesTypeMappingFragment.ColumnConditions); foreach (var columnMapping in from cm in tableMapping.ColumnMappings from pm in cm.PropertyMappings where pm.EntityType == entityType select new { cm.Column, Property = pm }) { if (columnMapping.Property.PropertyPath != null && !IsRootTypeMapping( rootMappings, columnMapping.Property.EntityType, columnMapping.Property.PropertyPath)) { var existingPropertyMapping = propertiesTypeMappingFragment.ColumnMappings.SingleOrDefault( x => x.PropertyPath == columnMapping.Property.PropertyPath); if (existingPropertyMapping != null) { existingPropertyMappings.Remove(existingPropertyMapping); } else { existingPropertyMapping = new ColumnMappingBuilder(columnMapping.Column, columnMapping.Property.PropertyPath); propertiesTypeMappingFragment.AddColumnMapping(existingPropertyMapping); } } if (columnMapping.Property.Conditions != null) { foreach (var condition in columnMapping.Property.Conditions) { if (conditionTypeMappingFragment.ColumnConditions.Contains(condition)) { existingConditions.Remove(condition); } else if (!entityType.Abstract) { conditionTypeMappingFragment.AddConditionProperty(condition); } } } } // Any leftover mappings are removed foreach (var leftoverPropertyMapping in existingPropertyMappings) { propertiesTypeMappingFragment.RemoveColumnMapping(leftoverPropertyMapping); } foreach (var leftoverCondition in existingConditions) { conditionTypeMappingFragment.RemoveConditionProperty(leftoverCondition); } if (entityType.Abstract) { propertiesTypeMappingFragment.ClearConditions(); } }
public void Cannot_add_invalid_column_mapping_builder() { var mappingFragment = new MappingFragment( new EntitySet(), new EntityTypeMapping( new EntitySetMapping( new EntitySet(), new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace)))), false); Assert.Equal( "columnMappingBuilder", Assert.Throws<ArgumentNullException>( () => mappingFragment.AddColumnMapping(null)).ParamName); Assert.Equal( Strings.InvalidColumnBuilderArgument("columnBuilderMapping"), Assert.Throws<ArgumentException>( () => mappingFragment.AddColumnMapping( new ColumnMappingBuilder(new EdmProperty("S"), new List<EdmProperty>()))).Message); }
BuildEntityTypeMapping(EntitySetMapping storeEntitySetMapping, SimpleMappingContext mappingContext, EntitySet storeEntitySet) { Debug.Assert(storeEntitySetMapping != null, "storeEntitySetMapping != null"); Debug.Assert(mappingContext != null, "mappingContext != null"); var entityType = storeEntitySetMapping.EntitySet.ElementType; var entityTypeMapping = new EntityTypeMapping(storeEntitySetMapping); entityTypeMapping.AddType(entityType); var mappingFragment = new MappingFragment(storeEntitySet, entityTypeMapping, false); entityTypeMapping.AddFragment(mappingFragment); foreach (var propertyMapping in BuildPropertyMapping(storeEntitySet.ElementType, mappingContext)) { mappingFragment.AddColumnMapping(propertyMapping); } return entityTypeMapping; }
public void Generate( EntityType entityType, IEnumerable<EdmProperty> properties, EntitySetMapping entitySetMapping, MappingFragment entityTypeMappingFragment, IList<EdmProperty> propertyPath, bool createNewColumn) { DebugCheck.NotNull(entityType); DebugCheck.NotNull(properties); DebugCheck.NotNull(entityTypeMappingFragment); DebugCheck.NotNull(propertyPath); var rootDeclaredProperties = entityType.GetRootType().DeclaredProperties; foreach (var property in properties) { if (property.IsComplexType && propertyPath.Any( p => p.IsComplexType && (p.ComplexType == property.ComplexType))) { throw Error.CircularComplexTypeHierarchy(); } propertyPath.Add(property); if (property.IsComplexType) { Generate( entityType, property.ComplexType.Properties, entitySetMapping, entityTypeMappingFragment, propertyPath, createNewColumn); } else { var tableColumn = entitySetMapping.EntityTypeMappings .SelectMany(etm => etm.MappingFragments) .SelectMany(etmf => etmf.ColumnMappings) .Where(pm => pm.PropertyPath.SequenceEqual(propertyPath)) .Select(pm => pm.ColumnProperty) .FirstOrDefault(); if (tableColumn == null || createNewColumn) { var columnName = string.Join("_", propertyPath.Select(p => p.Name)); tableColumn = MapTableColumn( property, columnName, !rootDeclaredProperties.Contains(propertyPath.First())); entityTypeMappingFragment.Table.AddColumn(tableColumn); if (entityType.KeyProperties().Contains(property)) { entityTypeMappingFragment.Table.AddKeyMember(tableColumn); } } entityTypeMappingFragment.AddColumnMapping( new ColumnMappingBuilder(tableColumn, propertyPath.ToList())); } propertyPath.Remove(property); } }
public void Generate_should_exclude_sgp_properties_from_corresponding_function_mappings() { var functionMappingGenerator = new ModificationFunctionMappingGenerator(ProviderRegistry.Sql2008_ProviderManifest); var databaseMapping = new DbDatabaseMapping() .Initialize(new EdmModel(DataSpace.CSpace), new EdmModel(DataSpace.SSpace)); var entityType = new EntityType("E", "N", DataSpace.CSpace); var intProperty = EdmProperty.CreatePrimitive("Id", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)); intProperty.SetStoreGeneratedPattern(StoreGeneratedPattern.Identity); entityType.AddKeyMember(intProperty); var stringProperty = EdmProperty.CreatePrimitive("Name", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); stringProperty.SetStoreGeneratedPattern(StoreGeneratedPattern.Computed); entityType.AddMember(stringProperty); var entitySetMapping = databaseMapping.AddEntitySetMapping( databaseMapping.Model.AddEntitySet("ES", entityType)); var storageEntityTypeMapping = new EntityTypeMapping( new EntitySetMapping(new EntitySet(), databaseMapping.EntityContainerMappings.Single())); storageEntityTypeMapping.AddType(entityType); var storageMappingFragment = new MappingFragment(new EntitySet(), storageEntityTypeMapping, false); storageMappingFragment.AddColumnMapping( new ColumnMappingBuilder(new EdmProperty("C0", TypeUsage.Create(new PrimitiveType() { DataSpace = DataSpace.SSpace })), new[] { intProperty })); storageMappingFragment.AddColumnMapping( new ColumnMappingBuilder(new EdmProperty("C1", TypeUsage.Create(new PrimitiveType() { DataSpace = DataSpace.SSpace })), new[] { stringProperty })); storageEntityTypeMapping.AddFragment(storageMappingFragment); entitySetMapping.AddTypeMapping(storageEntityTypeMapping); functionMappingGenerator.Generate(entityType, databaseMapping); var modificationFunctionMapping = entitySetMapping.ModificationFunctionMappings.Single(); Assert.NotNull(modificationFunctionMapping); var functionMapping = modificationFunctionMapping.InsertFunctionMapping; Assert.NotNull(functionMapping); Assert.Equal(0, functionMapping.ParameterBindings.Count); Assert.Equal(2, functionMapping.ResultBindings.Count); var function = functionMapping.Function; Assert.NotNull(function); Assert.Equal("E_Insert", function.Name); Assert.Equal(0, function.Parameters.Count); functionMapping = modificationFunctionMapping.UpdateFunctionMapping; Assert.NotNull(functionMapping); Assert.Equal(1, functionMapping.ParameterBindings.Count); Assert.Equal(1, functionMapping.ResultBindings.Count); function = functionMapping.Function; Assert.NotNull(function); Assert.Equal("E_Update", function.Name); Assert.Equal(1, function.Parameters.Count); functionMapping = modificationFunctionMapping.DeleteFunctionMapping; Assert.NotNull(functionMapping); Assert.Equal(1, functionMapping.ParameterBindings.Count); Assert.Null(functionMapping.ResultBindings); function = modificationFunctionMapping.DeleteFunctionMapping.Function; Assert.NotNull(function); Assert.Equal("E_Delete", function.Name); Assert.Equal(1, function.Parameters.Count); }
public static void MovePropertyMapping( DbDatabaseMapping databaseMapping, IEnumerable<EntitySet> entitySets, MappingFragment fromFragment, MappingFragment toFragment, ColumnMappingBuilder propertyMappingBuilder, bool requiresUpdate, bool useExisting) { // move the column from the formTable to the table in fragment if (requiresUpdate && fromFragment.Table != toFragment.Table) { UpdatePropertyMapping(databaseMapping, entitySets, GetColumnMappingIndex(databaseMapping), propertyMappingBuilder, fromFragment.Table, toFragment.Table, useExisting); } // move the propertyMapping fromFragment.RemoveColumnMapping(propertyMappingBuilder); toFragment.AddColumnMapping(propertyMappingBuilder); }
public void Cannot_add_duplicate_column_mapping_builder() { var mappingFragment = new MappingFragment( new EntitySet(), new EntityTypeMapping( new EntitySetMapping( new EntitySet(), new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace)))), false); var columnMappingBuilder = new ColumnMappingBuilder(new EdmProperty("S", TypeUsage.Create(new PrimitiveType() { DataSpace = DataSpace.SSpace })), new[] { new EdmProperty("S") }); mappingFragment.AddColumnMapping(columnMappingBuilder); Assert.Equal( Strings.InvalidColumnBuilderArgument("columnBuilderMapping"), Assert.Throws<ArgumentException>( () => mappingFragment.AddColumnMapping(columnMappingBuilder)).Message); }
public static void CopyPropertyMappingToFragment( ColumnMappingBuilder propertyMappingBuilder, MappingFragment fragment, Func<EdmProperty, bool> isCompatible, bool useExisting) { // Ensure column is in the fragment's table var column = TablePrimitiveOperations.IncludeColumn(fragment.Table, propertyMappingBuilder.ColumnProperty, isCompatible, useExisting); // Add the property mapping fragment.AddColumnMapping( new ColumnMappingBuilder(column, propertyMappingBuilder.PropertyPath)); }
public void Can_add_scalar_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 property = new EdmProperty("P"); var columnMappingBuilder = new ColumnMappingBuilder(columnProperty, new[] { property }); mappingFragment.AddColumnMapping(columnMappingBuilder); Assert.Same(columnMappingBuilder, mappingFragment.ColumnMappings.Single()); var scalarPropertyMapping = (ScalarPropertyMapping)mappingFragment.PropertyMappings.Single(); Assert.Same(columnProperty, scalarPropertyMapping.Column); Assert.Same(property, scalarPropertyMapping.Property); }