protected static StorageEntityTypeMapping GetEntityTypeMappingInHierarchy(
            DbDatabaseMapping databaseMapping, EntityType entityType)
        {
            DebugCheck.NotNull(databaseMapping);
            DebugCheck.NotNull(entityType);

            var entityTypeMapping = databaseMapping.GetEntityTypeMapping(entityType);

            if (entityTypeMapping == null)
            {
                var entitySetMapping =
                    databaseMapping.GetEntitySetMapping(databaseMapping.Model.GetEntitySet(entityType));

                if (entitySetMapping != null)
                {
                    entityTypeMapping = entitySetMapping
                                        .EntityTypeMappings
                                        .First(
                        etm => entityType.DeclaredProperties.All(
                            dp => etm.MappingFragments.First()
                            .ColumnMappings.Select(pm => pm.PropertyPath.First()).Contains(dp)));
                }
            }

            if (entityTypeMapping == null)
            {
                throw Error.UnmappedAbstractType(entityType.GetClrType());
            }

            return(entityTypeMapping);
        }
Exemple #2
0
        private static EntityType FindBaseTableForExtraPropertyMapping(
            DbDatabaseMapping databaseMapping, EntityType entityType, ColumnMappingBuilder pm)
        {
            var baseType = (EntityType)entityType.BaseType;

            MappingFragment baseFragment = null;

            while (baseType != null &&
                   baseFragment == null)
            {
                var baseMapping = databaseMapping.GetEntityTypeMapping(baseType);
                if (baseMapping != null)
                {
                    baseFragment =
                        baseMapping.MappingFragments.SingleOrDefault(
                            f => f.ColumnMappings.Any(bpm => bpm.PropertyPath.SequenceEqual(pm.PropertyPath)));

                    if (baseFragment != null)
                    {
                        return(baseFragment.Table);
                    }
                }
                baseType = (EntityType)baseType.BaseType;
            }
            return(null);
        }
        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);
        }
        private void ConfigureEntityTypes(DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest)
        {
            DebugCheck.NotNull(databaseMapping);
            DebugCheck.NotNull(providerManifest);

            var sortedEntityConfigurations =
                SortEntityConfigurationsByInheritance(databaseMapping);

            foreach (var entityTypeConfiguration in sortedEntityConfigurations)
            {
                var entityTypeMapping
                    = databaseMapping.GetEntityTypeMapping(entityTypeConfiguration.ClrType);

                entityTypeConfiguration.ConfigureTablesAndConditions(
                    entityTypeMapping, databaseMapping, providerManifest);

                // run through all unconfigured derived types of the current entityType to make sure the property mappings now point to the right places
                ConfigureUnconfiguredDerivedTypes(
                    databaseMapping,
                    providerManifest,
                    databaseMapping.Model.GetEntityType(entityTypeConfiguration.ClrType),
                    sortedEntityConfigurations);
            }

            new EntityMappingService(databaseMapping).Configure();

            foreach (var entityType in databaseMapping.Model.EntityTypes.Where(e => e.GetConfiguration() != null))
            {
                var entityTypeConfiguration = (EntityTypeConfiguration)entityType.GetConfiguration();

                entityTypeConfiguration.Configure(entityType, databaseMapping, providerManifest);
            }
        }
Exemple #5
0
        protected static EntityTypeMapping GetEntityTypeMappingInHierarchy(
            DbDatabaseMapping databaseMapping, EntityType entityType)
        {
            DebugCheck.NotNull(databaseMapping);
            DebugCheck.NotNull(entityType);

            var entityTypeMapping = databaseMapping.GetEntityTypeMapping(entityType);

            if (entityTypeMapping == null)
            {
                var entitySetMapping =
                    databaseMapping.GetEntitySetMapping(databaseMapping.Model.GetEntitySet(entityType));

                if (entitySetMapping != null)
                {
                    entityTypeMapping = entitySetMapping
                                        .EntityTypeMappings
                                        .First(
                        etm => entityType.DeclaredProperties.All(
                            dp => etm.MappingFragments.First()
                            .ColumnMappings.Select(pm => pm.PropertyPath.First()).Contains(dp)));
                }
            }

            Debug.Assert(entityTypeMapping != null);

            return(entityTypeMapping);
        }
        internal static void ConfigureUnconfiguredType(
            DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest, EntityType entityType)
        {
            var c = new EntityMappingConfiguration();
            var entityTypeMapping
                = databaseMapping.GetEntityTypeMapping(entityType.GetClrType());

            c.Configure(databaseMapping, providerManifest, entityType, ref entityTypeMapping, false, 0, 1);
        }
Exemple #7
0
        internal static void ConfigureUnconfiguredType(
            DbDatabaseMapping databaseMapping,
            ICollection <EntitySet> entitySets,
            DbProviderManifest providerManifest,
            EntityType entityType,
            IDictionary <string, object> commonAnnotations)
        {
            EntityMappingConfiguration mappingConfiguration = new EntityMappingConfiguration();
            EntityTypeMapping          entityTypeMapping    = databaseMapping.GetEntityTypeMapping(EntityTypeExtensions.GetClrType(entityType));

            mappingConfiguration.Configure(databaseMapping, entitySets, providerManifest, entityType, ref entityTypeMapping, false, 0, 1, commonAnnotations);
        }
        internal static void ConfigureUnconfiguredType(
            DbDatabaseMapping databaseMapping,
            DbProviderManifest providerManifest,
            EntityType entityType,
            IDictionary <string, object> commonAnnotations)
        {
            var c = new EntityMappingConfiguration();
            var entityTypeMapping
                = databaseMapping.GetEntityTypeMapping(entityType.GetClrType());

            c.Configure(databaseMapping, providerManifest, entityType, ref entityTypeMapping, false, 0, 1, commonAnnotations);
        }
Exemple #9
0
        private HashSet <EdmPropertyPath> DiscoverAllMappingsToContain(
            DbDatabaseMapping databaseMapping,
            EntityType entityType,
            EntityType toTable,
            bool isSharingTableWithBase)
        {
            HashSet <EdmPropertyPath> mappingsToContain = new HashSet <EdmPropertyPath>();

            entityType.KeyProperties().Each <EdmProperty>((Action <EdmProperty>)(p => mappingsToContain.AddRange <EdmPropertyPath>((IEnumerable <EdmPropertyPath>)p.ToPropertyPathList())));
            if (this.MapInheritedProperties)
            {
                entityType.Properties.Except <EdmProperty>((IEnumerable <EdmProperty>)entityType.DeclaredProperties).Each <EdmProperty>((Action <EdmProperty>)(p => mappingsToContain.AddRange <EdmPropertyPath>((IEnumerable <EdmPropertyPath>)p.ToPropertyPathList())));
            }
            if (isSharingTableWithBase)
            {
                HashSet <EdmPropertyPath> baseMappingsToContain = new HashSet <EdmPropertyPath>();
                EntityType        baseType          = (EntityType)entityType.BaseType;
                EntityTypeMapping entityTypeMapping = (EntityTypeMapping)null;
                MappingFragment   mappingFragment   = (MappingFragment)null;
                for (; baseType != null && entityTypeMapping == null; baseType = (EntityType)baseType.BaseType)
                {
                    entityTypeMapping = databaseMapping.GetEntityTypeMapping((EntityType)entityType.BaseType);
                    if (entityTypeMapping != null)
                    {
                        mappingFragment = entityTypeMapping.MappingFragments.SingleOrDefault <MappingFragment>((Func <MappingFragment, bool>)(tmf => tmf.Table == toTable));
                    }
                    if (mappingFragment == null)
                    {
                        baseType.DeclaredProperties.Each <EdmProperty>((Action <EdmProperty>)(p => baseMappingsToContain.AddRange <EdmPropertyPath>((IEnumerable <EdmPropertyPath>)p.ToPropertyPathList())));
                    }
                }
                if (mappingFragment != null)
                {
                    foreach (ColumnMappingBuilder columnMapping in mappingFragment.ColumnMappings)
                    {
                        mappingsToContain.Add(new EdmPropertyPath((IEnumerable <EdmProperty>)columnMapping.PropertyPath));
                    }
                }
                mappingsToContain.AddRange <EdmPropertyPath>((IEnumerable <EdmPropertyPath>)baseMappingsToContain);
            }
            if (this.Properties == null)
            {
                entityType.DeclaredProperties.Each <EdmProperty>((Action <EdmProperty>)(p => mappingsToContain.AddRange <EdmPropertyPath>((IEnumerable <EdmPropertyPath>)p.ToPropertyPathList())));
            }
            else
            {
                this.Properties.Each <PropertyPath>((Action <PropertyPath>)(p => mappingsToContain.AddRange <EdmPropertyPath>(EntityMappingConfiguration.PropertyPathToEdmPropertyPath(p, entityType))));
            }
            return(mappingsToContain);
        }
Exemple #10
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);
        }
Exemple #11
0
        protected static EntityTypeMapping GetEntityTypeMappingInHierarchy(
            DbDatabaseMapping databaseMapping,
            EntityType entityType)
        {
            EntityTypeMapping entityTypeMapping = databaseMapping.GetEntityTypeMapping(entityType);

            if (entityTypeMapping == null)
            {
                EntitySetMapping entitySetMapping = databaseMapping.GetEntitySetMapping(databaseMapping.Model.GetEntitySet(entityType));
                if (entitySetMapping != null)
                {
                    entityTypeMapping = entitySetMapping.EntityTypeMappings.First <EntityTypeMapping>((Func <EntityTypeMapping, bool>)(etm => entityType.DeclaredProperties.All <EdmProperty>((Func <EdmProperty, bool>)(dp => etm.MappingFragments.First <MappingFragment>().ColumnMappings.Select <ColumnMappingBuilder, EdmProperty>((Func <ColumnMappingBuilder, EdmProperty>)(pm => pm.PropertyPath.First <EdmProperty>())).Contains <EdmProperty>(dp)))));
                }
            }
            return(entityTypeMapping);
        }
Exemple #12
0
        public void GetEntityTypeMapping_should_return_mapping_for_type()
        {
            var databaseMapping = new DbDatabaseMapping()
                                  .Initialize(new EdmModel(DataSpace.CSpace), new EdmModel(DataSpace.SSpace));
            var entityType        = new EntityType("E", "N", DataSpace.CSpace);
            var entityTypeMapping = new StorageEntityTypeMapping(null);

            entityTypeMapping.AddType(entityType);
            databaseMapping.AddEntitySetMapping(
                new EntitySet
            {
                Name = "ES"
            }).AddTypeMapping(entityTypeMapping);

            Assert.Same(entityTypeMapping, databaseMapping.GetEntityTypeMapping(entityType));
        }
Exemple #13
0
        private void ConfigureEntityTypes(
            DbDatabaseMapping databaseMapping,
            ICollection <EntitySet> entitySets,
            DbProviderManifest providerManifest)
        {
            IList <EntityTypeConfiguration> sortedEntityConfigurations = this.SortEntityConfigurationsByInheritance(databaseMapping);

            foreach (EntityTypeConfiguration typeConfiguration in (IEnumerable <EntityTypeConfiguration>)sortedEntityConfigurations)
            {
                EntityTypeMapping entityTypeMapping = databaseMapping.GetEntityTypeMapping(typeConfiguration.ClrType);
                typeConfiguration.ConfigureTablesAndConditions(entityTypeMapping, databaseMapping, entitySets, providerManifest);
                System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.ConfigureUnconfiguredDerivedTypes(databaseMapping, entitySets, providerManifest, databaseMapping.Model.GetEntityType(typeConfiguration.ClrType), sortedEntityConfigurations);
            }
            new EntityMappingService(databaseMapping).Configure();
            foreach (EntityType entityType in databaseMapping.Model.EntityTypes.Where <EntityType>((Func <EntityType, bool>)(e => e.GetConfiguration() != null)))
            {
                ((EntityTypeConfiguration)entityType.GetConfiguration()).Configure(entityType, databaseMapping, providerManifest);
            }
        }
Exemple #14
0
        private MappingFragment FindOrCreateTypeMappingFragment(
            DbDatabaseMapping databaseMapping,
            ref EntityTypeMapping entityTypeMapping,
            int configurationIndex,
            EntityType entityType,
            DbProviderManifest providerManifest)
        {
            if (entityTypeMapping == null)
            {
                new TableMappingGenerator(providerManifest).Generate(entityType, databaseMapping);
                entityTypeMapping  = databaseMapping.GetEntityTypeMapping(entityType);
                configurationIndex = 0;
            }
            MappingFragment mappingFragment;

            if (configurationIndex < entityTypeMapping.MappingFragments.Count)
            {
                mappingFragment = entityTypeMapping.MappingFragments[configurationIndex];
            }
            else
            {
                if (this.MapInheritedProperties)
                {
                    throw Error.EntityMappingConfiguration_DuplicateMapInheritedProperties((object)entityType.Name);
                }
                if (this.Properties == null)
                {
                    throw Error.EntityMappingConfiguration_DuplicateMappedProperties((object)entityType.Name);
                }
                this.Properties.Each <PropertyPath>((Action <PropertyPath>)(p =>
                {
                    if (EntityMappingConfiguration.PropertyPathToEdmPropertyPath(p, entityType).Any <EdmPropertyPath>((Func <EdmPropertyPath, bool>)(pp => !entityType.KeyProperties().Contains <EdmProperty>(pp.First <EdmProperty>()))))
                    {
                        throw Error.EntityMappingConfiguration_DuplicateMappedProperty((object)entityType.Name, (object)p.ToString());
                    }
                }));
                EntityType table       = entityTypeMapping.MappingFragments[0].Table;
                EntityType entityType1 = databaseMapping.Database.AddTable(table.Name, table);
                mappingFragment = EntityMappingOperations.CreateTypeMappingFragment(entityTypeMapping, entityTypeMapping.MappingFragments[0], databaseMapping.Database.GetEntitySet(entityType1));
            }
            return(mappingFragment);
        }
Exemple #15
0
        public void GetEntityTypeMapping_should_return_mapping_for_type_by_clrType()
        {
            var databaseMapping = new DbDatabaseMapping()
                                  .Initialize(new EdmModel(DataSpace.CSpace), new EdmModel(DataSpace.SSpace));
            var entityType = new EntityType("Foo", "N", DataSpace.CSpace);
            var type       = typeof(object);

            entityType.Annotations.SetClrType(type);
            var entityTypeMapping = new StorageEntityTypeMapping(null);

            entityTypeMapping.AddType(entityType);
            entityTypeMapping.SetClrType(typeof(object));
            databaseMapping.AddEntitySetMapping(
                new EntitySet
            {
                Name = "ES"
            }).AddTypeMapping(entityTypeMapping);

            Assert.Same(entityTypeMapping, databaseMapping.GetEntityTypeMapping(typeof(object)));
        }
Exemple #16
0
        private static EntityType FindBaseTableForExtraPropertyMapping(
            DbDatabaseMapping databaseMapping,
            EntityType entityType,
            ColumnMappingBuilder pm)
        {
            EntityType baseType = (EntityType)entityType.BaseType;

            for (MappingFragment mappingFragment = (MappingFragment)null; baseType != null && mappingFragment == null; baseType = (EntityType)baseType.BaseType)
            {
                EntityTypeMapping entityTypeMapping = databaseMapping.GetEntityTypeMapping(baseType);
                if (entityTypeMapping != null)
                {
                    mappingFragment = entityTypeMapping.MappingFragments.SingleOrDefault <MappingFragment>((Func <MappingFragment, bool>)(f => f.ColumnMappings.Any <ColumnMappingBuilder>((Func <ColumnMappingBuilder, bool>)(bpm => bpm.PropertyPath.SequenceEqual <EdmProperty>((IEnumerable <EdmProperty>)pm.PropertyPath)))));
                    if (mappingFragment != null)
                    {
                        return(mappingFragment.Table);
                    }
                }
            }
            return((EntityType)null);
        }
Exemple #17
0
        private static void UpdatePrincipalTables(
            DbDatabaseMapping databaseMapping,
            EntityType toTable,
            bool removeFks,
            AssociationType associationType,
            EntityType et)
        {
            List <AssociationEndMember> associationEndMemberList = new List <AssociationEndMember>();
            AssociationEndMember        principalEnd;
            AssociationEndMember        dependentEnd;

            if (associationType.TryGuessPrincipalAndDependentEnds(out principalEnd, out dependentEnd))
            {
                associationEndMemberList.Add(principalEnd);
            }
            else if (associationType.SourceEnd.RelationshipMultiplicity == RelationshipMultiplicity.Many && associationType.TargetEnd.RelationshipMultiplicity == RelationshipMultiplicity.Many)
            {
                associationEndMemberList.Add(associationType.SourceEnd);
                associationEndMemberList.Add(associationType.TargetEnd);
            }
            else
            {
                associationEndMemberList.Add(associationType.SourceEnd);
            }
            foreach (AssociationEndMember associationEnd in associationEndMemberList)
            {
                if (associationEnd.GetEntityType() == et)
                {
                    IEnumerable <KeyValuePair <EntityType, IEnumerable <EdmProperty> > > keyValuePairs;
                    if (associationType.Constraint != null)
                    {
                        EntityType entityType = associationType.GetOtherEnd(associationEnd).GetEntityType();
                        keyValuePairs = databaseMapping.Model.GetSelfAndAllDerivedTypes(entityType).Select <EntityType, EntityTypeMapping>((Func <EntityType, EntityTypeMapping>)(t => databaseMapping.GetEntityTypeMapping(t))).Where <EntityTypeMapping>((Func <EntityTypeMapping, bool>)(dm => dm != null)).SelectMany <EntityTypeMapping, MappingFragment>((Func <EntityTypeMapping, IEnumerable <MappingFragment> >)(dm => dm.MappingFragments.Where <MappingFragment>((Func <MappingFragment, bool>)(tmf => associationType.Constraint.ToProperties.All <EdmProperty>((Func <EdmProperty, bool>)(p => tmf.ColumnMappings.Any <ColumnMappingBuilder>((Func <ColumnMappingBuilder, bool>)(pm => pm.PropertyPath.First <EdmProperty>() == p)))))))).Distinct <MappingFragment>((Func <MappingFragment, MappingFragment, bool>)((f1, f2) => f1.Table == f2.Table)).Select <MappingFragment, KeyValuePair <EntityType, IEnumerable <EdmProperty> > >((Func <MappingFragment, KeyValuePair <EntityType, IEnumerable <EdmProperty> > >)(df => new KeyValuePair <EntityType, IEnumerable <EdmProperty> >(df.Table, df.ColumnMappings.Where <ColumnMappingBuilder>((Func <ColumnMappingBuilder, bool>)(pm => associationType.Constraint.ToProperties.Contains(pm.PropertyPath.First <EdmProperty>()))).Select <ColumnMappingBuilder, EdmProperty>((Func <ColumnMappingBuilder, EdmProperty>)(pm => pm.ColumnProperty)))));
                    }
                    else
                    {
                        AssociationSetMapping associationSetMapping = databaseMapping.EntityContainerMappings.Single <EntityContainerMapping>().AssociationSetMappings.Single <AssociationSetMapping>((Func <AssociationSetMapping, bool>)(asm => asm.AssociationSet.ElementType == associationType));
                        keyValuePairs = (IEnumerable <KeyValuePair <EntityType, IEnumerable <EdmProperty> > >) new KeyValuePair <EntityType, IEnumerable <EdmProperty> >[1]
                        {
                            new KeyValuePair <EntityType, IEnumerable <EdmProperty> >(associationSetMapping.Table, (associationSetMapping.SourceEndMapping.AssociationEnd == associationEnd ? (IEnumerable <ScalarPropertyMapping>)associationSetMapping.SourceEndMapping.PropertyMappings : (IEnumerable <ScalarPropertyMapping>)associationSetMapping.TargetEndMapping.PropertyMappings).Select <ScalarPropertyMapping, EdmProperty>((Func <ScalarPropertyMapping, EdmProperty>)(pm => pm.Column)))
                        };
                    }
                    foreach (KeyValuePair <EntityType, IEnumerable <EdmProperty> > keyValuePair in keyValuePairs)
                    {
                        KeyValuePair <EntityType, IEnumerable <EdmProperty> > tableInfo = keyValuePair;
                        foreach (ForeignKeyBuilder foreignKeyBuilder in tableInfo.Key.ForeignKeyBuilders.Where <ForeignKeyBuilder>((Func <ForeignKeyBuilder, bool>)(fk => fk.DependentColumns.SequenceEqual <EdmProperty>(tableInfo.Value))).ToArray <ForeignKeyBuilder>())
                        {
                            if (removeFks)
                            {
                                tableInfo.Key.RemoveForeignKey(foreignKeyBuilder);
                            }
                            else if (foreignKeyBuilder.GetAssociationType() == null || foreignKeyBuilder.GetAssociationType() == associationType)
                            {
                                foreignKeyBuilder.PrincipalTable = toTable;
                            }
                        }
                    }
                }
            }
        }
Exemple #18
0
        private HashSet <EdmPropertyPath> DiscoverAllMappingsToContain(
            DbDatabaseMapping databaseMapping, EntityType entityType, EntityType toTable,
            bool isSharingTableWithBase)
        {
            // Ensure all specified properties are the only ones present in this fragment and table
            var mappingsToContain = new HashSet <EdmPropertyPath>();

            // Include Key Properties always
            entityType.KeyProperties().Each(
                p =>
                mappingsToContain.AddRange(p.ToPropertyPathList()));

            // Include All Inherited Properties
            if (MapInheritedProperties)
            {
                entityType.Properties.Except(entityType.DeclaredProperties).Each(
                    p =>
                    mappingsToContain.AddRange(p.ToPropertyPathList()));
            }

            // If sharing table with base type, include all the mappings that the base has
            if (isSharingTableWithBase)
            {
                var baseMappingsToContain = new HashSet <EdmPropertyPath>();
                var baseType = (EntityType)entityType.BaseType;
                EntityTypeMapping baseMapping  = null;
                MappingFragment   baseFragment = null;
                // if the base is abstract it may have no mapping so look upwards until you find either:
                //   1. a type with mappings and
                //   2. if none can be found (abstract until the root or hit another table), then include all declared properties on that base type
                while (baseType != null &&
                       baseMapping == null)
                {
                    baseMapping = databaseMapping.GetEntityTypeMapping((EntityType)entityType.BaseType);
                    if (baseMapping != null)
                    {
                        baseFragment = baseMapping.MappingFragments.SingleOrDefault(tmf => tmf.Table == toTable);
                    }

                    if (baseFragment == null)
                    {
                        baseType.DeclaredProperties.Each(
                            p =>
                            baseMappingsToContain.AddRange(p.ToPropertyPathList()));
                    }

                    baseType = (EntityType)baseType.BaseType;
                }

                if (baseFragment != null)
                {
                    foreach (var pm in baseFragment.ColumnMappings)
                    {
                        mappingsToContain.Add(new EdmPropertyPath(pm.PropertyPath));
                    }
                }

                mappingsToContain.AddRange(baseMappingsToContain);
            }

            if (Properties == null)
            {
                // Include All Declared Properties
                entityType.DeclaredProperties.Each(
                    p =>
                    mappingsToContain.AddRange(p.ToPropertyPathList()));
            }
            else
            {
                // Include Specific Properties
                Properties.Each(
                    p =>
                    mappingsToContain.AddRange(PropertyPathToEdmPropertyPath(p, entityType)));
            }

            return(mappingsToContain);
        }
Exemple #19
0
        private MappingFragment FindOrCreateTypeMappingFragment(
            DbDatabaseMapping databaseMapping,
            ref EntityTypeMapping entityTypeMapping,
            int configurationIndex,
            EntityType entityType,
            DbProviderManifest providerManifest)
        {
            MappingFragment fragment = null;

            if (entityTypeMapping == null)
            {
                Debug.Assert(entityType.Abstract);
                new TableMappingGenerator(providerManifest).
                Generate(entityType, databaseMapping);
                entityTypeMapping  = databaseMapping.GetEntityTypeMapping(entityType);
                configurationIndex = 0;
            }

            if (configurationIndex < entityTypeMapping.MappingFragments.Count)
            {
                fragment = entityTypeMapping.MappingFragments[configurationIndex];
            }
            else
            {
                if (MapInheritedProperties)
                {
                    throw Error.EntityMappingConfiguration_DuplicateMapInheritedProperties(entityType.Name);
                }
                else if (Properties == null)
                {
                    throw Error.EntityMappingConfiguration_DuplicateMappedProperties(entityType.Name);
                }
                else
                {
                    Properties.Each(
                        p =>
                    {
                        if (
                            PropertyPathToEdmPropertyPath(p, entityType).Any(
                                pp => !entityType.KeyProperties().Contains(pp.First())))
                        {
                            throw Error.EntityMappingConfiguration_DuplicateMappedProperty(
                                entityType.Name, p.ToString());
                        }
                    });
                }

                // Special case where they've asked for an extra table related to this type that only will include the PK columns
                // Uniquify: can be false, always move to a new table
                var templateTable = entityTypeMapping.MappingFragments[0].Table;

                var table = databaseMapping.Database.AddTable(templateTable.Name, templateTable);

                fragment
                    = EntityMappingOperations.CreateTypeMappingFragment(
                          entityTypeMapping,
                          entityTypeMapping.MappingFragments[0],
                          databaseMapping.Database.GetEntitySet(table));
            }
            return(fragment);
        }
        private static void UpdatePrincipalTables(
            DbDatabaseMapping databaseMapping, EntityType toTable, bool removeFks,
            AssociationType associationType, EntityType et)
        {
            AssociationEndMember principalEnd, dependentEnd;
            var endsToCheck = new List <AssociationEndMember>();

            if (associationType.TryGuessPrincipalAndDependentEnds(out principalEnd, out dependentEnd))
            {
                endsToCheck.Add(principalEnd);
            }
            else if (associationType.SourceEnd.RelationshipMultiplicity == RelationshipMultiplicity.Many &&
                     associationType.TargetEnd.RelationshipMultiplicity == RelationshipMultiplicity.Many)
            {
                // many to many consider both ends
                endsToCheck.Add(associationType.SourceEnd);
                endsToCheck.Add(associationType.TargetEnd);
            }
            else
            {
                // 1:1 and 0..1:0..1
                endsToCheck.Add(associationType.SourceEnd);
            }

            foreach (var end in endsToCheck)
            {
                if (end.GetEntityType() == et)
                {
                    IEnumerable <KeyValuePair <EntityType, IEnumerable <EdmProperty> > > dependentTableInfos;
                    if (associationType.Constraint != null)
                    {
                        var originalDependentType = associationType.GetOtherEnd(end).GetEntityType();
                        var allDependentTypes     = databaseMapping.Model.GetSelfAndAllDerivedTypes(originalDependentType);

                        dependentTableInfos =
                            allDependentTypes.Select(t => databaseMapping.GetEntityTypeMapping(t)).Where(
                                dm => dm != null)
                            .SelectMany(
                                dm => dm.MappingFragments
                                .Where(
                                    tmf => associationType.Constraint.ToProperties
                                    .All(
                                        p =>
                                        tmf.ColumnMappings.Any(
                                            pm => pm.PropertyPath.First() == p))))
                            .Distinct((f1, f2) => f1.Table == f2.Table)
                            .Select(
                                df =>
                                new KeyValuePair <EntityType, IEnumerable <EdmProperty> >(
                                    df.Table,
                                    df.ColumnMappings.Where(
                                        pm =>
                                        associationType.Constraint.ToProperties.Contains(
                                            pm.PropertyPath.First())).Select(
                                        pm => pm.ColumnProperty)));
                    }
                    else
                    {
                        // IA
                        var associationSetMapping =
                            databaseMapping.EntityContainerMappings
                            .Single().AssociationSetMappings
                            .Single(asm => asm.AssociationSet.ElementType == associationType);

                        var dependentTable   = associationSetMapping.Table;
                        var propertyMappings = associationSetMapping.SourceEndMapping.EndMember == end
                                                   ? associationSetMapping.SourceEndMapping.PropertyMappings
                                                   : associationSetMapping.TargetEndMapping.PropertyMappings;
                        var dependentColumns = propertyMappings.Select(pm => pm.ColumnProperty);

                        dependentTableInfos = new[]
                        {
                            new KeyValuePair
                            <EntityType, IEnumerable <EdmProperty> >(
                                dependentTable, dependentColumns)
                        };
                    }

                    foreach (var tableInfo in dependentTableInfos)
                    {
                        foreach (
                            var fk in
                            tableInfo.Key.ForeignKeyBuilders.Where(
                                fk => fk.DependentColumns.SequenceEqual(tableInfo.Value)).ToArray(

                                ))
                        {
                            if (removeFks)
                            {
                                tableInfo.Key.RemoveForeignKey(fk);
                            }
                            else
                            {
                                fk.PrincipalTable = toTable;
                            }
                        }
                    }
                }
            }
        }
        protected static DbEntityTypeMapping GetEntityTypeMappingInHierarchy(
            DbDatabaseMapping databaseMapping, EdmEntityType entityType)
        {
            Contract.Requires(databaseMapping != null);
            Contract.Requires(entityType != null);

            var entityTypeMapping = databaseMapping.GetEntityTypeMapping(entityType);

            if (entityTypeMapping == null)
            {
                var entitySetMapping =
                    databaseMapping.GetEntitySetMapping(databaseMapping.Model.GetEntitySet(entityType));

                if (entitySetMapping != null)
                {
                    entityTypeMapping = entitySetMapping
                        .EntityTypeMappings
                        .First(
                            etm => entityType.DeclaredProperties.All(
                                dp => etm.TypeMappingFragments.First()
                                          .PropertyMappings.Select(pm => pm.PropertyPath.First()).Contains(dp)));
                }
            }

            if (entityTypeMapping == null)
            {
                throw Error.UnmappedAbstractType(entityType.GetClrType());
            }

            return entityTypeMapping;
        }