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, ProviderRegistry.Sql2008_ProviderManifest, entityTypeMapping.EntityType,
                ref entityTypeMapping, false, 0, 1,
                new Dictionary <string, object>());

            Assert.Equal("Foo", table.GetTableName().Name);
        }
        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));
        }
Пример #3
0
        public static MappingFragment CreateTypeMappingFragment(
            EntityTypeMapping entityTypeMapping,
            MappingFragment templateFragment,
            EntitySet tableSet)
        {
            MappingFragment fragment = new MappingFragment(tableSet, (TypeMapping)entityTypeMapping, false);

            entityTypeMapping.AddFragment(fragment);
            foreach (ColumnMappingBuilder propertyMappingBuilder in templateFragment.ColumnMappings.Where <ColumnMappingBuilder>((Func <ColumnMappingBuilder, bool>)(pm => pm.ColumnProperty.IsPrimaryKeyColumn)))
            {
                EntityMappingOperations.CopyPropertyMappingToFragment(propertyMappingBuilder, fragment, TablePrimitiveOperations.GetNameMatcher(propertyMappingBuilder.ColumnProperty.Name), true);
            }
            return(fragment);
        }
        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());
        }
Пример #5
0
        public void Generate(EntityType entityType, DbDatabaseMapping databaseMapping)
        {
            EntitySet         entitySet          = databaseMapping.Model.GetEntitySet(entityType);
            EntitySetMapping  entitySetMapping   = databaseMapping.GetEntitySetMapping(entitySet) ?? databaseMapping.AddEntitySetMapping(entitySet);
            EntityTypeMapping entityTypeMapping1 = entitySetMapping.EntityTypeMappings.FirstOrDefault <EntityTypeMapping>((Func <EntityTypeMapping, bool>)(m => m.EntityTypes.Contains((EntityTypeBase)entitySet.ElementType))) ?? entitySetMapping.EntityTypeMappings.FirstOrDefault <EntityTypeMapping>();
            EntityType        entityType1        = entityTypeMapping1 != null?entityTypeMapping1.MappingFragments.First <MappingFragment>().Table : databaseMapping.Database.AddTable(entityType.GetRootType().Name);

            EntityTypeMapping entityTypeMapping2 = new EntityTypeMapping((EntitySetMapping)null);
            MappingFragment   mappingFragment    = new MappingFragment(databaseMapping.Database.GetEntitySet(entityType1), (TypeMapping)entityTypeMapping2, false);

            entityTypeMapping2.AddType(entityType);
            entityTypeMapping2.AddFragment(mappingFragment);
            entityTypeMapping2.SetClrType(EntityTypeExtensions.GetClrType(entityType));
            entitySetMapping.AddTypeMapping(entityTypeMapping2);
            new PropertyMappingGenerator(this._providerManifest).Generate(entityType, (IEnumerable <EdmProperty>)entityType.Properties, entitySetMapping, mappingFragment, (IList <EdmProperty>) new List <EdmProperty>(), false);
        }
        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);
        }
Пример #7
0
        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);
        }
Пример #8
0
        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 Can_generate_function_mappings_for_entity_type()
        {
            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));

            entityType.AddKeyMember(intProperty);

            var stringProperty = EdmProperty.CreatePrimitive("Name", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            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(2, functionMapping.ParameterBindings.Count);
            Assert.Null(functionMapping.ResultBindings);

            var function = functionMapping.Function;

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

            functionMapping = modificationFunctionMapping.UpdateFunctionMapping;

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

            function = functionMapping.Function;

            Assert.NotNull(function);
            Assert.Equal("E_Update", function.Name);
            Assert.Equal(2, 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);
        }
Пример #10
0
 private void Transform()
 {
     foreach (EntitySet entitySet1 in this._entityTypes.GetEntitySets())
     {
         EntitySet entitySet = entitySet1;
         Dictionary <TableMapping, Dictionary <EntityType, EntityTypeMapping> > dictionary = new Dictionary <TableMapping, Dictionary <EntityType, EntityTypeMapping> >();
         foreach (EntityType entityType1 in this._entityTypes.GetEntityTypes(entitySet))
         {
             EntityType entityType = entityType1;
             foreach (TableMapping tableMapping in this._tableMappings.Values.Where <TableMapping>((Func <TableMapping, bool>)(tm => tm.EntityTypes.Contains(entitySet, entityType))))
             {
                 Dictionary <EntityType, EntityTypeMapping> rootMappings;
                 if (!dictionary.TryGetValue(tableMapping, out rootMappings))
                 {
                     rootMappings = new Dictionary <EntityType, EntityTypeMapping>();
                     dictionary.Add(tableMapping, rootMappings);
                 }
                 EntityMappingService.RemoveRedundantDefaultDiscriminators(tableMapping);
                 bool requiresIsTypeOf = this.DetermineRequiresIsTypeOf(tableMapping, entitySet, entityType);
                 EntityTypeMapping propertiesTypeMapping;
                 MappingFragment   propertiesTypeMappingFragment;
                 if (this.FindPropertyEntityTypeMapping(tableMapping, entitySet, entityType, requiresIsTypeOf, out propertiesTypeMapping, out propertiesTypeMappingFragment))
                 {
                     bool entityTypeMapping1 = EntityMappingService.DetermineRequiresSplitEntityTypeMapping(tableMapping, entityType, requiresIsTypeOf);
                     EntityTypeMapping conditionTypeMapping = this.FindConditionTypeMapping(entityType, entityTypeMapping1, propertiesTypeMapping);
                     MappingFragment   typeMappingFragment  = EntityMappingService.FindConditionTypeMappingFragment(this._databaseMapping.Database.GetEntitySet(tableMapping.Table), propertiesTypeMappingFragment, conditionTypeMapping);
                     if (requiresIsTypeOf)
                     {
                         if (!propertiesTypeMapping.IsHierarchyMapping)
                         {
                             EntityTypeMapping entityTypeMapping2 = this._databaseMapping.GetEntityTypeMappings(entityType).SingleOrDefault <EntityTypeMapping>((Func <EntityTypeMapping, bool>)(etm => etm.IsHierarchyMapping));
                             if (entityTypeMapping2 == null)
                             {
                                 if (propertiesTypeMapping.MappingFragments.Count > 1)
                                 {
                                     EntityTypeMapping typeMapping = propertiesTypeMapping.Clone();
                                     this._databaseMapping.GetEntitySetMappings().Single <EntitySetMapping>((Func <EntitySetMapping, bool>)(esm => esm.EntityTypeMappings.Contains(propertiesTypeMapping))).AddTypeMapping(typeMapping);
                                     foreach (MappingFragment fragment in propertiesTypeMapping.MappingFragments.Where <MappingFragment>((Func <MappingFragment, bool>)(tmf => tmf != propertiesTypeMappingFragment)).ToArray <MappingFragment>())
                                     {
                                         propertiesTypeMapping.RemoveFragment(fragment);
                                         typeMapping.AddFragment(fragment);
                                     }
                                 }
                                 propertiesTypeMapping.AddIsOfType(propertiesTypeMapping.EntityType);
                             }
                             else
                             {
                                 propertiesTypeMapping.RemoveFragment(propertiesTypeMappingFragment);
                                 if (propertiesTypeMapping.MappingFragments.Count == 0)
                                 {
                                     this._databaseMapping.GetEntitySetMapping(entitySet).RemoveTypeMapping(propertiesTypeMapping);
                                 }
                                 propertiesTypeMapping = entityTypeMapping2;
                                 propertiesTypeMapping.AddFragment(propertiesTypeMappingFragment);
                             }
                         }
                         rootMappings.Add(entityType, propertiesTypeMapping);
                     }
                     EntityMappingService.ConfigureTypeMappings(tableMapping, rootMappings, entityType, propertiesTypeMappingFragment, typeMappingFragment);
                     if (propertiesTypeMappingFragment.IsUnmappedPropertiesFragment() && propertiesTypeMappingFragment.ColumnMappings.All <ColumnMappingBuilder>((Func <ColumnMappingBuilder, bool>)(pm => entityType.GetKeyProperties().Contains(pm.PropertyPath.First <EdmProperty>()))))
                     {
                         this.RemoveFragment(entitySet, propertiesTypeMapping, propertiesTypeMappingFragment);
                         if (entityTypeMapping1 && typeMappingFragment.ColumnMappings.All <ColumnMappingBuilder>((Func <ColumnMappingBuilder, bool>)(pm => entityType.GetKeyProperties().Contains(pm.PropertyPath.First <EdmProperty>()))))
                         {
                             this.RemoveFragment(entitySet, conditionTypeMapping, typeMappingFragment);
                         }
                     }
                     EntityMappingConfiguration.CleanupUnmappedArtifacts(this._databaseMapping, tableMapping.Table);
                     foreach (ForeignKeyBuilder foreignKeyBuilder in tableMapping.Table.ForeignKeyBuilders)
                     {
                         AssociationType associationType = foreignKeyBuilder.GetAssociationType();
                         if (associationType != null && associationType.IsRequiredToNonRequired())
                         {
                             AssociationEndMember principalEnd;
                             AssociationEndMember dependentEnd;
                             foreignKeyBuilder.GetAssociationType().TryGuessPrincipalAndDependentEnds(out principalEnd, out dependentEnd);
                             if (dependentEnd.GetEntityType() == entityType)
                             {
                                 this.MarkColumnsAsNonNullableIfNoTableSharing(entitySet, tableMapping.Table, entityType, foreignKeyBuilder.DependentColumns);
                             }
                         }
                     }
                 }
             }
         }
         this.ConfigureAssociationSetMappingForeignKeys(entitySet);
     }
 }