private static void RemoveColumnMapping(StructuralTypeMapping structuralTypeMapping, IEnumerable <EdmProperty> propertyPath)
        {
            DebugCheck.NotNull(structuralTypeMapping);
            DebugCheck.NotNull(propertyPath);

            // Remove the target column mapping by walking down the mapping fragment
            // tree corresponding to the passed-in property path until we reach the scalar
            // mapping leaf node. On the way out remove any empty mappings.

            var propertyMapping
                = structuralTypeMapping
                  .Properties
                  .Single(pm => ReferenceEquals(pm.EdmProperty, propertyPath.First()));

            if (propertyMapping is StorageScalarPropertyMapping)
            {
                structuralTypeMapping.RemoveProperty(propertyMapping);
            }
            else
            {
                var complexPropertyMapping = ((StorageComplexPropertyMapping)propertyMapping);
                var complexTypeMapping     = complexPropertyMapping.TypeMappings.Single();

                RemoveColumnMapping(complexTypeMapping, propertyPath.Skip(1));

                if (!complexTypeMapping.Properties.Any())
                {
                    structuralTypeMapping.RemoveProperty(complexPropertyMapping);
                }
            }
        }
Example #2
0
        internal void AddColumnMapping(ColumnMappingBuilder columnMappingBuilder)
        {
            Check.NotNull <ColumnMappingBuilder>(columnMappingBuilder, nameof(columnMappingBuilder));
            if (!columnMappingBuilder.PropertyPath.Any <EdmProperty>() || this._columnMappings.Contains(columnMappingBuilder))
            {
                throw new ArgumentException(Strings.InvalidColumnBuilderArgument((object)"columnBuilderMapping"));
            }
            this._columnMappings.Add(columnMappingBuilder);
            StructuralTypeMapping structuralTypeMapping = (StructuralTypeMapping)this;
            int         index;
            EdmProperty property;

            for (index = 0; index < columnMappingBuilder.PropertyPath.Count - 1; ++index)
            {
                property = columnMappingBuilder.PropertyPath[index];
                ComplexPropertyMapping complexPropertyMapping = structuralTypeMapping.PropertyMappings.OfType <ComplexPropertyMapping>().SingleOrDefault <ComplexPropertyMapping>((Func <ComplexPropertyMapping, bool>)(pm => object.ReferenceEquals((object)pm.Property, (object)property)));
                ComplexTypeMapping     typeMapping            = (ComplexTypeMapping)null;
                if (complexPropertyMapping == null)
                {
                    typeMapping = new ComplexTypeMapping(false);
                    typeMapping.AddType(property.ComplexType);
                    complexPropertyMapping = new ComplexPropertyMapping(property);
                    complexPropertyMapping.AddTypeMapping(typeMapping);
                    structuralTypeMapping.AddPropertyMapping((PropertyMapping)complexPropertyMapping);
                }
                structuralTypeMapping = (StructuralTypeMapping)(typeMapping ?? complexPropertyMapping.TypeMappings.Single <ComplexTypeMapping>());
            }
            property = columnMappingBuilder.PropertyPath[index];
            ScalarPropertyMapping scalarPropertyMapping1 = structuralTypeMapping.PropertyMappings.OfType <ScalarPropertyMapping>().SingleOrDefault <ScalarPropertyMapping>((Func <ScalarPropertyMapping, bool>)(pm => object.ReferenceEquals((object)pm.Property, (object)property)));

            if (scalarPropertyMapping1 == null)
            {
                ScalarPropertyMapping scalarPropertyMapping2 = new ScalarPropertyMapping(property, columnMappingBuilder.ColumnProperty);
                structuralTypeMapping.AddPropertyMapping((PropertyMapping)scalarPropertyMapping2);
                columnMappingBuilder.SetTarget(scalarPropertyMapping2);
            }
            else
            {
                scalarPropertyMapping1.Column = columnMappingBuilder.ColumnProperty;
            }
        }
Example #3
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);
            }
        }
        private static void RemoveColumnMapping(StructuralTypeMapping structuralTypeMapping, IEnumerable<EdmProperty> propertyPath)
        {
            DebugCheck.NotNull(structuralTypeMapping);
            DebugCheck.NotNull(propertyPath);

            // Remove the target column mapping by walking down the mapping fragment
            // tree corresponding to the passed-in property path until we reach the scalar
            // mapping leaf node. On the way out remove any empty mappings.

            var propertyMapping
                = structuralTypeMapping
                    .Properties
                    .Single(pm => ReferenceEquals(pm.EdmProperty, propertyPath.First()));

            if (propertyMapping is StorageScalarPropertyMapping)
            {
                structuralTypeMapping.RemoveProperty(propertyMapping);
            }
            else
            {
                var complexPropertyMapping = ((StorageComplexPropertyMapping)propertyMapping);
                var complexTypeMapping = complexPropertyMapping.TypeMappings.Single();

                RemoveColumnMapping(complexTypeMapping, propertyPath.Skip(1));

                if (!complexTypeMapping.Properties.Any())
                {
                    structuralTypeMapping.RemoveProperty(complexPropertyMapping);
                }
            }
        }
		/// <summary>
		/// Gets the name of the column for the property from the metadata.
		/// Set nestedProperty for the property of a complex type to lookup.
		/// </summary>
		/// <param name="mappingFragment">EF metadata for finding mappings.</param>
		/// <param name="edmProperty">The of the model to find the column for (for simple types), or for complex types this is the containing complex type.</param>
		/// <param name="nestedProperty">Only required to map complex types. The property of the complex type to find the column name for.</param>
		/// <returns>The column name for the property</returns>
		/// <exception cref="EnumGeneratorException">
		/// </exception>
		private static string GetColumnName(StructuralTypeMapping mappingFragment, EdmProperty edmProperty, EdmProperty nestedProperty = null)
		{
			var propertyMapping = GetPropertyMapping(mappingFragment, edmProperty);

			if (nestedProperty != null)
			{
				var complexPropertyMapping = propertyMapping as ComplexPropertyMapping;
				if (complexPropertyMapping == null)
				{
					throw new EnumGeneratorException(string.Format(
						"Failed to cast complex property mapping for {0}.{1} to ComplexPropertyMapping", edmProperty, nestedProperty));
				}
				var complexTypeMappings = complexPropertyMapping.TypeMappings;
				if (complexTypeMappings.Count() != 1)
				{
					throw new EnumGeneratorException(string.Format(
						"{0} complexPropertyMapping TypeMappings found for property {1}.{2}", complexTypeMappings.Count(), edmProperty, nestedProperty));
				}
				var complexTypeMapping = complexTypeMappings.Single();
				var propertyMappings = complexTypeMapping.PropertyMappings.Where(pm => pm.Property.Name == nestedProperty.Name).ToList();
				if (propertyMappings.Count() != 1)
				{
					throw new EnumGeneratorException(string.Format(
						"{0} complexMappings found for property {1}.{2}", propertyMappings.Count(), edmProperty, nestedProperty));
				}

				propertyMapping = propertyMappings.Single();
			}

			return GetColumnNameFromPropertyMapping(edmProperty, propertyMapping);
		}
		private static PropertyMapping GetPropertyMapping(StructuralTypeMapping mappingFragment, EdmProperty edmProperty)
		{
			var matches = mappingFragment.PropertyMappings.Where(m => m.Property.Name == edmProperty.Name).ToList();
			if (matches.Count() != 1)
			{
				throw new EnumGeneratorException(string.Format(
					"{0} matches found for property {1}", matches.Count(), edmProperty));
			}
			var match = matches.Single();
			return match;
		}
        public void AddColumnMapping(ColumnMappingBuilder columnMappingBuilder)
        {
            Check.NotNull(columnMappingBuilder, "columnMappingBuilder");
            if (!columnMappingBuilder.PropertyPath.Any() ||
                _columnMappings.Contains(columnMappingBuilder))
            {
                throw new ArgumentException(Strings.InvalidColumnBuilderArgument("columnBuilderMapping"));
            }

            DebugCheck.NotNull(columnMappingBuilder.ColumnProperty);

            _columnMappings.Add(columnMappingBuilder);

            StructuralTypeMapping structuralTypeMapping = this;
            EdmProperty           property;

            // Turn the property path into a mapping fragment nested tree structure.

            var i = 0;

            for (; i < columnMappingBuilder.PropertyPath.Count - 1; i++)
            {
                // The first n-1 properties are complex so we just need to build
                // a corresponding tree of complex type mappings.

                property = columnMappingBuilder.PropertyPath[i];

                var complexPropertyMapping
                    = structuralTypeMapping
                      .Properties
                      .OfType <StorageComplexPropertyMapping>()
                      .SingleOrDefault(pm => ReferenceEquals(pm.EdmProperty, property));

                StorageComplexTypeMapping complexTypeMapping = null;

                if (complexPropertyMapping == null)
                {
                    complexTypeMapping = new StorageComplexTypeMapping(false);
                    complexTypeMapping.AddType(property.ComplexType);

                    complexPropertyMapping = new StorageComplexPropertyMapping(property);
                    complexPropertyMapping.AddTypeMapping(complexTypeMapping);

                    structuralTypeMapping.AddProperty(complexPropertyMapping);
                }

                structuralTypeMapping
                    = complexTypeMapping
                      ?? complexPropertyMapping.TypeMappings.Single();
            }

            // The last property has to be a scalar mapping to the target column.
            // Extract it and create the scalar mapping leaf node, ensuring that we
            // set the target column.

            property = columnMappingBuilder.PropertyPath[i];

            var scalarPropertyMapping
                = structuralTypeMapping
                  .Properties
                  .OfType <StorageScalarPropertyMapping>()
                  .SingleOrDefault(pm => ReferenceEquals(pm.EdmProperty, property));

            if (scalarPropertyMapping == null)
            {
                scalarPropertyMapping
                    = new StorageScalarPropertyMapping(property, columnMappingBuilder.ColumnProperty);

                structuralTypeMapping.AddProperty(scalarPropertyMapping);

                columnMappingBuilder.SetTarget(scalarPropertyMapping);
            }
            else
            {
                scalarPropertyMapping.ColumnProperty = columnMappingBuilder.ColumnProperty;
            }
        }