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);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
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 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);
        }
        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());
        }
Exemplo n.º 7
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);
        }
Exemplo n.º 8
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 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;
        }
        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);
        }
        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);
        }