예제 #1
0
        /// <summary>
        /// Removes a type mapping.
        /// </summary>
        /// <param name="typeMapping">The type mapping to remove.</param>
        public void RemoveTypeMapping(EntityTypeMapping typeMapping)
        {
            Check.NotNull(typeMapping, "typeMapping");
            ThrowIfReadOnly();

            _entityTypeMappings.Remove(typeMapping);
        }
예제 #2
0
        /// <summary>
        /// Adds a type mapping.
        /// </summary>
        /// <param name="typeMapping">The type mapping to add.</param>
        public void AddTypeMapping(EntityTypeMapping typeMapping)
        {
            Check.NotNull(typeMapping, "typeMapping");
            ThrowIfReadOnly();

            _entityTypeMappings.Add(typeMapping);
        }
예제 #3
0
        public void Can_create_hierarchy_mappings()
        {
            var entityTypeMapping
                = new EntityTypeMapping(
                      new EntitySetMapping(new EntitySet(), new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace))));

            Assert.False(entityTypeMapping.IsHierarchyMapping);

            var entityType = new EntityType("E", "N", DataSpace.CSpace);

            entityTypeMapping.AddIsOfType(entityType);

            Assert.True(entityTypeMapping.IsHierarchyMapping);

            entityTypeMapping.RemoveIsOfType(entityType);
            entityTypeMapping.AddType(entityType);

            Assert.False(entityTypeMapping.IsHierarchyMapping);

            var entityType2 = new EntityType("E2", "N", DataSpace.CSpace);

            entityTypeMapping.AddType(entityType2);

            Assert.True(entityTypeMapping.IsHierarchyMapping);
        }
예제 #4
0
        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());
        }
        private static bool RemapsInheritedProperties(
            DbDatabaseMapping databaseMapping, EntityTypeMapping entityTypeMapping)
        {
            var inheritedProperties = entityTypeMapping.EntityType.Properties
                                                       .Except(entityTypeMapping.EntityType.DeclaredProperties)
                                                       .Except(entityTypeMapping.EntityType.GetKeyProperties());

            foreach (var property in inheritedProperties)
            {
                var fragment = GetFragmentForPropertyMapping(entityTypeMapping, property);

                if (fragment != null)
                {
                    // find if this inherited property is mapped to another table by a base type
                    var baseType = (EntityType)entityTypeMapping.EntityType.BaseType;
                    while (baseType != null)
                    {
                        if (databaseMapping.GetEntityTypeMappings(baseType)
                                           .Select(baseTypeMapping => GetFragmentForPropertyMapping(baseTypeMapping, property))
                                           .Any(
                                               baseFragment => baseFragment != null
                                                               && baseFragment.Table != fragment.Table))
                        {
                            return true;
                        }
                        baseType = (EntityType)baseType.BaseType;
                    }
                }
            }
            return false;
        }
        public void Configure_should_update_table_name_when_base_type_is_null()
        {
            var entityMappingConfiguration
                = new EntityMappingConfiguration
                      {
                          TableName = new DatabaseName("Foo")
                      };

            var entityTypeMapping = new EntityTypeMapping(null);

            entityTypeMapping.AddType(new EntityType("E", "N", DataSpace.CSpace));

            var databaseMapping =
                new DbDatabaseMapping().Initialize(new EdmModel(DataSpace.CSpace), new EdmModel(DataSpace.SSpace));

            var table = databaseMapping.Database.AddTable("foo");
            var entitySet = databaseMapping.Database.GetEntitySet(table);

            entityTypeMapping.AddFragment(new MappingFragment(entitySet, entityTypeMapping, false));
            
            entityMappingConfiguration.Configure(
                databaseMapping, databaseMapping.Model.Container.EntitySets,
                ProviderRegistry.Sql2008_ProviderManifest, entityTypeMapping.EntityType,
                ref entityTypeMapping, false, 0, 1,
                new Dictionary<string, object>());

            Assert.Equal("Foo", table.GetTableName().Name);
        }
예제 #7
0
            public static string GetIdentity(EntityTypeMapping mapping)
            {
                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(",", types.Concat(isOfTypes)));
            }
예제 #8
0
        public void Can_get_entity_set_mapping()
        {
            var entitySetMapping
                = new EntitySetMapping(
                      new EntitySet(),
                      new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace)));
            var entityTypeMapping = new EntityTypeMapping(entitySetMapping);

            Assert.Same(entitySetMapping, entityTypeMapping.EntitySetMapping);
        }
예제 #9
0
            public static string GetIdentity(TypeMapping mapping)
            {
                EntityTypeMapping mapping1 = mapping as EntityTypeMapping;

                if (mapping1 != null)
                {
                    return(BaseMetadataMappingVisitor.IdentityHelper.GetIdentity(mapping1));
                }
                return(BaseMetadataMappingVisitor.IdentityHelper.GetIdentity((AssociationTypeMapping)mapping));
            }
예제 #10
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);
        }
예제 #11
0
        public void AddType_throws_for_null_type()
        {
            var entityTypeMapping
                = new EntityTypeMapping(
                      new EntitySetMapping(
                          new EntitySet(),
                          new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace))));

            Assert.Equal(
                "type",
                Assert.Throws <ArgumentNullException>(() => entityTypeMapping.AddType(null)).ParamName);
        }
        public static void GetIdentity_of_StorageMappingFragment_returns_expected_value()
        {
            var entityType        = new EntityType("ET", "N", DataSpace.CSpace);
            var entitySet         = new EntitySet("ES", "S", "T", null, entityType);
            var entityTypeMapping = new EntityTypeMapping(null);

            entityTypeMapping.AddType(entityType);
            var mappingFragment = new MappingFragment(entitySet, entityTypeMapping, false);

            Assert.Equal(entitySet.Identity,
                         BaseMetadataMappingVisitor.IdentityHelper.GetIdentity(mappingFragment));
        }
        public void AddType_throws_for_null_type()
        {
            var entityTypeMapping
                = new EntityTypeMapping(
                    new EntitySetMapping(
                        new EntitySet(), 
                        new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace))));

            Assert.Equal(
                "type",
                Assert.Throws<ArgumentNullException>(() => entityTypeMapping.AddType(null)).ParamName);
        }
예제 #14
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);
        }
        public static EntityTypeMapping Clone(this EntityTypeMapping entityTypeMapping)
        {
            DebugCheck.NotNull(entityTypeMapping);

            var clone = new EntityTypeMapping(null);

            clone.AddType(entityTypeMapping.EntityType);

            entityTypeMapping.Annotations.Copy(clone.Annotations);

            return clone;
        }
        public void Can_not_create_mapping_fragment_with_null_entity_set()
        {
            var entityTypeMapping =
                new EntityTypeMapping(
                    new EntitySetMapping(
                        new EntitySet(),
                        new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace))));

            Assert.Equal(
                "storeEntitySet",
                Assert.Throws <ArgumentNullException>(
                    () => new MappingFragment(null, entityTypeMapping, false)).ParamName);
        }
예제 #17
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);
        }
예제 #18
0
        public void Can_not_create_mapping_fragment_with_null_entity_set()
        {
            var entityTypeMapping = 
                new EntityTypeMapping(
                new EntitySetMapping(
                    new EntitySet(),
                    new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace))));

            Assert.Equal(
                "storeEntitySet",
                Assert.Throws<ArgumentNullException>(
                    () => new MappingFragment(null, entityTypeMapping, false)).ParamName);
        }
        public void Can_get_entity_type()
        {
            var entityTypeMapping 
                = new EntityTypeMapping(
                    new EntitySetMapping(new EntitySet(), new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace))));

            Assert.Null(entityTypeMapping.EntityType);

            var entityType = new EntityType("E", "N", DataSpace.CSpace);

            entityTypeMapping.AddType(entityType);

            Assert.Same(entityType, entityTypeMapping.EntityType);
        }
예제 #20
0
        public void Can_get_entity_type()
        {
            var entityTypeMapping
                = new EntityTypeMapping(
                      new EntitySetMapping(new EntitySet(), new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace))));

            Assert.Null(entityTypeMapping.EntityType);

            var entityType = new EntityType("E", "N", DataSpace.CSpace);

            entityTypeMapping.AddType(entityType);

            Assert.Same(entityType, entityTypeMapping.EntityType);
        }
예제 #21
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());
        }
예제 #23
0
        public void SetReadOnly_is_called_on_child_mapping_items()
        {
            var entitySet = new EntitySet();
            var entityTypeMapping
                = new EntityTypeMapping(
                      new EntitySetMapping(
                          entitySet,
                          new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace))));
            var fragment = new MappingFragment(entitySet, entityTypeMapping, false);

            entityTypeMapping.AddFragment(fragment);

            Assert.False(fragment.IsReadOnly);
            entityTypeMapping.SetReadOnly();
            Assert.True(fragment.IsReadOnly);
        }
        public static void GetIdentity_of_StorageEntityTypeMapping_returns_expected_value()
        {
            var entityType1 = new EntityType("ET1", "N", DataSpace.CSpace);
            var entityType2 = new EntityType("ET2", "N", DataSpace.CSpace);
            var entityType3 = new EntityType("ET3", "N", DataSpace.CSpace);
            var entityType4 = new EntityType("ET4", "N", DataSpace.CSpace);
            var mapping     = new EntityTypeMapping(null);

            mapping.AddType(entityType2);
            mapping.AddType(entityType1);
            mapping.AddIsOfType(entityType4);
            mapping.AddIsOfType(entityType3);

            Assert.Equal("N.ET1,N.ET2,N.ET3,N.ET4",
                         BaseMetadataMappingVisitor.IdentityHelper.GetIdentity((TypeMapping)mapping));
        }
예제 #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);
        }
예제 #26
0
        public void Added_isOfType_returned_in_isOfType_collection()
        {
            var entityTypeMapping
                = new EntityTypeMapping(
                      new EntitySetMapping(
                          new EntitySet(),
                          new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace))));

            Assert.Empty(entityTypeMapping.Types);
            Assert.Empty(entityTypeMapping.IsOfTypes);

            var entityType = new EntityType("E", "N", DataSpace.CSpace);

            entityTypeMapping.AddIsOfType(entityType);

            Assert.Same(entityType, entityTypeMapping.IsOfTypes.Single());
            Assert.Empty(entityTypeMapping.Types);
        }
예제 #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);
        }
        public void Added_type_returned_in_type_collection()
        {
            var entityTypeMapping
                = new EntityTypeMapping(
                    new EntitySetMapping(
                        new EntitySet(),
                        new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace))));

            Assert.Empty(entityTypeMapping.Types);
            Assert.Empty(entityTypeMapping.IsOfTypes);

            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            
            entityTypeMapping.AddType(entityType);

            Assert.Same(entityType, entityTypeMapping.Types.Single());
            Assert.Empty(entityTypeMapping.IsOfTypes);
        }
예제 #29
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);
        }
예제 #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);
        }
예제 #31
0
        public void Cannot_remove_isOfType_when_read_only()
        {
            var entityTypeMapping
                = new EntityTypeMapping(
                      new EntitySetMapping(
                          new EntitySet(),
                          new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace))));

            var entityType = new EntityType("E", "N", DataSpace.CSpace);

            entityTypeMapping.AddIsOfType(entityType);

            entityTypeMapping.SetReadOnly();

            Assert.Equal(
                Strings.OperationOnReadOnlyItem,
                Assert.Throws <InvalidOperationException>(
                    () => entityTypeMapping.RemoveIsOfType(entityType)).Message);
        }
예제 #32
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);
        }
예제 #33
0
        public void Can_add_remove_isOfType()
        {
            var entityTypeMapping
                = new EntityTypeMapping(
                      new EntitySetMapping(
                          new EntitySet(),
                          new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace))));

            Assert.Empty(entityTypeMapping.IsOfTypes);

            var entityType = new EntityType("E", "N", DataSpace.CSpace);

            entityTypeMapping.AddIsOfType(entityType);

            Assert.Same(entityType, entityTypeMapping.IsOfTypes.Single());

            entityTypeMapping.RemoveIsOfType(entityType);

            Assert.Empty(entityTypeMapping.IsOfTypes);
        }
        public void GetComplexPropertyMappings_should_return_all_complex_property_mappings_for_type()
        {
            var databaseMapping = new DbDatabaseMapping()
                .Initialize(new EdmModel(DataSpace.CSpace), new EdmModel(DataSpace.SSpace));
            var entitySet = new EntitySet
                                {
                                    Name = "ES"
                                };
            var entitySetMapping = databaseMapping.AddEntitySetMapping(entitySet);
            var entityTypeMapping = new EntityTypeMapping(null);
            entitySetMapping.AddTypeMapping(entityTypeMapping);
            var entityTypeMappingFragment = new MappingFragment(entitySet, entityTypeMapping, false);
            entityTypeMapping.AddFragment(entityTypeMappingFragment);
            var complexType = new ComplexType("C");
            var propertyMapping1
                = new ColumnMappingBuilder(
                    new EdmProperty("C", TypeUsage.Create(new PrimitiveType() { DataSpace = DataSpace.SSpace })),
                    new[]
                        {
                            EdmProperty.CreateComplex("P1", complexType),
                            EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String))
                        });
            var type = typeof(object);

            complexType.GetMetadataProperties().SetClrType(type);

            entityTypeMappingFragment.AddColumnMapping(propertyMapping1);

            var propertyMapping2
                = new ColumnMappingBuilder(
                    new EdmProperty("C", TypeUsage.Create(new PrimitiveType() { DataSpace = DataSpace.SSpace })),
                    new List<EdmProperty>
                        {
                            EdmProperty.CreateComplex("P3", complexType),
                            EdmProperty.CreatePrimitive(
                                "P2", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)),
                        });
            entityTypeMappingFragment.AddColumnMapping(propertyMapping2);

            Assert.Equal(2, databaseMapping.GetComplexPropertyMappings(typeof(object)).Count());
        }
        public void Generate(EntityType entityType, DbDatabaseMapping databaseMapping)
        {
            DebugCheck.NotNull(entityType);
            DebugCheck.NotNull(databaseMapping);

            var entitySet = databaseMapping.Model.GetEntitySet(entityType);

            var entitySetMapping
                = databaseMapping.GetEntitySetMapping(entitySet)
                  ?? databaseMapping.AddEntitySetMapping(entitySet);

            var entityTypeMapping =
                entitySetMapping.EntityTypeMappings.FirstOrDefault(
                    m => m.EntityTypes.Contains(entitySet.ElementType))
                ?? entitySetMapping.EntityTypeMappings.FirstOrDefault();

            var table
                = entityTypeMapping != null
                      ? entityTypeMapping.MappingFragments.First().Table
                      : databaseMapping.Database.AddTable(entityType.GetRootType().Name);

            entityTypeMapping = new EntityTypeMapping(null);

            var entityTypeMappingFragment
                = new MappingFragment(databaseMapping.Database.GetEntitySet(table), entityTypeMapping, false);

            entityTypeMapping.AddType(entityType);
            entityTypeMapping.AddFragment(entityTypeMappingFragment);
            entityTypeMapping.SetClrType(entityType.GetClrType());

            entitySetMapping.AddTypeMapping(entityTypeMapping);

            new PropertyMappingGenerator(_providerManifest)
                .Generate(
                    entityType,
                    entityType.Properties,
                    entitySetMapping,
                    entityTypeMappingFragment,
                    new List<EdmProperty>(),
                    false);
        }
예제 #36
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);
        }
예제 #37
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);
        }
        public void GetPropertyMapping_should_return_mapping_with_path()
        {
            var entityTypeMapping = new EntityTypeMapping(null);
            var propertyFoo = EdmProperty.CreateComplex("Foo", new ComplexType("CT"));
            var propertyBar = EdmProperty.CreatePrimitive("Bar", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            var entityPropertyMapping
                = new ColumnMappingBuilder(
                    new EdmProperty("C", TypeUsage.Create(new PrimitiveType() { DataSpace = DataSpace.SSpace })),
                    new[]
                        {
                            propertyFoo,
                            propertyBar,
                        });

            var entityTypeMappingFragment
                = new MappingFragment(new EntitySet(), entityTypeMapping, false);

            entityTypeMappingFragment.AddColumnMapping(entityPropertyMapping);
            entityTypeMapping.AddFragment(entityTypeMappingFragment);

            Assert.Same(entityPropertyMapping, entityTypeMapping.GetPropertyMapping(propertyFoo, propertyBar));
        }
        public void Can_create_hierarchy_mappings()
        {
            var entityTypeMapping
                = new EntityTypeMapping(
                    new EntitySetMapping(new EntitySet(), new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace))));

            Assert.False(entityTypeMapping.IsHierarchyMapping);

            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            
            entityTypeMapping.AddIsOfType(entityType);

            Assert.True(entityTypeMapping.IsHierarchyMapping);

            entityTypeMapping.RemoveIsOfType(entityType);
            entityTypeMapping.AddType(entityType);

            Assert.False(entityTypeMapping.IsHierarchyMapping);

            var entityType2 = new EntityType("E2", "N", DataSpace.CSpace);
            entityTypeMapping.AddType(entityType2);

            Assert.True(entityTypeMapping.IsHierarchyMapping);
        }
        public void SetReadOnly_is_called_on_child_mapping_items()
        {
            var entitySet = new EntitySet();
            var entityTypeMapping
                = new EntityTypeMapping(
                    new EntitySetMapping(
                        entitySet,
                        new EntityContainerMapping(new EntityContainer("C", DataSpace.CSpace))));
            var fragment = new MappingFragment(entitySet, entityTypeMapping, false);
            entityTypeMapping.AddFragment(fragment);

            Assert.False(fragment.IsReadOnly);
            entityTypeMapping.SetReadOnly();
            Assert.True(fragment.IsReadOnly);
        }
        public void Generate_should_exclude_sgp_properties_from_corresponding_function_mappings()
        {
            var functionMappingGenerator
                = new ModificationFunctionMappingGenerator(ProviderRegistry.Sql2008_ProviderManifest);

            var databaseMapping
                = new DbDatabaseMapping()
                    .Initialize(new EdmModel(DataSpace.CSpace), new EdmModel(DataSpace.SSpace));

            var entityType = new EntityType("E", "N", DataSpace.CSpace);

            var intProperty = EdmProperty.CreatePrimitive("Id", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32));
            intProperty.SetStoreGeneratedPattern(StoreGeneratedPattern.Identity);
            entityType.AddKeyMember(intProperty);

            var stringProperty = EdmProperty.CreatePrimitive("Name", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));
            stringProperty.SetStoreGeneratedPattern(StoreGeneratedPattern.Computed);
            entityType.AddMember(stringProperty);

            var entitySetMapping
                = databaseMapping.AddEntitySetMapping(
                    databaseMapping.Model.AddEntitySet("ES", entityType));

            var storageEntityTypeMapping
                = new EntityTypeMapping(
                    new EntitySetMapping(new EntitySet(), databaseMapping.EntityContainerMappings.Single()));

            storageEntityTypeMapping.AddType(entityType);

            var storageMappingFragment = new MappingFragment(new EntitySet(), storageEntityTypeMapping, false);

            storageMappingFragment.AddColumnMapping(
                new ColumnMappingBuilder(new EdmProperty("C0", TypeUsage.Create(new PrimitiveType() { DataSpace = DataSpace.SSpace })), new[] { intProperty }));

            storageMappingFragment.AddColumnMapping(
                new ColumnMappingBuilder(new EdmProperty("C1", TypeUsage.Create(new PrimitiveType() { DataSpace = DataSpace.SSpace })), new[] { stringProperty }));

            storageEntityTypeMapping.AddFragment(storageMappingFragment);

            entitySetMapping.AddTypeMapping(storageEntityTypeMapping);

            functionMappingGenerator.Generate(entityType, databaseMapping);

            var modificationFunctionMapping
                = entitySetMapping.ModificationFunctionMappings.Single();

            Assert.NotNull(modificationFunctionMapping);

            var functionMapping = modificationFunctionMapping.InsertFunctionMapping;

            Assert.NotNull(functionMapping);
            Assert.Equal(0, functionMapping.ParameterBindings.Count);
            Assert.Equal(2, functionMapping.ResultBindings.Count);

            var function = functionMapping.Function;

            Assert.NotNull(function);
            Assert.Equal("E_Insert", function.Name);
            Assert.Equal(0, function.Parameters.Count);

            functionMapping = modificationFunctionMapping.UpdateFunctionMapping;

            Assert.NotNull(functionMapping);
            Assert.Equal(1, functionMapping.ParameterBindings.Count);
            Assert.Equal(1, functionMapping.ResultBindings.Count);

            function = functionMapping.Function;

            Assert.NotNull(function);
            Assert.Equal("E_Update", function.Name);
            Assert.Equal(1, function.Parameters.Count);

            functionMapping = modificationFunctionMapping.DeleteFunctionMapping;

            Assert.NotNull(functionMapping);
            Assert.Equal(1, functionMapping.ParameterBindings.Count);
            Assert.Null(functionMapping.ResultBindings);

            function = modificationFunctionMapping.DeleteFunctionMapping.Function;

            Assert.NotNull(function);
            Assert.Equal("E_Delete", function.Name);
            Assert.Equal(1, function.Parameters.Count);
        }
        private static MappingFragment FindConditionTypeMappingFragment(
            EntitySet tableSet, MappingFragment propertiesTypeMappingFragment,
            EntityTypeMapping conditionTypeMapping)
        {
            var table = tableSet.ElementType;

            var conditionTypeMappingFragment
                = conditionTypeMapping.MappingFragments
                    .SingleOrDefault(x => x.Table == table);

            if (conditionTypeMappingFragment == null)
            {
                conditionTypeMappingFragment
                    = EntityMappingOperations
                        .CreateTypeMappingFragment(conditionTypeMapping, propertiesTypeMappingFragment, tableSet);

                conditionTypeMappingFragment.SetIsConditionOnlyFragment(true);

                if (propertiesTypeMappingFragment.GetDefaultDiscriminator() != null)
                {
                    conditionTypeMappingFragment.SetDefaultDiscriminator(
                        propertiesTypeMappingFragment.GetDefaultDiscriminator());
                    propertiesTypeMappingFragment.RemoveDefaultDiscriminatorAnnotation();
                }
            }
            return conditionTypeMappingFragment;
        }
        private EntityTypeMapping FindConditionTypeMapping(
            EntityType entityType, bool requiresSplit, EntityTypeMapping propertiesTypeMapping)
        {
            var conditionTypeMapping = propertiesTypeMapping;

            if (requiresSplit)
            {
                if (!entityType.Abstract)
                {
                    conditionTypeMapping = propertiesTypeMapping.Clone();
                    conditionTypeMapping.RemoveIsOfType(conditionTypeMapping.EntityType);

                    var parentEntitySetMapping =
                        _databaseMapping.GetEntitySetMappings().Single(
                            esm => esm.EntityTypeMappings.Contains(propertiesTypeMapping));

                    parentEntitySetMapping.AddTypeMapping(conditionTypeMapping);
                }

                propertiesTypeMapping.MappingFragments.Each(tmf => tmf.ClearConditions());
            }
            return conditionTypeMapping;
        }
        // <summary>
        // Determines if the table and entity type need mapping, and if not, removes the existing entity type mapping
        // </summary>
        private bool FindPropertyEntityTypeMapping(
            TableMapping tableMapping,
            EntitySet entitySet,
            EntityType entityType,
            bool requiresIsTypeOf,
            out EntityTypeMapping entityTypeMapping,
            out MappingFragment fragment)
        {
            entityTypeMapping = null;
            fragment = null;
            var mapping = (from etm in _databaseMapping.GetEntityTypeMappings(entityType)
                           from tmf in etm.MappingFragments
                           where tmf.Table == tableMapping.Table
                           select new
                               {
                                   TypeMapping = etm,
                                   Fragment = tmf
                               }).SingleOrDefault();

            if (mapping != null)
            {
                entityTypeMapping = mapping.TypeMapping;
                fragment = mapping.Fragment;
                if (!requiresIsTypeOf
                    && entityType.Abstract)
                {
                    RemoveFragment(entitySet, mapping.TypeMapping, mapping.Fragment);
                    return false;
                }
                return true;
            }
            else
            {
                return false;
            }
        }
예제 #45
0
        private void WriteEntityTypeMappingElement(EntityTypeMapping entityTypeMapping)
        {
            DebugCheck.NotNull(entityTypeMapping);

            _xmlWriter.WriteStartElement(MslConstructs.EntityTypeMappingElement);
            _xmlWriter.WriteAttributeString(
                MslConstructs.EntityTypeMappingTypeNameAttribute,
                GetEntityTypeName(
                    _entityTypeNamespace + "." + entityTypeMapping.EntityType.Name, entityTypeMapping.IsHierarchyMapping));

            foreach (var mappingFragment in entityTypeMapping.MappingFragments)
            {
                WriteMappingFragmentElement(mappingFragment);
            }

            _xmlWriter.WriteEndElement();
        }
 private static MappingFragment GetFragmentForPropertyMapping(
     EntityTypeMapping entityTypeMapping, EdmProperty property)
 {
     return entityTypeMapping.MappingFragments
                             .SingleOrDefault(tmf => tmf.ColumnMappings.Any(pm => pm.PropertyPath.Last() == property));
 }
        internal void ConfigureTablesAndConditions(
            EntityTypeMapping entityTypeMapping,
            DbDatabaseMapping databaseMapping,
            ICollection<EntitySet> entitySets,
            DbProviderManifest providerManifest)
        {
            DebugCheck.NotNull(databaseMapping);
            DebugCheck.NotNull(providerManifest);

            var entityType
                = (entityTypeMapping != null)
                      ? entityTypeMapping.EntityType
                      : databaseMapping.Model.GetEntityType(ClrType);

            if (_entityMappingConfigurations.Any())
            {
                for (var i = 0; i < _entityMappingConfigurations.Count; i++)
                {
                    _entityMappingConfigurations[i]
                        .Configure(
                            databaseMapping,
                            entitySets,
                            providerManifest,
                            entityType,
                            ref entityTypeMapping,
                            IsMappingAnyInheritedProperty(entityType),
                            i,
                            _entityMappingConfigurations.Count,
                            _annotations);
                }
            }
            else
            {
                ConfigureUnconfiguredType(databaseMapping, entitySets, providerManifest, entityType, _annotations);
            }
        }
예제 #48
0
 public static string GetIdentity(EntityTypeMapping mapping)
 {
     return(string.Join(",", mapping.Types.Select <EntityTypeBase, string>((Func <EntityTypeBase, string>)(it => it.Identity)).OrderBy <string, string>((Func <string, string>)(it => it), (IComparer <string>)StringComparer.Ordinal).Concat <string>((IEnumerable <string>)mapping.IsOfTypes.Select <EntityTypeBase, string>((Func <EntityTypeBase, string>)(it => it.Identity)).OrderBy <string, string>((Func <string, string>)(it => it), (IComparer <string>)StringComparer.Ordinal))));
 }
 /// <summary>Removes a type mapping.</summary>
 /// <param name="typeMapping">The type mapping to remove.</param>
 public void RemoveTypeMapping(EntityTypeMapping typeMapping)
 {
     Check.NotNull <EntityTypeMapping>(typeMapping, nameof(typeMapping));
     this.ThrowIfReadOnly();
     this._entityTypeMappings.Remove(typeMapping);
 }
 private Type GetClrTypeFromTypeMapping(MetadataWorkspace metadata, ObjectItemCollection objectItemCollection, EntityTypeMapping mapping)
 {
     return GetClrType(metadata, objectItemCollection, mapping.EntityType ?? mapping.IsOfEntityTypes.First());
 }
        // <summary>
        // Finds interesting entity properties - primary keys (if requested), properties (including complex properties and nested properties)
        // with concurrency mode set to fixed and C-Side condition members and adds them to the
        // <paramref
        //     name="interestingMembers" />
        // .
        // </summary>
        // <param name="entityTypeMapping"> Entity type mapping. Must not be null. </param>
        // <param name="interestingMembersKind"> Scenario the members should be returned for. </param>
        // <param name="interestingMembers"> The list the interesting members (if any) will be added to. Must not be null. </param>
        private static void FindInterestingEntityMappingMembers(
            EntityTypeMapping entityTypeMapping, InterestingMembersKind interestingMembersKind, List<EdmMember> interestingMembers)
        {
            DebugCheck.NotNull(entityTypeMapping);
            DebugCheck.NotNull(interestingMembers);

            foreach (var propertyMapping in entityTypeMapping.MappingFragments.SelectMany(mf => mf.AllProperties))
            {
                var scalarPropMapping = propertyMapping as ScalarPropertyMapping;
                var complexPropMapping = propertyMapping as ComplexPropertyMapping;
                var conditionMapping = propertyMapping as ConditionPropertyMapping;

                Debug.Assert(!(propertyMapping is EndPropertyMapping), "association mapping properties should be handled elsewhere.");

                Debug.Assert(
                    scalarPropMapping != null ||
                    complexPropMapping != null ||
                    conditionMapping != null, "Unimplemented property mapping");

                //scalar property
                if (scalarPropMapping != null
                    && scalarPropMapping.Property != null)
                {
                    // (0) if a member is part of the key it is interesting
                    if (MetadataHelper.IsPartOfEntityTypeKey(scalarPropMapping.Property))
                    {
                        // For backwards compatibility we do return primary keys from the obsolete MetadataWorkspace.GetRequiredOriginalValueMembers() method
                        if (interestingMembersKind == InterestingMembersKind.RequiredOriginalValueMembers)
                        {
                            interestingMembers.Add(scalarPropMapping.Property);
                        }
                    }
                    //(3) if a scalar property has Fixed concurrency mode then it is "interesting"
                    else if (MetadataHelper.GetConcurrencyMode(scalarPropMapping.Property)
                             == ConcurrencyMode.Fixed)
                    {
                        interestingMembers.Add(scalarPropMapping.Property);
                    }
                }
                else if (complexPropMapping != null)
                {
                    // (7) All complex members - partial update scenarios only
                    // (3.1) The complex property or its one of its children has fixed concurrency mode
                    if (interestingMembersKind == InterestingMembersKind.PartialUpdate
                        ||
                        MetadataHelper.GetConcurrencyMode(complexPropMapping.Property) == ConcurrencyMode.Fixed
                        || HasFixedConcurrencyModeInAnyChildProperty(complexPropMapping))
                    {
                        interestingMembers.Add(complexPropMapping.Property);
                    }
                }
                else if (conditionMapping != null)
                {
                    //(1) C-Side condition members are 'interesting'
                    if (conditionMapping.Property != null)
                    {
                        interestingMembers.Add(conditionMapping.Property);
                    }
                }
            }
        }
        private void RemoveFragment(
            EntitySet entitySet, EntityTypeMapping entityTypeMapping, MappingFragment fragment)
        {
            // Make the default discriminator nullable if this type isn't using it but there is a base type
            var defaultDiscriminator = fragment.GetDefaultDiscriminator();

            if (defaultDiscriminator != null
                && entityTypeMapping.EntityType.BaseType != null
                && !entityTypeMapping.EntityType.Abstract)
            {
                var columnMapping =
                    _tableMappings[fragment.Table].ColumnMappings.SingleOrDefault(
                        cm => cm.Column == defaultDiscriminator);

                if (columnMapping != null)
                {
                    var propertyMapping = columnMapping.PropertyMappings.SingleOrDefault(
                        pm => pm.EntityType == entityTypeMapping.EntityType);
                    if (propertyMapping != null)
                    {
                        columnMapping.PropertyMappings.Remove(propertyMapping);
                    }
                }

                defaultDiscriminator.Nullable = true;
            }

            // The default TPH mapping may result in columns being created that are no longer required
            // when an abstract type mapping to the table is removed, for example in TPC cases. We need
            // to remove these columns.
            if (entityTypeMapping.EntityType.Abstract)
            {
                foreach (var columnMapping in _tableMappings[fragment.Table].ColumnMappings.Where(
                    cm => cm.PropertyMappings.All(pm => pm.EntityType == entityTypeMapping.EntityType)))
                {
                    fragment.Table.RemoveMember(columnMapping.Column);
                }
            }

            entityTypeMapping.RemoveFragment(fragment);
            
            if (!entityTypeMapping.MappingFragments.Any())
            {
                _databaseMapping.GetEntitySetMapping(entitySet).RemoveTypeMapping(entityTypeMapping);
            }
        }
        public static MappingFragment CreateTypeMappingFragment(
            EntityTypeMapping entityTypeMapping, MappingFragment templateFragment, EntitySet tableSet)
        {
            var fragment = new MappingFragment(tableSet, entityTypeMapping, false);

            entityTypeMapping.AddFragment(fragment);

            // Move all PK mappings to the extra fragment
            foreach (
                var pkPropertyMapping in templateFragment.ColumnMappings.Where(pm => pm.ColumnProperty.IsPrimaryKeyColumn))
            {
                CopyPropertyMappingToFragment(
                    pkPropertyMapping, fragment, TablePrimitiveOperations.GetNameMatcher(pkPropertyMapping.ColumnProperty.Name),
                    useExisting: true);

            }
            return fragment;
        }
            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;
        }