public void WriteEntitySetMappingElement(EntitySetMapping entitySetMapping)
        {
            DebugCheck.NotNull(entitySetMapping);

            _xmlWriter.WriteStartElement(MslConstructs.EntitySetMappingElement);
            _xmlWriter.WriteAttributeString(MslConstructs.EntitySetMappingNameAttribute, entitySetMapping.EntitySet.Name);

            foreach (var entityTypeMapping in entitySetMapping.EntityTypeMappings)
            {
                WriteEntityTypeMappingElement(entityTypeMapping);
            }

            foreach (var modificationFunctionMapping in entitySetMapping.ModificationFunctionMappings)
            {
                _xmlWriter.WriteStartElement(MslConstructs.EntityTypeMappingElement);
                _xmlWriter.WriteAttributeString(
                    MslConstructs.EntityTypeMappingTypeNameAttribute,
                    GetEntityTypeName(_entityTypeNamespace + "." + modificationFunctionMapping.EntityType.Name, false));

                WriteModificationFunctionMapping(modificationFunctionMapping);

                _xmlWriter.WriteEndElement();
            }

            _xmlWriter.WriteEndElement();
        }
Esempio n. 2
0
        public void GenerateViews_generates_views_for_all_containers_that_contain_mappings()
        {
            var storageMappingItemCollection =
                CreateStorageMappingItemCollection(new[] { Ssdl, SchoolSsdl }, new[] { Csdl, SchoolCsdl }, new[] { Msl });

            // Add second container mapping without type mappings.
            var conceptualEntityContainer = storageMappingItemCollection.EdmItemCollection.GetItems <EntityContainer>()[1];
            var storeEntityContainer      = storageMappingItemCollection.StoreItemCollection.GetItems <EntityContainer>()[1];
            var storeEntitySet            = storeEntityContainer.EntitySets.First();

            var containerMapping = new EntityContainerMapping(
                conceptualEntityContainer, storeEntityContainer, storageMappingItemCollection, false);

            var entitySetMapping = new EntitySetMapping(storeEntitySet, containerMapping);

            containerMapping.AddSetMapping(entitySetMapping);

            containerMapping.SetReadOnly();
            storageMappingItemCollection.AddInternal(containerMapping);

            var errors = new List <EdmSchemaError>();
            var views  = storageMappingItemCollection.GenerateViews("AdventureWorksEntities3", "AdventureWorksModelStoreContainer", errors);

            Assert.Empty(errors);
            Assert.Equal(2, views.Count);

            errors = new List <EdmSchemaError>();
            views  = storageMappingItemCollection.GenerateViews("SchoolContainer", "SchoolStoreContainer", errors);

            Assert.Empty(errors);
            Assert.Equal(0, views.Count);
        }
Esempio n. 3
0
        public void Can_clear_modification_function_mappings()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var entitySet  = new EntitySet("S", "N", null, null, entityType);
            var function   = new EdmFunction("F", "N", DataSpace.SSpace, new EdmFunctionPayload());

            var container = new EntityContainer("C", DataSpace.CSpace);

            container.AddEntitySetBase(entitySet);

            var entitySetMapping =
                new EntitySetMapping(
                    entitySet,
                    new EntityContainerMapping(container));

            var functionMapping =
                new ModificationFunctionMapping(
                    entitySet,
                    entityType,
                    function,
                    Enumerable.Empty <ModificationFunctionParameterBinding>(),
                    null,
                    null);

            var entityFunctionMappings =
                new EntityTypeModificationFunctionMapping(entityType, functionMapping, null, null);

            entitySetMapping.AddModificationFunctionMapping(entityFunctionMappings);

            Assert.Equal(1, entitySetMapping.ModificationFunctionMappings.Count());

            entitySetMapping.ClearModificationFunctionMappings();

            Assert.Equal(0, entitySetMapping.ModificationFunctionMappings.Count());
        }
        public void WriteMappingFragment_should_write_store_entity_set_name()
        {
            var fixture = new Fixture();

            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var entitySet = new EntitySet("ES", "S", null, null, entityType);
            var entityContainer = new EntityContainer("EC", DataSpace.SSpace);

            entityContainer.AddEntitySetBase(entitySet);

            var storageEntitySetMapping
                = new EntitySetMapping(
                    entitySet,
                    new EntityContainerMapping(entityContainer));

            TypeMapping typeMapping = new EntityTypeMapping(storageEntitySetMapping);

            var mappingFragment = new MappingFragment(entitySet, typeMapping, false);

            fixture.Writer.WriteMappingFragmentElement(mappingFragment);

            Assert.Equal(
                @"<MappingFragment StoreEntitySet=""ES"" />",
                fixture.ToString());
        }
        public static void RemoveDefaultDiscriminator(
            this MappingFragment entityTypeMappingFragment, EntitySetMapping entitySetMapping)
        {
            DebugCheck.NotNull(entityTypeMappingFragment);

            var discriminatorColumn = entityTypeMappingFragment.RemoveDefaultDiscriminatorCondition();
            if (discriminatorColumn != null)
            {
                var table = entityTypeMappingFragment.Table;

                table.Properties
                     .Where(c => c.Name.Equals(discriminatorColumn.Name, StringComparison.Ordinal))
                     .ToList()
                     .Each(table.RemoveMember);
            }

            if (entitySetMapping != null
                && entityTypeMappingFragment.IsConditionOnlyFragment()
                &&
                !entityTypeMappingFragment.ColumnConditions.Any())
            {
                var entityTypeMapping =
                    entitySetMapping.EntityTypeMappings.Single(
                        etm => etm.MappingFragments.Contains(entityTypeMappingFragment));

                entityTypeMapping.RemoveFragment(entityTypeMappingFragment);

                if (entityTypeMapping.MappingFragments.Count == 0)
                {
                    entitySetMapping.RemoveTypeMapping(entityTypeMapping);
                }
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Removes an association set mapping.
        /// </summary>
        /// <param name="setMapping">The association set mapping to remove.</param>
        public void RemoveSetMapping(EntitySetMapping setMapping)
        {
            Check.NotNull(setMapping, "setMapping");
            Util.ThrowIfReadOnly(this);

            m_entitySetMappings.Remove(setMapping.Set.Name);
        }
        public void Can_clear_modification_function_mappings()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var entitySet = new EntitySet("S", "N", null, null, entityType);
            var function = new EdmFunction("F", "N", DataSpace.SSpace, new EdmFunctionPayload());

            var container = new EntityContainer("C", DataSpace.CSpace);
            container.AddEntitySetBase(entitySet);

            var entitySetMapping =
                new EntitySetMapping(
                    entitySet,
                    new EntityContainerMapping(container));

            var functionMapping =
                new ModificationFunctionMapping(
                    entitySet,
                    entityType,
                    function,
                    Enumerable.Empty<ModificationFunctionParameterBinding>(),
                    null,
                    null);

            var entityFunctionMappings =
                new EntityTypeModificationFunctionMapping(entityType, functionMapping, null, null);

            entitySetMapping.AddModificationFunctionMapping(entityFunctionMappings);

            Assert.Equal(1, entitySetMapping.ModificationFunctionMappings.Count());

            entitySetMapping.ClearModificationFunctionMappings();

            Assert.Equal(0, entitySetMapping.ModificationFunctionMappings.Count());
        }
Esempio n. 8
0
        public void Can_get_entity_set()
        {
            var entityContainerMapping = new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace));
            var entitySet        = new EntitySet();
            var entitySetMapping = new EntitySetMapping(entitySet, entityContainerMapping);

            Assert.Same(entitySet, entitySetMapping.EntitySet);
        }
        public void Can_get_entity_set()
        {
            var entityContainerMapping = new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace));
            var entitySet = new EntitySet();
            var entitySetMapping = new EntitySetMapping(entitySet, entityContainerMapping);

            Assert.Same(entitySet, entitySetMapping.EntitySet);
        }
Esempio n. 10
0
        public void Can_get_container_mapping()
        {
            var containerMapping = new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace));
            var storageSetMapping
                = new EntitySetMapping(
                      new EntitySet(),
                      containerMapping);

            Assert.Same(containerMapping, storageSetMapping.EntityContainerMapping);
        }
 /// <summary>Adds an entity set mapping.</summary>
 /// <param name="setMapping">The entity set mapping to add.</param>
 public void AddSetMapping(EntitySetMapping setMapping)
 {
     Check.NotNull <EntitySetMapping>(setMapping, nameof(setMapping));
     Util.ThrowIfReadOnly((MetadataItem)this);
     if (this.m_entitySetMappings.ContainsKey(setMapping.Set.Name))
     {
         return;
     }
     this.m_entitySetMappings.Add(setMapping.Set.Name, (EntitySetBaseMapping)setMapping);
 }
Esempio n. 12
0
        public void Can_get_container_mapping()
        {
            var containerMapping = new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace));
            var storageSetMapping
                = new EntitySetMapping(
                    new EntitySet(),
                    containerMapping);

            Assert.Same(containerMapping, storageSetMapping.EntityContainerMapping);
        }
Esempio n. 13
0
        /// <summary>
        /// Adds an entity set mapping.
        /// </summary>
        /// <param name="setMapping">The entity set mapping to add.</param>
        public void AddSetMapping(EntitySetMapping setMapping)
        {
            Check.NotNull(setMapping, "setMapping");
            Util.ThrowIfReadOnly(this);

            if (!m_entitySetMappings.ContainsKey(setMapping.Set.Name))
            {
                m_entitySetMappings.Add(setMapping.Set.Name, setMapping);
            }
        }
Esempio n. 14
0
        public void Can_not_add_null_mapping()
        {
            var storageSetMapping
                = new EntitySetMapping(
                    new EntitySet(),
                    new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace)));

            Assert.Equal(
                "typeMapping",
                Assert.Throws<ArgumentNullException>(() => storageSetMapping.AddTypeMapping(null)).ParamName);
        }
Esempio n. 15
0
        public void Can_not_add_null_mapping()
        {
            var storageSetMapping
                = new EntitySetMapping(
                      new EntitySet(),
                      new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace)));

            Assert.Equal(
                "typeMapping",
                Assert.Throws <ArgumentNullException>(() => storageSetMapping.AddTypeMapping(null)).ParamName);
        }
Esempio n. 16
0
        public void Can_get_set_mapping()
        {
            var storageSetMapping
                = new EntitySetMapping(
                    new EntitySet(),
                    new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace)));

            var storageTypeMapping
                = new EntityTypeMapping(storageSetMapping);

            Assert.Same(storageSetMapping, storageTypeMapping.SetMapping);
        }
Esempio n. 17
0
        public void Can_get_set_mapping()
        {
            var storageSetMapping
                = new EntitySetMapping(
                      new EntitySet(),
                      new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace)));

            var storageTypeMapping
                = new EntityTypeMapping(storageSetMapping);

            Assert.Same(storageSetMapping, storageTypeMapping.SetMapping);
        }
Esempio n. 18
0
        public void Cannot_add_type_mapping_when_read_only()
        {
            var entityContainerMapping = new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace));
            var entitySetMapping       = new EntitySetMapping(new EntitySet(), entityContainerMapping);
            var entityTypeMapping      = new EntityTypeMapping(entitySetMapping);

            entitySetMapping.SetReadOnly();

            Assert.Equal(
                Strings.OperationOnReadOnlyItem,
                Assert.Throws <InvalidOperationException>(
                    () => entitySetMapping.AddTypeMapping(entityTypeMapping)).Message);
        }
        public static void GetIdentity_of_StorageSetMapping_returns_expected_value()
        {
            var entityType            = new EntityType("ET", "N", DataSpace.CSpace);
            var entitySet             = new EntitySet("ES", "S", "T", null, entityType);
            var associationType       = new AssociationType("AT", "N", false, DataSpace.CSpace);
            var associationSet        = new AssociationSet("AS", associationType);
            var entitySetMapping      = new EntitySetMapping(entitySet, null);
            var associationSetMapping = new AssociationSetMapping(associationSet, entitySet);

            Assert.Equal(entitySet.Identity,
                         BaseMetadataMappingVisitor.IdentityHelper.GetIdentity(entitySetMapping));
            Assert.Equal(associationSet.Identity,
                         BaseMetadataMappingVisitor.IdentityHelper.GetIdentity(associationSetMapping));
        }
Esempio n. 20
0
        public void Can_get_entity_type_mappings()
        {
            var entityContainerMapping = new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace));
            var entitySetMapping       = new EntitySetMapping(new EntitySet(), entityContainerMapping);

            Assert.Empty(entitySetMapping.EntityTypeMappings);

            var entityTypeMapping
                = new EntityTypeMapping(
                      new EntitySetMapping(new EntitySet(), entityContainerMapping));

            entitySetMapping.AddTypeMapping(entityTypeMapping);

            Assert.Same(entityTypeMapping, entitySetMapping.EntityTypeMappings.Single());
        }
        public void Can_get_entity_type_mappings()
        {
            var entityContainerMapping = new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace));
            var entitySetMapping = new EntitySetMapping(new EntitySet(), entityContainerMapping);

            Assert.Empty(entitySetMapping.EntityTypeMappings);

            var entityTypeMapping
                = new EntityTypeMapping(
                    new EntitySetMapping(new EntitySet(), entityContainerMapping));

            entitySetMapping.AddTypeMapping(entityTypeMapping);

            Assert.Same(entityTypeMapping, entitySetMapping.EntityTypeMappings.Single());
        }
        public void Can_get_entity_set_mappings()
        {
            var entityContainerMapping = new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace));

            Assert.Empty(entityContainerMapping.EntitySetMappings);
            Assert.Empty(entityContainerMapping.EntitySetMaps);

            var entitySetMapping
                = new EntitySetMapping(
                    new EntitySet("ES", null, null, null, new EntityType("E", "N", DataSpace.CSpace)), entityContainerMapping);

            entityContainerMapping.AddSetMapping(entitySetMapping);

            Assert.Same(entitySetMapping, entityContainerMapping.EntitySetMappings.Single());
            Assert.Same(entitySetMapping, entityContainerMapping.EntitySetMaps.Single());
        }
Esempio n. 23
0
        public void Can_get_entity_set_mappings()
        {
            var entityContainerMapping = new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace));

            Assert.Empty(entityContainerMapping.EntitySetMappings);
            Assert.Empty(entityContainerMapping.EntitySetMaps);

            var entitySetMapping
                = new EntitySetMapping(
                      new EntitySet("ES", null, null, null, new EntityType("E", "N", DataSpace.CSpace)), entityContainerMapping);

            entityContainerMapping.AddSetMapping(entitySetMapping);

            Assert.Same(entitySetMapping, entityContainerMapping.EntitySetMappings.Single());
            Assert.Same(entitySetMapping, entityContainerMapping.EntitySetMaps.Single());
        }
        public void WriteEntitySetMappingElement_should_write_modification_function_mappings()
        {
            var fixture = new Fixture();

            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var entitySet = new EntitySet("ES", "S", null, null, entityType);
            var entityContainer = new EntityContainer("EC", DataSpace.SSpace);

            entityContainer.AddEntitySetBase(entitySet);

            var storageEntitySetMapping
                = new EntitySetMapping(
                    entitySet,
                    new EntityContainerMapping(entityContainer));

            var storageModificationFunctionMapping
                = new ModificationFunctionMapping(
                    entitySet,
                    entityType,
                    new EdmFunction("F", "N", DataSpace.SSpace, new EdmFunctionPayload()),
                    Enumerable.Empty<ModificationFunctionParameterBinding>(),
                    null,
                    null);

            storageEntitySetMapping.AddModificationFunctionMapping(
                new EntityTypeModificationFunctionMapping(
                    entityType,
                    storageModificationFunctionMapping,
                    storageModificationFunctionMapping,
                    storageModificationFunctionMapping));

            fixture.Writer.WriteEntitySetMappingElement(storageEntitySetMapping);

            Assert.Equal(
                @"<EntitySetMapping Name=""ES"">
  <EntityTypeMapping TypeName="".E"">
    <ModificationFunctionMapping>
      <InsertFunction FunctionName=""N.F"" />
      <UpdateFunction FunctionName=""N.F"" />
      <DeleteFunction FunctionName=""N.F"" />
    </ModificationFunctionMapping>
  </EntityTypeMapping>
</EntitySetMapping>",
                fixture.ToString());
        }
Esempio n. 25
0
        public void SetReadOnly_is_called_on_child_mapping_items()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var entitySet  = new EntitySet("S", "N", null, null, entityType);
            var function   = new EdmFunction(
                "F", "N", DataSpace.CSpace,
                new EdmFunctionPayload
            {
                IsFunctionImport = true
            });

            var container = new EntityContainer("C", DataSpace.CSpace);

            container.AddEntitySetBase(entitySet);
            container.AddFunctionImport(function);

            var entitySetMapping =
                new EntitySetMapping(
                    entitySet,
                    new EntityContainerMapping(container));

            var functionMapping =
                new ModificationFunctionMapping(
                    entitySet,
                    entityType,
                    function,
                    Enumerable.Empty <ModificationFunctionParameterBinding>(),
                    null,
                    null);

            var entityFunctionMapping =
                new EntityTypeModificationFunctionMapping(entityType, functionMapping, null, null);

            entitySetMapping.AddModificationFunctionMapping(entityFunctionMapping);

            var entityTypeMapping = new EntityTypeMapping(entitySetMapping);

            entitySetMapping.AddTypeMapping(entityTypeMapping);

            Assert.False(entityTypeMapping.IsReadOnly);
            Assert.False(entityFunctionMapping.IsReadOnly);
            entitySetMapping.SetReadOnly();
            Assert.True(entityTypeMapping.IsReadOnly);
            Assert.True(entityFunctionMapping.IsReadOnly);
        }
Esempio n. 26
0
        public void Cannot_add_mapping_fragment_when_read_only()
        {
            var setMapping
                = new EntitySetMapping(
                    new EntitySet(),
                    new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace)));
            var typeMapping
                = new EntityTypeMapping(setMapping);

            typeMapping.SetReadOnly();

            var mappingFragment = new MappingFragment(new EntitySet(), typeMapping, false);

            Assert.Equal(
                Strings.OperationOnReadOnlyItem,
                Assert.Throws<InvalidOperationException>(
                    () => typeMapping.AddFragment(mappingFragment)).Message);
        }
Esempio n. 27
0
        public void Cannot_add_mapping_fragment_when_read_only()
        {
            var setMapping
                = new EntitySetMapping(
                      new EntitySet(),
                      new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace)));
            var typeMapping
                = new EntityTypeMapping(setMapping);

            typeMapping.SetReadOnly();

            var mappingFragment = new MappingFragment(new EntitySet(), typeMapping, false);

            Assert.Equal(
                Strings.OperationOnReadOnlyItem,
                Assert.Throws <InvalidOperationException>(
                    () => typeMapping.AddFragment(mappingFragment)).Message);
        }
        private static bool HasBaseWithIsTypeOf(EntitySetMapping entitySetMapping, EntityType entityType)
        {
            var baseType = entityType.BaseType;

            while (baseType != null)
            {
                if (entitySetMapping.EntityTypeMappings
                                    .Where(etm => etm.EntityType == baseType)
                                    .Any(etm => etm.IsHierarchyMapping))
                {
                    return true;
                }

                baseType = baseType.BaseType;
            }

            return false;
        }
Esempio n. 29
0
        public void Can_add_remove_type_mapping()
        {
            var storageSetMapping 
                = new EntitySetMapping(
                    new EntitySet(), 
                    new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace)));

            Assert.Empty(storageSetMapping.TypeMappings);

            var entityTypeMapping = new EntityTypeMapping(storageSetMapping);

            storageSetMapping.AddTypeMapping(entityTypeMapping);

            Assert.Same(entityTypeMapping, storageSetMapping.TypeMappings.Single());

            storageSetMapping.RemoveTypeMapping(entityTypeMapping);

            Assert.Empty(storageSetMapping.TypeMappings);
        }
Esempio n. 30
0
        public void Can_add_remove_type_mapping()
        {
            var storageSetMapping
                = new EntitySetMapping(
                      new EntitySet(),
                      new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace)));

            Assert.Empty(storageSetMapping.TypeMappings);

            var entityTypeMapping = new EntityTypeMapping(storageSetMapping);

            storageSetMapping.AddTypeMapping(entityTypeMapping);

            Assert.Same(entityTypeMapping, storageSetMapping.TypeMappings.Single());

            storageSetMapping.RemoveTypeMapping(entityTypeMapping);

            Assert.Empty(storageSetMapping.TypeMappings);
        }
Esempio n. 31
0
        public void Cannot_remove_modification_function_mapping_when_read_only()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var entitySet  = new EntitySet("S", "N", null, null, entityType);
            var function   = new EdmFunction(
                "F", "N", DataSpace.CSpace,
                new EdmFunctionPayload
            {
                IsFunctionImport = true
            });

            var container = new EntityContainer("C", DataSpace.CSpace);

            container.AddEntitySetBase(entitySet);
            container.AddFunctionImport(function);

            var entitySetMapping =
                new EntitySetMapping(
                    entitySet,
                    new EntityContainerMapping(container));

            var functionMapping =
                new ModificationFunctionMapping(
                    entitySet,
                    entityType,
                    function,
                    Enumerable.Empty <ModificationFunctionParameterBinding>(),
                    null,
                    null);

            var entityFunctionMappings =
                new EntityTypeModificationFunctionMapping(entityType, functionMapping, null, null);

            entitySetMapping.AddModificationFunctionMapping(entityFunctionMappings);
            entitySetMapping.SetReadOnly();

            Assert.Equal(
                Strings.OperationOnReadOnlyItem,
                Assert.Throws <InvalidOperationException>(
                    () => entitySetMapping.RemoveModificationFunctionMapping(entityFunctionMappings)).Message);
        }
Esempio n. 32
0
        public void Can_add_get_remove_modification_function_mappings()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var entitySet  = new EntitySet("S", "N", null, null, entityType);
            var function   = new EdmFunction(
                "F", "N", DataSpace.CSpace,
                new EdmFunctionPayload
            {
                IsFunctionImport = true
            });

            var container = new EntityContainer("C", DataSpace.CSpace);

            container.AddEntitySetBase(entitySet);
            container.AddFunctionImport(function);

            var entitySetMapping =
                new EntitySetMapping(
                    entitySet,
                    new EntityContainerMapping(container));

            var functionMapping =
                new ModificationFunctionMapping(
                    entitySet,
                    entityType,
                    function,
                    Enumerable.Empty <ModificationFunctionParameterBinding>(),
                    null,
                    null);

            var entityFunctionMappings =
                new EntityTypeModificationFunctionMapping(entityType, functionMapping, null, null);

            entitySetMapping.AddModificationFunctionMapping(entityFunctionMappings);

            Assert.Same(entityFunctionMappings, entitySetMapping.ModificationFunctionMappings.Single());

            entitySetMapping.RemoveModificationFunctionMapping(entityFunctionMappings);

            Assert.Empty(entitySetMapping.ModificationFunctionMappings);
        }
Esempio n. 33
0
        public void Can_add_remove_mapping_fragment()
        {
            var storageSetMapping
                = new EntitySetMapping(
                      new EntitySet(),
                      new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace)));

            var storageTypeMapping
                = new EntityTypeMapping(storageSetMapping);

            Assert.Empty(storageTypeMapping.MappingFragments);

            var mappingFragment = new MappingFragment(new EntitySet(), storageTypeMapping, false);

            storageTypeMapping.AddFragment(mappingFragment);

            Assert.Same(mappingFragment, storageTypeMapping.MappingFragments.Single());

            storageTypeMapping.RemoveFragment(mappingFragment);

            Assert.Empty(storageTypeMapping.MappingFragments);
        }
Esempio n. 34
0
        public void Can_add_remove_mapping_fragment()
        {
            var storageSetMapping
                = new EntitySetMapping(
                    new EntitySet(),
                    new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace)));

            var storageTypeMapping
                = new EntityTypeMapping(storageSetMapping);

            Assert.Empty(storageTypeMapping.MappingFragments);

            var mappingFragment = new MappingFragment(new EntitySet(), storageTypeMapping, false);

            storageTypeMapping.AddFragment(mappingFragment);

            Assert.Same(mappingFragment, storageTypeMapping.MappingFragments.Single());

            storageTypeMapping.RemoveFragment(mappingFragment);

            Assert.Empty(storageTypeMapping.MappingFragments);
        }
Esempio n. 35
0
        public void SetReadOnly_is_called_on_child_mapping_items()
        {
            var conceptualContainer = new EntityContainer("C", DataSpace.CSpace);
            var storeContainer      = new EntityContainer("S", DataSpace.CSpace);
            var containerMapping    = new EntityContainerMapping(conceptualContainer, storeContainer, null, false);

            var entitySet
                = new EntitySet(
                      "ES", "S", "T", "Q",
                      new EntityType("ET", "N", DataSpace.SSpace));
            var entitySetMapping = new EntitySetMapping(entitySet, containerMapping);
            var associationSetMapping
                = new AssociationSetMapping(
                      new AssociationSet(
                          "AS",
                          new AssociationType("AT", "N", false, DataSpace.CSpace)),
                      entitySet);
            var functionImportMapping
                = new FunctionImportMappingFake(
                      new EdmFunction("FI", "N", DataSpace.CSpace),
                      new EdmFunction("TF", "N", DataSpace.SSpace));

            containerMapping.AddSetMapping(entitySetMapping);
            containerMapping.AddSetMapping(associationSetMapping);
            containerMapping.AddFunctionImportMapping(functionImportMapping);

            Assert.False(containerMapping.IsReadOnly);
            Assert.False(entitySetMapping.IsReadOnly);
            Assert.False(associationSetMapping.IsReadOnly);
            Assert.False(functionImportMapping.IsReadOnly);

            containerMapping.SetReadOnly();

            Assert.True(containerMapping.IsReadOnly);
            Assert.True(entitySetMapping.IsReadOnly);
            Assert.True(associationSetMapping.IsReadOnly);
            Assert.True(functionImportMapping.IsReadOnly);
        }
 /// <summary>
 /// Creates an EntityTypeMapping instance.
 /// </summary>
 /// <param name="entitySetMapping">The EntitySetMapping that contains this EntityTypeMapping.</param>
 public EntityTypeMapping(EntitySetMapping entitySetMapping)
 {
     _entitySetMapping = entitySetMapping;
     _fragments = new List<MappingFragment>();
 }
        public void Cannot_remove_type_mapping_when_read_only()
        {
            var entityContainerMapping = new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace));
            var entitySetMapping = new EntitySetMapping(new EntitySet(), entityContainerMapping);
            var entityTypeMapping = new EntityTypeMapping(entitySetMapping);

            entitySetMapping.AddTypeMapping(entityTypeMapping);
            entitySetMapping.SetReadOnly();

            Assert.Equal(
                Strings.OperationOnReadOnlyItem,
                Assert.Throws<InvalidOperationException>(
                    () => entitySetMapping.RemoveTypeMapping(entityTypeMapping)).Message);
        }
        public void Cannot_remove_modification_function_mapping_when_read_only()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var entitySet = new EntitySet("S", "N", null, null, entityType);
            var function = new EdmFunction(
                "F", "N", DataSpace.CSpace,
                new EdmFunctionPayload
                {
                    IsFunctionImport = true
                });

            var container = new EntityContainer("C", DataSpace.CSpace);
            container.AddEntitySetBase(entitySet);
            container.AddFunctionImport(function);

            var entitySetMapping =
                new EntitySetMapping(
                    entitySet,
                    new EntityContainerMapping(container));

            var functionMapping =
                new ModificationFunctionMapping(
                    entitySet,
                    entityType,
                    function,
                    Enumerable.Empty<ModificationFunctionParameterBinding>(),
                    null,
                    null);

            var entityFunctionMappings =
                new EntityTypeModificationFunctionMapping(entityType, functionMapping, null, null);

            entitySetMapping.AddModificationFunctionMapping(entityFunctionMappings);
            entitySetMapping.SetReadOnly();

            Assert.Equal(
                Strings.OperationOnReadOnlyItem,
                Assert.Throws<InvalidOperationException>(
                    () => entitySetMapping.RemoveModificationFunctionMapping(entityFunctionMappings)).Message);
        }
Esempio n. 39
0
 /// <summary>
 /// Creates an EntityTypeMapping instance.
 /// </summary>
 /// <param name="entitySetMapping">The EntitySetMapping that contains this EntityTypeMapping.</param>
 public EntityTypeMapping(EntitySetMapping entitySetMapping)
 {
     _entitySetMapping = entitySetMapping;
     _fragments        = new List <MappingFragment>();
 }
        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);
            }
        }
            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 BuildEntityMapping_creates_valid_entity_mappings()
        {
            var storeEntityType =
                EntityType.Create(
                    "foo_S",
                    "bar_S",
                    DataSpace.SSpace,
                    new[] { "Id" },
                    new[] { CreateStoreProperty("Id", "int") },
                    null);

            var modelEntityType =
                EntityType.Create(
                    "foo_C",
                    "bar_C",
                    DataSpace.CSpace,
                    new[] { "C_Id" },
                    new[] { EdmProperty.CreatePrimitive("C_Id", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)) },
                    null);

            var mappingContext = new SimpleMappingContext(new EdmModel(DataSpace.SSpace), true);
            mappingContext.AddMapping(storeEntityType, modelEntityType);
            mappingContext.AddMapping(storeEntityType.Properties.Single(), modelEntityType.Properties.Single());

            var modelEntitySet = EntitySet.Create("ES", "Ns", null, null, modelEntityType, null);
            var modelEntityContainer = EntityContainer.Create("C", DataSpace.SSpace, new[] { modelEntitySet }, null, null);

            var storeEntitySet = EntitySet.Create("ES", "Ns.Store", null, null, storeEntityType, null);
            var storageEntitySetMapping =
                new EntitySetMapping(
                    modelEntitySet,
                    new EntityContainerMapping(modelEntityContainer, null, null, false, false));

            var typeMapping =
                DbDatabaseMappingBuilder
                    .BuildEntityTypeMapping(storageEntitySetMapping, mappingContext, storeEntitySet);

            Assert.Same(modelEntityType, typeMapping.EntityType);
            Assert.Equal(1, typeMapping.MappingFragments.Count);
            var mappingFragment = typeMapping.MappingFragments.Single();
            Assert.Equal(storeEntityType, mappingFragment.Table);
            Assert.Equal(1, mappingFragment.ColumnMappings.Count());
        }
        public void GenerateViews_generates_views_for_all_containers_that_contain_mappings()
        {
            var storageMappingItemCollection = 
                CreateStorageMappingItemCollection(new[] { Ssdl, SchoolSsdl }, new[] { Csdl, SchoolCsdl }, new[] { Msl});

            // Add second container mapping without type mappings.
            var conceptualEntityContainer = storageMappingItemCollection.EdmItemCollection.GetItems<EntityContainer>()[1];
            var storeEntityContainer = storageMappingItemCollection.StoreItemCollection.GetItems<EntityContainer>()[1];
            var storeEntitySet = storeEntityContainer.EntitySets.First();

            var containerMapping = new EntityContainerMapping(
                conceptualEntityContainer, storeEntityContainer, storageMappingItemCollection, false);

            var entitySetMapping = new EntitySetMapping(storeEntitySet, containerMapping);
            containerMapping.AddSetMapping(entitySetMapping);

            containerMapping.SetReadOnly();
            storageMappingItemCollection.AddInternal(containerMapping);

            var errors = new List<EdmSchemaError>();
            var views = storageMappingItemCollection.GenerateViews("AdventureWorksEntities3", "AdventureWorksModelStoreContainer", errors);

            Assert.Empty(errors);
            Assert.Equal(2, views.Count);

            errors = new List<EdmSchemaError>();
            views = storageMappingItemCollection.GenerateViews("SchoolContainer", "SchoolStoreContainer", errors);

            Assert.Empty(errors);
            Assert.Equal(0, views.Count);
        }
        public static EntitySetMapping AddEntitySetMapping(
            this DbDatabaseMapping databaseMapping, EntitySet entitySet)
        {
            DebugCheck.NotNull(databaseMapping);
            DebugCheck.NotNull(entitySet);

            var entitySetMapping = new EntitySetMapping(entitySet, null);

            databaseMapping
                .EntityContainerMappings
                .Single()
                .AddSetMapping(entitySetMapping);

            return entitySetMapping;
        }
 /// <summary>Removes an association set mapping.</summary>
 /// <param name="setMapping">The association set mapping to remove.</param>
 public void RemoveSetMapping(EntitySetMapping setMapping)
 {
     Check.NotNull <EntitySetMapping>(setMapping, nameof(setMapping));
     Util.ThrowIfReadOnly((MetadataItem)this);
     this.m_entitySetMappings.Remove(setMapping.Set.Name);
 }
        public void SetReadOnly_is_called_on_child_mapping_items()
        {
            var conceptualContainer = new EntityContainer("C", DataSpace.CSpace);
            var storeContainer = new EntityContainer("S", DataSpace.CSpace);
            var containerMapping = new EntityContainerMapping(conceptualContainer, storeContainer, null, false);

            var entitySet
                = new EntitySet(
                    "ES", "S", "T", "Q",
                    new EntityType("ET", "N", DataSpace.SSpace));
            var entitySetMapping = new EntitySetMapping(entitySet, containerMapping);
            var associationSetMapping
                = new AssociationSetMapping(
                    new AssociationSet(
                        "AS",
                        new AssociationType("AT", "N", false, DataSpace.CSpace)),
                    entitySet);
            var functionImporMapping 
                = new FunctionImportMappingFake(
                    new EdmFunction("FI", "N", DataSpace.CSpace),
                    new EdmFunction("TF", "N", DataSpace.SSpace));

            containerMapping.AddSetMapping(entitySetMapping);
            containerMapping.AddSetMapping(associationSetMapping);
            containerMapping.AddFunctionImportMapping(functionImporMapping);

            Assert.False(containerMapping.IsReadOnly);
            Assert.False(entitySetMapping.IsReadOnly);
            Assert.False(associationSetMapping.IsReadOnly);
            Assert.False(functionImporMapping.IsReadOnly);

            containerMapping.SetReadOnly();

            Assert.True(containerMapping.IsReadOnly);
            Assert.True(entitySetMapping.IsReadOnly);
            Assert.True(associationSetMapping.IsReadOnly);
            Assert.True(functionImporMapping.IsReadOnly);
        }
		private static MappingFragment FindStorageMappingFragmentInStorageMapping(EntitySetMapping storageMapping)
		{
			// Find the storage mapping fragment that the entity is mapped to
			var entityTypeMappings = storageMapping.EntityTypeMappings;
			var entityTypeMapping = entityTypeMappings.First();
			// using First() because Table-per-Hierarchy (TPH) produces multiple copies of the entity type mapping
			var fragments = entityTypeMapping.Fragments;
			if (fragments.Count() != 1)
			{
				throw new EnumGeneratorException(string.Format("{0} Fragments found.", fragments.Count()));
			}
			var fragment = fragments.Single();
			return fragment;
		}
        private static IEnumerable<ColumnMappingBuilder> GetColumnMappings(
            EntityType entityType, EntitySetMapping entitySetMapping)
        {
            DebugCheck.NotNull(entityType);
            DebugCheck.NotNull(entitySetMapping);

            return new[] { entityType }
                .Concat(GetParents(entityType))
                .SelectMany(
                    et => entitySetMapping
                              .TypeMappings
                              .Where(stm => stm.Types.Contains(et))
                              .SelectMany(stm => stm.MappingFragments)
                              .SelectMany(mf => mf.ColumnMappings));
        }
        public static void GetIdentity_of_StorageSetMapping_returns_expected_value()
        {
            var entityType = new EntityType("ET", "N", DataSpace.CSpace);
            var entitySet = new EntitySet("ES", "S", "T", null, entityType);
            var associationType = new AssociationType("AT", "N", false, DataSpace.CSpace);
            var associationSet = new AssociationSet("AS", associationType);
            var entitySetMapping = new EntitySetMapping(entitySet, null);
            var associationSetMapping = new AssociationSetMapping(associationSet, entitySet);

            Assert.Equal(entitySet.Identity, 
                BaseMetadataMappingVisitor.IdentityHelper.GetIdentity(entitySetMapping));
            Assert.Equal(associationSet.Identity, 
                BaseMetadataMappingVisitor.IdentityHelper.GetIdentity(associationSetMapping));
        }
        public void Can_add_get_remove_modification_function_mappings()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var entitySet = new EntitySet("S", "N", null, null, entityType);
            var function = new EdmFunction(
                "F", "N", DataSpace.CSpace,
                new EdmFunctionPayload
                    {
                        IsFunctionImport = true
                    });

            var container = new EntityContainer("C", DataSpace.CSpace);
            container.AddEntitySetBase(entitySet);
            container.AddFunctionImport(function);

            var entitySetMapping =
                new EntitySetMapping(
                    entitySet,
                    new EntityContainerMapping(container));

            var functionMapping =
                new ModificationFunctionMapping(
                    entitySet,
                    entityType,
                    function,
                    Enumerable.Empty<ModificationFunctionParameterBinding>(),
                    null,
                    null);

            var entityFunctionMappings =
                new EntityTypeModificationFunctionMapping(entityType, functionMapping, null, null);

            entitySetMapping.AddModificationFunctionMapping(entityFunctionMappings);

            Assert.Same(entityFunctionMappings, entitySetMapping.ModificationFunctionMappings.Single());

            entitySetMapping.RemoveModificationFunctionMapping(entityFunctionMappings);

            Assert.Empty(entitySetMapping.ModificationFunctionMappings);
        }
        public void SetReadOnly_is_called_on_child_mapping_items()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var entitySet = new EntitySet("S", "N", null, null, entityType);
            var function = new EdmFunction(
                "F", "N", DataSpace.CSpace,
                new EdmFunctionPayload
                {
                    IsFunctionImport = true
                });

            var container = new EntityContainer("C", DataSpace.CSpace);
            container.AddEntitySetBase(entitySet);
            container.AddFunctionImport(function);

            var entitySetMapping =
                new EntitySetMapping(
                    entitySet,
                    new EntityContainerMapping(container));

            var functionMapping =
                new ModificationFunctionMapping(
                    entitySet,
                    entityType,
                    function,
                    Enumerable.Empty<ModificationFunctionParameterBinding>(),
                    null,
                    null);

            var entityFunctionMapping =
                new EntityTypeModificationFunctionMapping(entityType, functionMapping, null, null);

            entitySetMapping.AddModificationFunctionMapping(entityFunctionMapping);

            var entityTypeMapping = new EntityTypeMapping(entitySetMapping);

            entitySetMapping.AddTypeMapping(entityTypeMapping);

            Assert.False(entityTypeMapping.IsReadOnly);
            Assert.False(entityFunctionMapping.IsReadOnly);
            entitySetMapping.SetReadOnly();
            Assert.True(entityTypeMapping.IsReadOnly);
            Assert.True(entityFunctionMapping.IsReadOnly);
        }
            BuildEntitySetMappings(EntityContainerMapping entityContainerMapping, SimpleMappingContext mappingContext)
        {
            Debug.Assert(entityContainerMapping != null, "entityContainerMapping != null");
            Debug.Assert(mappingContext != null, "mappingContext != null");

            foreach (var storeEntitySet in mappingContext.StoreEntitySets())
            {
                var entitySetMapping = new EntitySetMapping(mappingContext[storeEntitySet], entityContainerMapping);
                entitySetMapping.AddTypeMapping(BuildEntityTypeMapping(entitySetMapping, mappingContext, storeEntitySet));
                yield return entitySetMapping;
            }
        }