public static void GetIdentity_of_StorageComplexTypeMapping_returns_expected_value()
        {
            var complexType1     = new ComplexType("CT1", "N", DataSpace.CSpace);
            var complexType2     = new ComplexType("CT2", "N", DataSpace.CSpace);
            var complexType3     = new ComplexType("CT3", "N", DataSpace.CSpace);
            var complexType4     = new ComplexType("CT4", "N", DataSpace.CSpace);
            var property1        = new EdmProperty("A", TypeUsage.Create(complexType1));
            var property2        = new EdmProperty("B", TypeUsage.Create(complexType2));
            var propertyMapping1 = new ComplexPropertyMapping(property1);
            var propertyMapping2 = new ComplexPropertyMapping(property2);

            var mapping = new ComplexTypeMapping(false);

            mapping.AddType(complexType2);
            mapping.AddType(complexType1);
            mapping.AddIsOfType(complexType4);
            mapping.AddIsOfType(complexType3);
            mapping.AddPropertyMapping(propertyMapping2);
            mapping.AddPropertyMapping(propertyMapping1);

            Assert.Equal("ComplexProperty(Identity=A),ComplexProperty(Identity=B),N.CT1,N.CT2,N.CT3,N.CT4",
                         BaseMetadataMappingVisitor.IdentityHelper.GetIdentity(mapping));
        }
示例#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;
            }
        }
示例#3
0
        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
                      .PropertyMappings
                      .OfType <ComplexPropertyMapping>()
                      .SingleOrDefault(pm => ReferenceEquals(pm.Property, property));

                ComplexTypeMapping complexTypeMapping = null;

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

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

                    structuralTypeMapping.AddPropertyMapping(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
                  .PropertyMappings
                  .OfType <ScalarPropertyMapping>()
                  .SingleOrDefault(pm => ReferenceEquals(pm.Property, property));

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

                structuralTypeMapping.AddPropertyMapping(scalarPropertyMapping);

                columnMappingBuilder.SetTarget(scalarPropertyMapping);
            }
            else
            {
                scalarPropertyMapping.Column = columnMappingBuilder.ColumnProperty;
            }
        }
        internal 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
                        .PropertyMappings
                        .OfType<ComplexPropertyMapping>()
                        .SingleOrDefault(pm => ReferenceEquals(pm.Property, property));

                ComplexTypeMapping complexTypeMapping = null;

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

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

                    structuralTypeMapping.AddPropertyMapping(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
                    .PropertyMappings
                    .OfType<ScalarPropertyMapping>()
                    .SingleOrDefault(pm => ReferenceEquals(pm.Property, property));

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

                structuralTypeMapping.AddPropertyMapping(scalarPropertyMapping);

                columnMappingBuilder.SetTarget(scalarPropertyMapping);
            }
            else
            {
                scalarPropertyMapping.Column = columnMappingBuilder.ColumnProperty;
            }
        }
        public static void GetIdentity_of_StorageComplexTypeMapping_returns_expected_value()
        {
            var complexType1 = new ComplexType("CT1", "N", DataSpace.CSpace);
            var complexType2 = new ComplexType("CT2", "N", DataSpace.CSpace);
            var complexType3 = new ComplexType("CT3", "N", DataSpace.CSpace);
            var complexType4 = new ComplexType("CT4", "N", DataSpace.CSpace);
            var property1 = new EdmProperty("A", TypeUsage.Create(complexType1));
            var property2 = new EdmProperty("B", TypeUsage.Create(complexType2));
            var propertyMapping1 = new ComplexPropertyMapping(property1);
            var propertyMapping2 = new ComplexPropertyMapping(property2);

            var mapping = new ComplexTypeMapping(false);
            mapping.AddType(complexType2);
            mapping.AddType(complexType1);
            mapping.AddIsOfType(complexType4);
            mapping.AddIsOfType(complexType3);
            mapping.AddPropertyMapping(propertyMapping2);
            mapping.AddPropertyMapping(propertyMapping1);

            Assert.Equal("ComplexProperty(Identity=A),ComplexProperty(Identity=B),N.CT1,N.CT2,N.CT3,N.CT4",
                BaseMetadataMappingVisitor.IdentityHelper.GetIdentity(mapping));
        }