Пример #1
0
        public void Configure(
            DbDatabaseMapping databaseMapping,
            ICollection <EntitySet> entitySets,
            DbProviderManifest providerManifest,
            EntityType entityType,
            ref EntityTypeMapping entityTypeMapping,
            bool isMappingAnyInheritedProperty,
            int configurationIndex,
            int configurationCount,
            IDictionary <string, object> commonAnnotations)
        {
            EntityType                  baseType             = (EntityType)entityType.BaseType;
            bool                        flag                 = baseType == null && configurationIndex == 0;
            MappingFragment             typeMappingFragment1 = this.FindOrCreateTypeMappingFragment(databaseMapping, ref entityTypeMapping, configurationIndex, entityType, providerManifest);
            EntityType                  table                = typeMappingFragment1.Table;
            bool                        isTableSharing;
            EntityType                  createTargetTable      = this.FindOrCreateTargetTable(databaseMapping, typeMappingFragment1, entityType, table, out isTableSharing);
            bool                        isSharingTableWithBase = this.DiscoverIsSharingWithBase(databaseMapping, entityType, createTargetTable);
            HashSet <EdmPropertyPath>   contain = this.DiscoverAllMappingsToContain(databaseMapping, entityType, createTargetTable, isSharingTableWithBase);
            List <ColumnMappingBuilder> list    = typeMappingFragment1.ColumnMappings.ToList <ColumnMappingBuilder>();

            foreach (EdmPropertyPath edmPropertyPath in contain)
            {
                EdmPropertyPath      propertyPath         = edmPropertyPath;
                ColumnMappingBuilder columnMappingBuilder = typeMappingFragment1.ColumnMappings.SingleOrDefault <ColumnMappingBuilder>((Func <ColumnMappingBuilder, bool>)(pm => pm.PropertyPath.SequenceEqual <EdmProperty>((IEnumerable <EdmProperty>)propertyPath)));
                if (columnMappingBuilder == null)
                {
                    throw Error.EntityMappingConfiguration_DuplicateMappedProperty((object)entityType.Name, (object)propertyPath.ToString());
                }
                list.Remove(columnMappingBuilder);
            }
            if (!flag)
            {
                bool       isSplitting;
                EntityType parentTable = EntityMappingConfiguration.FindParentTable(databaseMapping, table, entityTypeMapping, createTargetTable, isMappingAnyInheritedProperty, configurationIndex, configurationCount, out isSplitting);
                if (parentTable != null)
                {
                    DatabaseOperations.AddTypeConstraint(databaseMapping.Database, entityType, parentTable, createTargetTable, isSplitting);
                }
            }
            if (table != createTargetTable)
            {
                if (this.Properties == null)
                {
                    AssociationMappingOperations.MoveAllDeclaredAssociationSetMappings(databaseMapping, entityType, table, createTargetTable, !isTableSharing);
                    ForeignKeyPrimitiveOperations.MoveAllDeclaredForeignKeyConstraintsForPrimaryKeyColumns(entityType, table, createTargetTable);
                }
                if (isMappingAnyInheritedProperty)
                {
                    IEnumerable <EntityType> baseTables            = databaseMapping.GetEntityTypeMappings(baseType).SelectMany <EntityTypeMapping, MappingFragment>((Func <EntityTypeMapping, IEnumerable <MappingFragment> >)(etm => (IEnumerable <MappingFragment>)etm.MappingFragments)).Select <MappingFragment, EntityType>((Func <MappingFragment, EntityType>)(mf => mf.Table));
                    AssociationSetMapping    associationSetMapping = databaseMapping.EntityContainerMappings.SelectMany <EntityContainerMapping, AssociationSetMapping>((Func <EntityContainerMapping, IEnumerable <AssociationSetMapping> >)(asm => asm.AssociationSetMappings)).FirstOrDefault <AssociationSetMapping>((Func <AssociationSetMapping, bool>)(a =>
                    {
                        if (!baseTables.Contains <EntityType>(a.Table))
                        {
                            return(false);
                        }
                        if (baseType != a.AssociationSet.ElementType.SourceEnd.GetEntityType())
                        {
                            return(baseType == a.AssociationSet.ElementType.TargetEnd.GetEntityType());
                        }
                        return(true);
                    }));
                    if (associationSetMapping != null)
                    {
                        AssociationType elementType = associationSetMapping.AssociationSet.ElementType;
                        throw Error.EntityMappingConfiguration_TPCWithIAsOnNonLeafType((object)elementType.Name, (object)elementType.SourceEnd.GetEntityType().Name, (object)elementType.TargetEnd.GetEntityType().Name);
                    }
                    ForeignKeyPrimitiveOperations.CopyAllForeignKeyConstraintsForPrimaryKeyColumns(databaseMapping.Database, table, createTargetTable);
                }
            }
            if (list.Any <ColumnMappingBuilder>())
            {
                EntityType extraTable = (EntityType)null;
                if (configurationIndex < configurationCount - 1)
                {
                    ColumnMappingBuilder pm = list.First <ColumnMappingBuilder>();
                    extraTable = EntityMappingConfiguration.FindTableForTemporaryExtraPropertyMapping(databaseMapping, entityType, table, createTargetTable, pm);
                    MappingFragment typeMappingFragment2 = EntityMappingOperations.CreateTypeMappingFragment(entityTypeMapping, typeMappingFragment1, databaseMapping.Database.GetEntitySet(extraTable));
                    bool            requiresUpdate       = extraTable != table;
                    foreach (ColumnMappingBuilder propertyMappingBuilder in list)
                    {
                        EntityMappingOperations.MovePropertyMapping(databaseMapping, (IEnumerable <EntitySet>)entitySets, typeMappingFragment1, typeMappingFragment2, propertyMappingBuilder, requiresUpdate, true);
                    }
                }
                else
                {
                    EntityType unmappedTable = (EntityType)null;
                    foreach (ColumnMappingBuilder columnMappingBuilder in list)
                    {
                        extraTable = EntityMappingConfiguration.FindTableForExtraPropertyMapping(databaseMapping, entityType, table, createTargetTable, ref unmappedTable, columnMappingBuilder);
                        MappingFragment mappingFragment = entityTypeMapping.MappingFragments.SingleOrDefault <MappingFragment>((Func <MappingFragment, bool>)(tmf => tmf.Table == extraTable));
                        if (mappingFragment == null)
                        {
                            mappingFragment = EntityMappingOperations.CreateTypeMappingFragment(entityTypeMapping, typeMappingFragment1, databaseMapping.Database.GetEntitySet(extraTable));
                            mappingFragment.SetIsUnmappedPropertiesFragment(true);
                        }
                        if (extraTable == table)
                        {
                            EntityMappingConfiguration.CopyDefaultDiscriminator(typeMappingFragment1, mappingFragment);
                        }
                        bool requiresUpdate = extraTable != table;
                        EntityMappingOperations.MovePropertyMapping(databaseMapping, (IEnumerable <EntitySet>)entitySets, typeMappingFragment1, mappingFragment, columnMappingBuilder, requiresUpdate, true);
                    }
                }
            }
            EntityMappingOperations.UpdatePropertyMappings(databaseMapping, (IEnumerable <EntitySet>)entitySets, table, typeMappingFragment1, !isTableSharing);
            this.ConfigureDefaultDiscriminator(entityType, typeMappingFragment1);
            this.ConfigureConditions(databaseMapping, entityType, typeMappingFragment1, providerManifest);
            EntityMappingOperations.UpdateConditions(databaseMapping.Database, table, typeMappingFragment1);
            ForeignKeyPrimitiveOperations.UpdatePrincipalTables(databaseMapping, entityType, table, createTargetTable, isMappingAnyInheritedProperty);
            EntityMappingConfiguration.CleanupUnmappedArtifacts(databaseMapping, table);
            EntityMappingConfiguration.CleanupUnmappedArtifacts(databaseMapping, createTargetTable);
            EntityMappingConfiguration.ConfigureAnnotations((EdmType)createTargetTable, commonAnnotations);
            EntityMappingConfiguration.ConfigureAnnotations((EdmType)createTargetTable, this._annotations);
            createTargetTable.SetConfiguration((object)this);
        }
        private void Transform()
        {
            foreach (var entitySet in _entityTypes.GetEntitySets())
            {
                var setRootMappings = new Dictionary <TableMapping, Dictionary <EntityType, StorageEntityTypeMapping> >();

                foreach (var entityType in _entityTypes.GetEntityTypes(entitySet))
                {
                    foreach (
                        var tableMapping in
                        _tableMappings.Values.Where(tm => tm.EntityTypes.Contains(entitySet, entityType)))
                    {
                        Dictionary <EntityType, StorageEntityTypeMapping> rootMappings;
                        if (!setRootMappings.TryGetValue(tableMapping, out rootMappings))
                        {
                            rootMappings = new Dictionary <EntityType, StorageEntityTypeMapping>();
                            setRootMappings.Add(tableMapping, rootMappings);
                        }

                        RemoveRedundantDefaultDiscriminators(tableMapping);

                        var requiresIsTypeOf = DetermineRequiresIsTypeOf(tableMapping, entitySet, entityType);
                        var requiresSplit    = false;

                        // Find the entity type mapping and fragment for this table / entity type mapping where properties will be mapped
                        StorageEntityTypeMapping propertiesTypeMapping;
                        StorageMappingFragment   propertiesTypeMappingFragment;
                        if (
                            !FindPropertyEntityTypeMapping(
                                tableMapping,
                                entitySet,
                                entityType,
                                requiresIsTypeOf,
                                out propertiesTypeMapping,
                                out propertiesTypeMappingFragment))
                        {
                            continue;
                        }

                        // Determine if the entity type mapping needs to be split into separate properties and condition type mappings.
                        requiresSplit = DetermineRequiresSplitEntityTypeMapping(
                            tableMapping, entityType, requiresIsTypeOf);

                        // Find the entity type mapping and fragment for this table / entity type mapping where conditions will be mapped
                        var conditionTypeMapping
                            = FindConditionTypeMapping(entityType, requiresSplit, propertiesTypeMapping);

                        var conditionTypeMappingFragment
                            = FindConditionTypeMappingFragment(
                                  _databaseMapping.Database.GetEntitySet(tableMapping.Table),
                                  propertiesTypeMappingFragment,
                                  conditionTypeMapping);

                        // Set the IsTypeOf appropriately
                        if (requiresIsTypeOf)
                        {
                            if (propertiesTypeMapping.IsHierarchyMapping == false)
                            {
                                var isTypeOfEntityTypeMapping =
                                    _databaseMapping.GetEntityTypeMappings(entityType).SingleOrDefault(
                                        etm => etm.IsHierarchyMapping);

                                if (isTypeOfEntityTypeMapping == null)
                                {
                                    if (propertiesTypeMapping.MappingFragments.Count > 1)
                                    {
                                        // Need to create a new entity type mapping with the non-IsTypeOf contents
                                        var nonIsTypeOfEntityTypeMapping = propertiesTypeMapping.Clone();
                                        var parentEntitySetMapping       =
                                            _databaseMapping.GetEntitySetMappings().Single(
                                                esm => esm.EntityTypeMappings.Contains(propertiesTypeMapping));
                                        parentEntitySetMapping.AddTypeMapping(nonIsTypeOfEntityTypeMapping);
                                        foreach (
                                            var fragment in
                                            propertiesTypeMapping.MappingFragments.Where(
                                                tmf => tmf != propertiesTypeMappingFragment).ToArray())
                                        {
                                            propertiesTypeMapping.RemoveFragment(fragment);
                                            nonIsTypeOfEntityTypeMapping.AddFragment(fragment);
                                        }
                                    }
                                    // else we just use the existing property mapping

                                    propertiesTypeMapping.AddIsOfType(propertiesTypeMapping.EntityType);
                                }
                                else
                                {
                                    // found an existing IsTypeOf mapping, so re-use that one
                                    propertiesTypeMapping.RemoveFragment(propertiesTypeMappingFragment);

                                    if (propertiesTypeMapping.MappingFragments.Count == 0)
                                    {
                                        _databaseMapping
                                        .GetEntitySetMapping(entitySet)
                                        .RemoveTypeMapping(propertiesTypeMapping);
                                    }

                                    propertiesTypeMapping = isTypeOfEntityTypeMapping;
                                    propertiesTypeMapping.AddFragment(propertiesTypeMappingFragment);
                                }
                            }
                            rootMappings.Add(entityType, propertiesTypeMapping);
                        }

                        ConfigureTypeMappings(
                            tableMapping, rootMappings, entityType, propertiesTypeMappingFragment,
                            conditionTypeMappingFragment);

                        if (propertiesTypeMappingFragment.IsUnmappedPropertiesFragment()
                            &&
                            propertiesTypeMappingFragment.ColumnMappings.All(
                                pm => entityType.GetKeyProperties().Contains(pm.PropertyPath.First())))
                        {
                            RemoveFragment(entitySet, propertiesTypeMapping, propertiesTypeMappingFragment);

                            if (requiresSplit
                                &&
                                conditionTypeMappingFragment.ColumnMappings.All(
                                    pm => entityType.GetKeyProperties().Contains(pm.PropertyPath.First())))
                            {
                                RemoveFragment(entitySet, conditionTypeMapping, conditionTypeMappingFragment);
                            }
                        }

                        EntityMappingConfiguration.CleanupUnmappedArtifacts(_databaseMapping, tableMapping.Table);

                        foreach (var fkConstraint in tableMapping.Table.ForeignKeyBuilders)
                        {
                            var associationType = fkConstraint.GetAssociationType();
                            if (associationType != null &&
                                associationType.IsRequiredToNonRequired())
                            {
                                AssociationEndMember _, dependentEnd;
                                fkConstraint.GetAssociationType().TryGuessPrincipalAndDependentEnds(
                                    out _, out dependentEnd);

                                if (dependentEnd.GetEntityType() == entityType)
                                {
                                    MarkColumnsAsNonNullableIfNoTableSharing(
                                        entitySet, tableMapping.Table, entityType, fkConstraint.DependentColumns);
                                }
                            }
                        }
                    }
                }

                ConfigureAssociationSetMappingForeignKeys(entitySet);
            }
        }
Пример #3
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);
     }
 }