private void ConfigurePropertyMappings(
            DbDatabaseMapping databaseMapping,
            EntityType entityType,
            DbProviderManifest providerManifest,
            bool allowOverride = false)
        {
            DebugCheck.NotNull(databaseMapping);
            DebugCheck.NotNull(entityType);
            DebugCheck.NotNull(providerManifest);

            var entityTypeMappings = databaseMapping.GetEntityTypeMappings(entityType);

            var propertyMappings
                = (from etm in entityTypeMappings
                   from etmf in etm.MappingFragments
                   from pm in etmf.ColumnMappings
                   select Tuple.Create(pm, etmf.Table))
                  .ToList();

            ConfigurePropertyMappings(propertyMappings, providerManifest, allowOverride);

            foreach (var derivedEntityType
                     in databaseMapping.Model.EntityTypes.Where(et => et.BaseType == entityType))
            {
                ConfigurePropertyMappings(databaseMapping, derivedEntityType, providerManifest, true);
            }
        }
Example #2
0
        internal static bool AnyBaseTypeToTableWithoutColumnCondition(
            DbDatabaseMapping databaseMapping, EntityType entityType, EntityType table,
            EdmProperty column)
        {
            var baseType = entityType.BaseType;

            while (baseType != null)
            {
                if (!baseType.Abstract)
                {
                    var baseTypeTableFragments
                        = databaseMapping.GetEntityTypeMappings((EntityType)baseType)
                          .SelectMany(etm => etm.MappingFragments)
                          .Where(tmf => tmf.Table == table)
                          .ToList();

                    if (baseTypeTableFragments.Any() &&
                        baseTypeTableFragments
                        .SelectMany(etmf => etmf.ColumnConditions)
                        .All(cc => cc.ColumnProperty != column))
                    {
                        return(true);
                    }
                }

                baseType = baseType.BaseType;
            }

            return(false);
        }
Example #3
0
        private static bool RemapsInheritedProperties(
            DbDatabaseMapping databaseMapping, StorageEntityTypeMapping 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);
        }
Example #4
0
 private static bool RemapsInheritedProperties(
     DbDatabaseMapping databaseMapping,
     EntityTypeMapping entityTypeMapping)
 {
     foreach (EdmProperty edmProperty in entityTypeMapping.EntityType.Properties.Except <EdmProperty>((IEnumerable <EdmProperty>)entityTypeMapping.EntityType.DeclaredProperties).Except <EdmProperty>((IEnumerable <EdmProperty>)entityTypeMapping.EntityType.GetKeyProperties()))
     {
         EdmProperty     property = edmProperty;
         MappingFragment fragment = MappingInheritedPropertiesSupportConvention.GetFragmentForPropertyMapping(entityTypeMapping, property);
         if (fragment != null)
         {
             for (EntityType baseType = (EntityType)entityTypeMapping.EntityType.BaseType; baseType != null; baseType = (EntityType)baseType.BaseType)
             {
                 if (databaseMapping.GetEntityTypeMappings(baseType).Select <EntityTypeMapping, MappingFragment>((Func <EntityTypeMapping, MappingFragment>)(baseTypeMapping => MappingInheritedPropertiesSupportConvention.GetFragmentForPropertyMapping(baseTypeMapping, property))).Any <MappingFragment>((Func <MappingFragment, bool>)(baseFragment =>
                 {
                     if (baseFragment != null)
                     {
                         return(baseFragment.Table != fragment.Table);
                     }
                     return(false);
                 })))
                 {
                     return(true);
                 }
             }
         }
     }
     return(false);
 }
Example #5
0
        private static EntityType FindParentTable(
            DbDatabaseMapping databaseMapping,
            EntityType fromTable,
            EntityTypeMapping entityTypeMapping,
            EntityType toTable,
            bool isMappingInheritedProperties,
            int configurationIndex,
            int configurationCount,
            out bool isSplitting)
        {
            EntityType entityType = (EntityType)null;

            isSplitting = false;
            if ((entityTypeMapping.UsesOtherTables(toTable) || configurationCount > 1) && configurationIndex != 0)
            {
                entityType  = entityTypeMapping.GetPrimaryTable();
                isSplitting = true;
            }
            if (entityType == null && fromTable != toTable && !isMappingInheritedProperties)
            {
                for (EdmType baseType = entityTypeMapping.EntityType.BaseType; baseType != null && entityType == null; baseType = baseType.BaseType)
                {
                    EntityTypeMapping entityTypeMapping1 = databaseMapping.GetEntityTypeMappings((EntityType)baseType).FirstOrDefault <EntityTypeMapping>();
                    if (entityTypeMapping1 != null)
                    {
                        entityType = entityTypeMapping1.GetPrimaryTable();
                    }
                }
            }
            return(entityType);
        }
        internal void Configure(
            EntityType entityType,
            DbDatabaseMapping databaseMapping,
            DbProviderManifest providerManifest)
        {
            DebugCheck.NotNull(entityType);
            DebugCheck.NotNull(databaseMapping);
            DebugCheck.NotNull(providerManifest);

            var entityTypeMapping
                = databaseMapping.GetEntityTypeMapping(entityType.GetClrType());

            if (entityTypeMapping != null)
            {
                VerifyAllCSpacePropertiesAreMapped(
                    databaseMapping.GetEntityTypeMappings(entityType).ToList(),
                    entityTypeMapping.EntityType.DeclaredProperties,
                    new List <EdmProperty>());
            }

            ConfigurePropertyMappings(databaseMapping, entityType, providerManifest);
            ConfigureAssociationMappings(databaseMapping, entityType, providerManifest);
            ConfigureDependentKeys(databaseMapping, providerManifest);
            ConfigureModificationStoredProcedures(databaseMapping, entityType, providerManifest);
        }
Example #7
0
        private bool DiscoverIsSharingWithBase(
            DbDatabaseMapping databaseMapping, EntityType entityType, EntityType toTable)
        {
            var isSharingTableWithBase = false;

            if (entityType.BaseType != null)
            {
                var baseType        = entityType.BaseType;
                var anyBaseMappings = false;

                while (baseType != null &&
                       !isSharingTableWithBase)
                {
                    var baseMappings = databaseMapping.GetEntityTypeMappings((EntityType)baseType);

                    if (baseMappings.Any())
                    {
                        isSharingTableWithBase =
                            baseMappings.SelectMany(m => m.MappingFragments).Any(tmf => tmf.Table == toTable);
                        anyBaseMappings = true;
                    }

                    baseType = baseType.BaseType;
                }

                if (!anyBaseMappings)
                {
                    isSharingTableWithBase = TableName == null || string.IsNullOrWhiteSpace(TableName.Name);
                }
            }
            return(isSharingTableWithBase);
        }
Example #8
0
        private bool DiscoverIsSharingWithBase(
            DbDatabaseMapping databaseMapping,
            EntityType entityType,
            EntityType toTable)
        {
            bool flag1 = false;

            if (entityType.BaseType != null)
            {
                EdmType baseType = entityType.BaseType;
                bool    flag2    = false;
                for (; baseType != null && !flag1; baseType = baseType.BaseType)
                {
                    IList <EntityTypeMapping> entityTypeMappings = databaseMapping.GetEntityTypeMappings((EntityType)baseType);
                    if (entityTypeMappings.Any <EntityTypeMapping>())
                    {
                        flag1 = entityTypeMappings.SelectMany <EntityTypeMapping, MappingFragment>((Func <EntityTypeMapping, IEnumerable <MappingFragment> >)(m => (IEnumerable <MappingFragment>)m.MappingFragments)).Any <MappingFragment>((Func <MappingFragment, bool>)(tmf => tmf.Table == toTable));
                        flag2 = true;
                    }
                }
                if (!flag2)
                {
                    flag1 = this.TableName == null || string.IsNullOrWhiteSpace(this.TableName.Name);
                }
            }
            return(flag1);
        }
Example #9
0
        public static EntityTypeMapping GetEntityTypeMapping(
            this DbDatabaseMapping databaseMapping,
            System.Data.Entity.Core.Metadata.Edm.EntityType entityType)
        {
            IList <EntityTypeMapping> entityTypeMappings = databaseMapping.GetEntityTypeMappings(entityType);

            if (entityTypeMappings.Count <= 1)
            {
                return(entityTypeMappings.FirstOrDefault <EntityTypeMapping>());
            }
            return(entityTypeMappings.SingleOrDefault <EntityTypeMapping>((Func <EntityTypeMapping, bool>)(m => m.IsHierarchyMapping)));
        }
        private void ConfigurePropertyMappings(
            DbDatabaseMapping databaseMapping,
            EntityType entityType,
            DbProviderManifest providerManifest,
            bool allowOverride = false)
        {
            DebugCheck.NotNull(databaseMapping);
            DebugCheck.NotNull(entityType);
            DebugCheck.NotNull(providerManifest);

            var entityTypeMappings
                = databaseMapping.GetEntityTypeMappings(entityType);

            var propertyMappings
                = (from etm in entityTypeMappings
                   from etmf in etm.MappingFragments
                   from pm in etmf.ColumnMappings
                   select Tuple.Create(pm, etmf.Table))
                  .ToList();

            ConfigurePropertyMappings(propertyMappings, providerManifest, allowOverride);

            _entityMappingConfigurations
            .Each(c => c.ConfigurePropertyMappings(propertyMappings, providerManifest, allowOverride));

            // Now, apply to any inherited (IsOfType) mappings
            var inheritedPropertyMappings
                = (from esm in databaseMapping.GetEntitySetMappings()
                   from etm in esm.EntityTypeMappings
                   where etm.IsHierarchyMapping &&
                   etm.EntityType.IsAncestorOf(entityType)
                   from etmf in etm.MappingFragments
                   from pm1 in etmf.ColumnMappings
                   where !propertyMappings.Any(pm2 => pm2.Item1.PropertyPath.SequenceEqual(pm1.PropertyPath))
                   select Tuple.Create(pm1, etmf.Table))
                  .ToList();

            ConfigurePropertyMappings(inheritedPropertyMappings, providerManifest);

            _entityMappingConfigurations
            .Each(c => c.ConfigurePropertyMappings(inheritedPropertyMappings, providerManifest));

            foreach (var derivedEntityType
                     in databaseMapping.Model.EntityTypes.Where(et => et.BaseType == entityType))
            {
                ConfigurePropertyMappings(databaseMapping, derivedEntityType, providerManifest, true);
            }
        }
        public static StorageEntityTypeMapping GetEntityTypeMapping(
            this DbDatabaseMapping databaseMapping, EntityType entityType)
        {
            DebugCheck.NotNull(databaseMapping);
            DebugCheck.NotNull(entityType);

            var mappings = databaseMapping.GetEntityTypeMappings(entityType);

            if (mappings.Count <= 1)
            {
                return(mappings.FirstOrDefault());
            }

            // Return the property mapping
            return(mappings.SingleOrDefault(m => m.IsHierarchyMapping));
        }
Example #12
0
        private void ConfigureIndexes(DbDatabaseMapping mapping, EntityType entityType)
        {
            DebugCheck.NotNull(mapping);
            DebugCheck.NotNull(entityType);

            var entityTypeMappings = mapping.GetEntityTypeMappings(entityType);

            if (_keyConfiguration != null)
            {
                entityTypeMappings
                .SelectMany(etm => etm.Fragments)
                .Each(f => _keyConfiguration.Configure(f.Table));
            }

            foreach (var indexConfiguration in _indexConfigurations)
            {
                foreach (var entityTypeMapping in entityTypeMappings)
                {
                    var propertyMappings = indexConfiguration.Key
                                           .ToDictionary(
                        icp => icp,
                        icp => entityTypeMapping.GetPropertyMapping(
                            entityType.GetDeclaredPrimitiveProperty(icp)));

                    if (indexConfiguration.Key.Count > 1 && string.IsNullOrEmpty(indexConfiguration.Value.Name))
                    {
                        indexConfiguration.Value.Name = IndexOperation.BuildDefaultName(
                            indexConfiguration.Key.Select(icp => propertyMappings[icp].ColumnProperty.Name));
                    }

                    int sortOrder = 0;

                    foreach (var indexConfigurationProperty in indexConfiguration.Key)
                    {
                        var propertyMapping = propertyMappings[indexConfigurationProperty];

                        indexConfiguration.Value.Configure(
                            propertyMapping.ColumnProperty,
                            (indexConfiguration.Key.Count != 1 ?
                             sortOrder :
                             -1));

                        ++sortOrder;
                    }
                }
            }
        }
Example #13
0
        private static EntityType FindParentTable(
            DbDatabaseMapping databaseMapping,
            EntityType fromTable,
            EntityTypeMapping entityTypeMapping,
            EntityType toTable,
            bool isMappingInheritedProperties,
            int configurationIndex,
            int configurationCount,
            out bool isSplitting)
        {
            EntityType parentTable = null;

            isSplitting = false;
            // Check for entity splitting first, since splitting on a derived type in TPT/TPC will always have fromTable != toTable
            if (entityTypeMapping.UsesOtherTables(toTable) ||
                configurationCount > 1)
            {
                if (configurationIndex != 0)
                {
                    // Entity Splitting case
                    parentTable = entityTypeMapping.GetPrimaryTable();
                    isSplitting = true;
                }
            }

            if (parentTable == null &&
                fromTable != toTable &&
                !isMappingInheritedProperties)
            {
                // TPT case
                var baseType = entityTypeMapping.EntityType.BaseType;
                while (baseType != null &&
                       parentTable == null)
                {
                    // Traverse to first anscestor with a mapping
                    var baseMapping = databaseMapping.GetEntityTypeMappings((EntityType)baseType).FirstOrDefault();
                    if (baseMapping != null)
                    {
                        parentTable = baseMapping.GetPrimaryTable();
                    }
                    baseType = baseType.BaseType;
                }
            }

            return(parentTable);
        }
Example #14
0
 internal static bool AnyBaseTypeToTableWithoutColumnCondition(
     DbDatabaseMapping databaseMapping,
     EntityType entityType,
     EntityType table,
     EdmProperty column)
 {
     for (EdmType baseType = entityType.BaseType; baseType != null; baseType = baseType.BaseType)
     {
         if (!baseType.Abstract)
         {
             List <MappingFragment> list = databaseMapping.GetEntityTypeMappings((EntityType)baseType).SelectMany <EntityTypeMapping, MappingFragment>((Func <EntityTypeMapping, IEnumerable <MappingFragment> >)(etm => (IEnumerable <MappingFragment>)etm.MappingFragments)).Where <MappingFragment>((Func <MappingFragment, bool>)(tmf => tmf.Table == table)).ToList <MappingFragment>();
             if (list.Any <MappingFragment>() && list.SelectMany <MappingFragment, ConditionPropertyMapping>((Func <MappingFragment, IEnumerable <ConditionPropertyMapping> >)(etmf => etmf.ColumnConditions)).All <ConditionPropertyMapping>((Func <ConditionPropertyMapping, bool>)(cc => cc.Column != column)))
             {
                 return(true);
             }
         }
     }
     return(false);
 }
Example #15
0
        private void ConfigurePropertyMappings(
            DbDatabaseMapping databaseMapping,
            EntityType entityType,
            DbProviderManifest providerManifest,
            bool allowOverride = false)
        {
            List <Tuple <ColumnMappingBuilder, EntityType> > propertyMappings = databaseMapping.GetEntityTypeMappings(entityType).SelectMany((Func <EntityTypeMapping, IEnumerable <MappingFragment> >)(etm => (IEnumerable <MappingFragment>)etm.MappingFragments), (etm, etmf) => new
            {
                etm  = etm,
                etmf = etmf
            }).SelectMany(_param0 => _param0.etmf.ColumnMappings, (_param0, pm) => Tuple.Create <ColumnMappingBuilder, EntityType>(pm, _param0.etmf.Table)).ToList <Tuple <ColumnMappingBuilder, EntityType> >();

            this.ConfigurePropertyMappings((IList <Tuple <ColumnMappingBuilder, EntityType> >)propertyMappings, providerManifest, allowOverride);
            this._entityMappingConfigurations.Each <EntityMappingConfiguration>((Action <EntityMappingConfiguration>)(c => c.ConfigurePropertyMappings((IList <Tuple <ColumnMappingBuilder, EntityType> >)propertyMappings, providerManifest, allowOverride)));
            List <Tuple <ColumnMappingBuilder, EntityType> > inheritedPropertyMappings = databaseMapping.GetEntitySetMappings().SelectMany((Func <EntitySetMapping, IEnumerable <EntityTypeMapping> >)(esm => (IEnumerable <EntityTypeMapping>)esm.EntityTypeMappings), (esm, etm) => new
            {
                esm = esm,
                etm = etm
            }).Where(_param1 =>
            {
                if (_param1.etm.IsHierarchyMapping)
                {
                    return(_param1.etm.EntityType.IsAncestorOf(entityType));
                }
                return(false);
            }).SelectMany(_param0 => (IEnumerable <MappingFragment>)_param0.etm.MappingFragments, (_param0, etmf) => new
            {
                \u003C\u003Eh__TransparentIdentifier43 = _param0,
                etmf = etmf
            }).SelectMany(_param0 => _param0.etmf.ColumnMappings, (_param0, pm1) => new
            {
                \u003C\u003Eh__TransparentIdentifier44 = _param0,
                pm1 = pm1
            }).Where(_param1 => !propertyMappings.Any <Tuple <ColumnMappingBuilder, EntityType> >((Func <Tuple <ColumnMappingBuilder, EntityType>, bool>)(pm2 => pm2.Item1.PropertyPath.SequenceEqual <EdmProperty>((IEnumerable <EdmProperty>)_param1.pm1.PropertyPath)))).Select(_param0 => Tuple.Create <ColumnMappingBuilder, EntityType>(_param0.pm1, _param0.\u003C\u003Eh__TransparentIdentifier44.etmf.Table)).ToList <Tuple <ColumnMappingBuilder, EntityType> >();

            this.ConfigurePropertyMappings((IList <Tuple <ColumnMappingBuilder, EntityType> >)inheritedPropertyMappings, providerManifest, false);
            this._entityMappingConfigurations.Each <EntityMappingConfiguration>((Action <EntityMappingConfiguration>)(c => c.ConfigurePropertyMappings((IList <Tuple <ColumnMappingBuilder, EntityType> >)inheritedPropertyMappings, providerManifest, false)));
            foreach (EntityType entityType1 in databaseMapping.Model.EntityTypes.Where <EntityType>((Func <EntityType, bool>)(et => et.BaseType == entityType)))
            {
                this.ConfigurePropertyMappings(databaseMapping, entityType1, providerManifest, true);
            }
        }
Example #16
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)
        {
            DebugCheck.NotNull(entityType);
            DebugCheck.NotNull(databaseMapping);
            DebugCheck.NotNull(providerManifest);

            var baseType        = (EntityType)entityType.BaseType;
            var isIdentityTable = baseType == null && configurationIndex == 0;

            var fragment = FindOrCreateTypeMappingFragment(
                databaseMapping, ref entityTypeMapping, configurationIndex, entityType, providerManifest);
            var  fromTable = fragment.Table;
            bool isTableSharing;
            var  toTable = FindOrCreateTargetTable(
                databaseMapping, fragment, entityType, fromTable, out isTableSharing);

            var isSharingTableWithBase = DiscoverIsSharingWithBase(databaseMapping, entityType, toTable);

            // Ensure all specified properties are the only ones present in this fragment and table
            var mappingsToContain = DiscoverAllMappingsToContain(
                databaseMapping, entityType, toTable, isSharingTableWithBase);

            // Validate that specified properties can be mapped
            var mappingsToMove = fragment.ColumnMappings.ToList();

            foreach (var propertyPath in mappingsToContain)
            {
                var propertyMapping = fragment.ColumnMappings.SingleOrDefault(
                    pm => pm.PropertyPath.SequenceEqual(propertyPath));

                if (propertyMapping == null)
                {
                    throw Error.EntityMappingConfiguration_DuplicateMappedProperty(
                              entityType.Name, propertyPath.ToString());
                }
                mappingsToMove.Remove(propertyMapping);
            }

            // Add table constraint if there are no inherited properties
            if (!isIdentityTable)
            {
                bool isSplitting;
                var  parentTable = FindParentTable(
                    databaseMapping,
                    fromTable,
                    entityTypeMapping,
                    toTable,
                    isMappingAnyInheritedProperty,
                    configurationIndex,
                    configurationCount,
                    out isSplitting);
                if (parentTable != null)
                {
                    DatabaseOperations.AddTypeConstraint(databaseMapping.Database, entityType, parentTable, toTable, isSplitting);
                }
            }

            // Update AssociationSetMappings (IAs) and FKs
            if (fromTable != toTable)
            {
                if (Properties == null)
                {
                    AssociationMappingOperations.MoveAllDeclaredAssociationSetMappings(
                        databaseMapping, entityType, fromTable, toTable, !isTableSharing);
                    ForeignKeyPrimitiveOperations.MoveAllDeclaredForeignKeyConstraintsForPrimaryKeyColumns(
                        entityType, fromTable, toTable);
                }
                if (isMappingAnyInheritedProperty)
                {
                    var baseTables =
                        databaseMapping.GetEntityTypeMappings(baseType)
                        .SelectMany(etm => etm.MappingFragments)
                        .Select(mf => mf.Table);

                    var associationMapping = databaseMapping.EntityContainerMappings
                                             .SelectMany(asm => asm.AssociationSetMappings)
                                             .FirstOrDefault(a => baseTables.Contains(a.Table) &&
                                                             (baseType == a.AssociationSet.ElementType.SourceEnd.GetEntityType() ||
                                                              baseType == a.AssociationSet.ElementType.TargetEnd.GetEntityType()));

                    if (associationMapping != null)
                    {
                        var associationType = associationMapping.AssociationSet.ElementType;

                        throw Error.EntityMappingConfiguration_TPCWithIAsOnNonLeafType(
                                  associationType.Name,
                                  associationType.SourceEnd.GetEntityType().Name,
                                  associationType.TargetEnd.GetEntityType().Name);
                    }

                    // With TPC, we need to move down FK constraints, even on PKs (except type mapping constraints that are not about associations)
                    ForeignKeyPrimitiveOperations.CopyAllForeignKeyConstraintsForPrimaryKeyColumns(
                        databaseMapping.Database, fromTable, toTable);
                }
            }

            if (mappingsToMove.Any())
            {
                EntityType extraTable = null;
                if (configurationIndex < configurationCount - 1)
                {
                    // Move all extra properties to a single new fragment
                    var anyPropertyMapping = mappingsToMove.First();

                    extraTable
                        = FindTableForTemporaryExtraPropertyMapping(
                              databaseMapping, entityType, fromTable, toTable, anyPropertyMapping);

                    var extraFragment
                        = EntityMappingOperations
                          .CreateTypeMappingFragment(entityTypeMapping, fragment, databaseMapping.Database.GetEntitySet(extraTable));

                    var requiresUpdate = extraTable != fromTable;

                    foreach (var pm in mappingsToMove)
                    {
                        // move the property mapping from toFragment to extraFragment
                        EntityMappingOperations.MovePropertyMapping(
                            databaseMapping, entitySets, fragment, extraFragment, pm, requiresUpdate, true);
                    }
                }
                else
                {
                    // Move each extra property mapping to a fragment refering to the table with the base mapping
                    EntityType unmappedTable = null;
                    foreach (var pm in mappingsToMove)
                    {
                        extraTable = FindTableForExtraPropertyMapping(
                            databaseMapping, entityType, fromTable, toTable, ref unmappedTable, pm);

                        var extraFragment =
                            entityTypeMapping.MappingFragments.SingleOrDefault(tmf => tmf.Table == extraTable);

                        if (extraFragment == null)
                        {
                            extraFragment
                                = EntityMappingOperations
                                  .CreateTypeMappingFragment(
                                      entityTypeMapping, fragment, databaseMapping.Database.GetEntitySet(extraTable));

                            extraFragment.SetIsUnmappedPropertiesFragment(true);
                        }

                        if (extraTable == fromTable)
                        {
                            // copy the default discriminator along with the properties
                            CopyDefaultDiscriminator(fragment, extraFragment);
                        }

                        var requiresUpdate = extraTable != fromTable;
                        EntityMappingOperations.MovePropertyMapping(
                            databaseMapping, entitySets, fragment, extraFragment, pm, requiresUpdate, true);
                    }
                }
            }

            // Ensure all property mappings refer to the table in the fragment
            // Uniquify: true if table sharing, false otherwise
            //           FK names should be uniquified
            //           declared properties are moved, inherited ones are copied (duplicated)
            EntityMappingOperations.UpdatePropertyMappings(
                databaseMapping, entitySets, fromTable, fragment, !isTableSharing);

            // Configure Conditions for the fragment
            ConfigureDefaultDiscriminator(entityType, fragment);
            ConfigureConditions(databaseMapping, entityType, fragment, providerManifest);

            // Ensure all conditions refer to columns on the table in the fragment
            EntityMappingOperations.UpdateConditions(databaseMapping.Database, fromTable, fragment);

            ForeignKeyPrimitiveOperations.UpdatePrincipalTables(
                databaseMapping, entityType, fromTable, toTable, isMappingAnyInheritedProperty);

            CleanupUnmappedArtifacts(databaseMapping, fromTable);
            CleanupUnmappedArtifacts(databaseMapping, toTable);

            ConfigureAnnotations(toTable, commonAnnotations);
            ConfigureAnnotations(toTable, _annotations);

            toTable.SetConfiguration(this);
        }
Example #17
0
        internal void Configure(
            EntityType entityType,
            DbDatabaseMapping databaseMapping,
            DbProviderManifest providerManifest)
        {
            EntityTypeMapping entityTypeMapping = databaseMapping.GetEntityTypeMapping(EntityTypeExtensions.GetClrType(entityType));

            if (entityTypeMapping != null)
            {
                EntityTypeConfiguration.VerifyAllCSpacePropertiesAreMapped((ICollection <EntityTypeMapping>)databaseMapping.GetEntityTypeMappings(entityType).ToList <EntityTypeMapping>(), (IEnumerable <EdmProperty>)entityTypeMapping.EntityType.DeclaredProperties, (IList <EdmProperty>) new List <EdmProperty>());
            }
            this.ConfigurePropertyMappings(databaseMapping, entityType, providerManifest, false);
            this.ConfigureAssociationMappings(databaseMapping, entityType, providerManifest);
            EntityTypeConfiguration.ConfigureDependentKeys(databaseMapping, providerManifest);
            this.ConfigureModificationStoredProcedures(databaseMapping, entityType, providerManifest);
        }
        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);
            }
        }
Example #19
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);
        }