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);
        }
Ejemplo n.º 2
0
        private static void RemoveColumnMapping(
            StructuralTypeMapping structuralTypeMapping,
            IEnumerable <EdmProperty> propertyPath)
        {
            PropertyMapping propertyMapping = structuralTypeMapping.PropertyMappings.Single <PropertyMapping>((Func <PropertyMapping, bool>)(pm => object.ReferenceEquals((object)pm.Property, (object)propertyPath.First <EdmProperty>())));

            if (propertyMapping is ScalarPropertyMapping)
            {
                structuralTypeMapping.RemovePropertyMapping(propertyMapping);
            }
            else
            {
                ComplexPropertyMapping complexPropertyMapping = (ComplexPropertyMapping)propertyMapping;
                ComplexTypeMapping     complexTypeMapping     = complexPropertyMapping.TypeMappings.Single <ComplexTypeMapping>();
                MappingFragment.RemoveColumnMapping((StructuralTypeMapping)complexTypeMapping, propertyPath.Skip <EdmProperty>(1));
                if (complexTypeMapping.PropertyMappings.Any <PropertyMapping>())
                {
                    return;
                }
                structuralTypeMapping.RemovePropertyMapping((PropertyMapping)complexPropertyMapping);
            }
        }
        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);
        }
Ejemplo n.º 4
0
        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);
        }
        // <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();
            }
        }
Ejemplo n.º 6
0
 internal void RemoveColumnMapping(ColumnMappingBuilder columnMappingBuilder)
 {
     this._columnMappings.Remove(columnMappingBuilder);
     MappingFragment.RemoveColumnMapping((StructuralTypeMapping)this, (IEnumerable <EdmProperty>)columnMappingBuilder.PropertyPath);
 }