Example #1
0
        /// <summary>
        /// Adds a type mapping corresponding to a nested complex type.
        /// </summary>
        /// <param name="typeMapping">The complex type mapping to be added.</param>
        public void AddTypeMapping(ComplexTypeMapping typeMapping)
        {
            Check.NotNull(typeMapping, "typeMapping");
            ThrowIfReadOnly();

            _typeMappings.Add(typeMapping);
        }
Example #2
0
        public void Can_create_with_valid_complex_type()
        {
            var complexType        = new ComplexType();
            var complexTypeMapping = new ComplexTypeMapping(complexType);

            Assert.Same(complexType, complexTypeMapping.ComplexType);
        }
Example #3
0
        /// <summary>
        /// Removes a type mapping corresponding to a nested complex type.
        /// </summary>
        /// <param name="typeMapping">The complex type mapping to be removed.</param>
        public void RemoveTypeMapping(ComplexTypeMapping typeMapping)
        {
            Check.NotNull(typeMapping, "typeMapping");
            ThrowIfReadOnly();

            _typeMappings.Remove(typeMapping);
        }
        public void Can_create_with_valid_complex_type()
        {
            var complexType = new ComplexType();
            var complexTypeMapping = new ComplexTypeMapping(complexType);

            Assert.Same(complexType, complexTypeMapping.ComplexType);
        }
Example #5
0
            public static string GetIdentity(ComplexTypeMapping mapping)
            {
                var properties = mapping.AllProperties.Select(it => GetIdentity(it))
                                 .OrderBy(it => it, StringComparer.Ordinal);
                var types = mapping.Types.Select(it => it.Identity)
                            .OrderBy(it => it, StringComparer.Ordinal);
                var isOfTypes = mapping.IsOfTypes.Select(it => it.Identity)
                                .OrderBy(it => it, StringComparer.Ordinal);

                return(string.Join(",", properties.Concat(types).Concat(isOfTypes)));
            }
        protected override void Visit(ComplexTypeMapping complexTypeMapping)
        {
            int instanceIndex;

            if (!this.AddObjectToSeenListAndHashBuilder((object)complexTypeMapping, out instanceIndex))
            {
                return;
            }
            this.AddObjectStartDumpToHashBuilder((object)complexTypeMapping, instanceIndex);
            base.Visit(complexTypeMapping);
            this.AddObjectEndDumpToHashBuilder();
        }
        public void Cannot_add_property_when_read_only()
        {
            var complexType = new ComplexType();
            var complexTypeMapping = new ComplexTypeMapping(complexType);

            complexTypeMapping.SetReadOnly();

            var scalarPropertyMapping = new ScalarPropertyMapping(new EdmProperty("P"), new EdmProperty("C", TypeUsage.Create(new PrimitiveType() { DataSpace = DataSpace.SSpace })));

            Assert.Equal(
                Strings.OperationOnReadOnlyItem,
                Assert.Throws<InvalidOperationException>(
                    () => complexTypeMapping.AddPropertyMapping(scalarPropertyMapping)).Message);
        }
Example #8
0
        public void Cannot_add_condition_when_read_only()
        {
            var complexType        = new ComplexType();
            var complexTypeMapping = new ComplexTypeMapping(complexType);

            complexTypeMapping.SetReadOnly();

            var conditionMapping = new IsNullConditionMapping(new EdmProperty("P"), true);

            Assert.Equal(
                Strings.OperationOnReadOnlyItem,
                Assert.Throws <InvalidOperationException>(
                    () => complexTypeMapping.AddCondition(conditionMapping)).Message);
        }
Example #9
0
        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);
        }
Example #10
0
 protected virtual void Visit(ComplexTypeMapping complexTypeMapping)
 {
     foreach (PropertyMapping propertyMapping in this.GetSequence <PropertyMapping>((IEnumerable <PropertyMapping>)complexTypeMapping.AllProperties, (Func <PropertyMapping, string>)(it => BaseMetadataMappingVisitor.IdentityHelper.GetIdentity(it))))
     {
         this.Visit(propertyMapping);
     }
     foreach (ComplexType complexType in this.GetSequence <ComplexType>((IEnumerable <ComplexType>)complexTypeMapping.IsOfTypes, (Func <ComplexType, string>)(it => it.Identity)))
     {
         this.Visit(complexType);
     }
     foreach (ComplexType complexType in this.GetSequence <ComplexType>((IEnumerable <ComplexType>)complexTypeMapping.Types, (Func <ComplexType, string>)(it => it.Identity)))
     {
         this.Visit(complexType);
     }
 }
Example #11
0
        public void Can_add_remove_conditions()
        {
            var complexType        = new ComplexType();
            var complexTypeMapping = new ComplexTypeMapping(complexType);
            var conditionMapping   = new IsNullConditionMapping(new EdmProperty("P"), true);

            Assert.Empty(complexTypeMapping.Conditions);

            complexTypeMapping.AddCondition(conditionMapping);

            Assert.Same(conditionMapping, complexTypeMapping.Conditions.Single());

            complexTypeMapping.RemoveCondition(conditionMapping);

            Assert.Empty(complexTypeMapping.Conditions);
        }
        public void Can_add_remove_properties()
        {
            var complexType = new ComplexType();
            var complexTypeMapping = new ComplexTypeMapping(complexType);
            var scalarPropertyMapping = new ScalarPropertyMapping(new EdmProperty("P"), new EdmProperty("C", TypeUsage.Create(new PrimitiveType() { DataSpace = DataSpace.SSpace })));

            Assert.Empty(complexTypeMapping.PropertyMappings);

            complexTypeMapping.AddPropertyMapping(scalarPropertyMapping);

            Assert.Same(scalarPropertyMapping, complexTypeMapping.PropertyMappings.Single());

            complexTypeMapping.RemovePropertyMapping(scalarPropertyMapping);

            Assert.Empty(complexTypeMapping.PropertyMappings);
        }
        public void Can_add_remove_conditions()
        {
            var complexType = new ComplexType();
            var complexTypeMapping = new ComplexTypeMapping(complexType);
            var conditionMapping = new IsNullConditionMapping(new EdmProperty("P"), true);

            Assert.Empty(complexTypeMapping.Conditions);

            complexTypeMapping.AddCondition(conditionMapping);

            Assert.Same(conditionMapping, complexTypeMapping.Conditions.Single());

            complexTypeMapping.RemoveCondition(conditionMapping);

            Assert.Empty(complexTypeMapping.Conditions);
        }
Example #14
0
        protected virtual void Visit(ComplexTypeMapping complexTypeMapping)
        {
            foreach (var property in GetSequence(complexTypeMapping.AllProperties, it => IdentityHelper.GetIdentity(it)))
            {
                Visit(property);
            }

            foreach (var type in GetSequence(complexTypeMapping.IsOfTypes, it => it.Identity))
            {
                Visit(type);
            }

            foreach (var type in GetSequence(complexTypeMapping.Types, it => it.Identity))
            {
                Visit(type);
            }
        }
        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);
        }
Example #16
0
        public void Cannot_add_property_when_read_only()
        {
            var complexType        = new ComplexType();
            var complexTypeMapping = new ComplexTypeMapping(complexType);

            complexTypeMapping.SetReadOnly();

            var scalarPropertyMapping = new ScalarPropertyMapping(new EdmProperty("P"), new EdmProperty("C", TypeUsage.Create(new PrimitiveType()
            {
                DataSpace = DataSpace.SSpace
            })));

            Assert.Equal(
                Strings.OperationOnReadOnlyItem,
                Assert.Throws <InvalidOperationException>(
                    () => complexTypeMapping.AddProperty(scalarPropertyMapping)).Message);
        }
Example #17
0
        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);
        }
Example #19
0
        public void Can_add_remove_properties()
        {
            var complexType           = new ComplexType();
            var complexTypeMapping    = new ComplexTypeMapping(complexType);
            var scalarPropertyMapping = new ScalarPropertyMapping(new EdmProperty("P"), new EdmProperty("C", TypeUsage.Create(new PrimitiveType()
            {
                DataSpace = DataSpace.SSpace
            })));

            Assert.Empty(complexTypeMapping.Properties);

            complexTypeMapping.AddProperty(scalarPropertyMapping);

            Assert.Same(scalarPropertyMapping, complexTypeMapping.Properties.Single());

            complexTypeMapping.RemoveProperty(scalarPropertyMapping);

            Assert.Empty(complexTypeMapping.Properties);
        }
Example #20
0
        public void SetReadOnly_is_called_on_child_mapping_items()
        {
            var complexType           = new ComplexType();
            var complexTypeMapping    = new ComplexTypeMapping(complexType);
            var scalarPropertyMapping = new ScalarPropertyMapping(new EdmProperty("P"), new EdmProperty("C", TypeUsage.Create(new PrimitiveType()
            {
                DataSpace = DataSpace.SSpace
            })));
            var conditionMapping = new IsNullConditionMapping(new EdmProperty("P"), true);

            complexTypeMapping.AddProperty(scalarPropertyMapping);
            complexTypeMapping.AddCondition(conditionMapping);

            Assert.False(scalarPropertyMapping.IsReadOnly);
            Assert.False(conditionMapping.IsReadOnly);
            complexTypeMapping.SetReadOnly();
            Assert.True(scalarPropertyMapping.IsReadOnly);
            Assert.True(conditionMapping.IsReadOnly);
        }
        protected override void Visit(ComplexTypeMapping complexTypeMapping)
        {
            int index;

            if (!AddObjectToSeenListAndHashBuilder(complexTypeMapping, out index))
            {
                return;
            }

            AddObjectStartDumpToHashBuilder(complexTypeMapping, index);

            #region Inner data visit

            base.Visit(complexTypeMapping);

            #endregion

            AddObjectEndDumpToHashBuilder();
        }
Example #22
0
        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);
        }
Example #23
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 #24
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 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));
        }
        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));
        }
        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;
            }
        }
        /// <summary>
        /// Adds a type mapping corresponding to a nested complex type.
        /// </summary>
        /// <param name="typeMapping">The complex type mapping to be added.</param>
        public void AddTypeMapping(ComplexTypeMapping typeMapping)
        {
            Check.NotNull(typeMapping, "typeMapping");
            ThrowIfReadOnly();

            _typeMappings.Add(typeMapping);
        }
Example #29
0
 /// <summary>
 /// Removes a type mapping corresponding to a nested complex type.
 /// </summary>
 /// <param name="typeMapping">The complex type mapping to be removed.</param>
 public void RemoveTypeMapping(ComplexTypeMapping typeMapping)
 {
     Check.NotNull <ComplexTypeMapping>(typeMapping, nameof(typeMapping));
     this.ThrowIfReadOnly();
     this._typeMappings.Remove(typeMapping);
 }
Example #30
0
 public static string GetIdentity(ComplexTypeMapping mapping)
 {
     return(string.Join(",", mapping.AllProperties.Select <PropertyMapping, string>((Func <PropertyMapping, string>)(it => BaseMetadataMappingVisitor.IdentityHelper.GetIdentity(it))).OrderBy <string, string>((Func <string, string>)(it => it), (IComparer <string>)StringComparer.Ordinal).Concat <string>((IEnumerable <string>)mapping.Types.Select <ComplexType, string>((Func <ComplexType, string>)(it => it.Identity)).OrderBy <string, string>((Func <string, string>)(it => it), (IComparer <string>)StringComparer.Ordinal)).Concat <string>((IEnumerable <string>)mapping.IsOfTypes.Select <ComplexType, string>((Func <ComplexType, string>)(it => it.Identity)).OrderBy <string, string>((Func <string, string>)(it => it), (IComparer <string>)StringComparer.Ordinal))));
 }
        public void SetReadOnly_is_called_on_child_mapping_items()
        {
            var complexType = new ComplexType();
            var complexTypeMapping = new ComplexTypeMapping(complexType);
            var scalarPropertyMapping = new ScalarPropertyMapping(new EdmProperty("P"), new EdmProperty("C", TypeUsage.Create(new PrimitiveType() { DataSpace = DataSpace.SSpace })));
            var conditionMapping = new IsNullConditionMapping(new EdmProperty("P"), true);

            complexTypeMapping.AddPropertyMapping(scalarPropertyMapping);
            complexTypeMapping.AddCondition(conditionMapping);

            Assert.False(scalarPropertyMapping.IsReadOnly);
            Assert.False(conditionMapping.IsReadOnly);
            complexTypeMapping.SetReadOnly();
            Assert.True(scalarPropertyMapping.IsReadOnly);
            Assert.True(conditionMapping.IsReadOnly);
        }
Example #32
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;
            }
        }
        public void Cannot_add_condition_when_read_only()
        {
            var complexType = new ComplexType();
            var complexTypeMapping = new ComplexTypeMapping(complexType);

            complexTypeMapping.SetReadOnly();

            var conditionMapping = new IsNullConditionMapping(new EdmProperty("P"), true);

            Assert.Equal(
                Strings.OperationOnReadOnlyItem,
                Assert.Throws<InvalidOperationException>(
                    () => complexTypeMapping.AddCondition(conditionMapping)).Message);
        }
        protected override void Visit(ComplexTypeMapping complexTypeMapping)
        {
            int index;
            if (!AddObjectToSeenListAndHashBuilder(complexTypeMapping, out index))
            {
                return;
            }

            AddObjectStartDumpToHashBuilder(complexTypeMapping, index);

            #region Inner data visit

            base.Visit(complexTypeMapping);

            #endregion

            AddObjectEndDumpToHashBuilder();
        }
        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);
        }
        /// <summary>
        /// Removes a type mapping corresponding to a nested complex type.
        /// </summary>
        /// <param name="typeMapping">The complex type mapping to be removed.</param>
        public void RemoveTypeMapping(ComplexTypeMapping typeMapping)
        {
            Check.NotNull(typeMapping, "typeMapping");
            ThrowIfReadOnly();

            _typeMappings.Remove(typeMapping);
        }