private static void GenerateDiscriminators(DbDatabaseMapping databaseMapping)
        {
            DebugCheck.NotNull(databaseMapping);

            foreach (var entitySetMapping in databaseMapping.GetEntitySetMappings())
            {
                if (entitySetMapping.EntityTypeMappings.Count() <= 1)
                {
                    continue;
                }

                var typeUsage
                    = databaseMapping.ProviderManifest.GetStoreType(DiscriminatorTypeUsage);

                var discriminatorColumn
                    = new EdmProperty(DiscriminatorColumnName, typeUsage)
                        {
                            Nullable = false,
                            DefaultValue = "(Undefined)"
                        };

                entitySetMapping
                    .EntityTypeMappings
                    .First()
                    .MappingFragments
                    .Single()
                    .Table
                    .AddColumn(discriminatorColumn);

                foreach (var entityTypeMapping in entitySetMapping.EntityTypeMappings)
                {
                    // Abstract classes don't need a discriminator as they won't be directly materialized
                    if (entityTypeMapping.EntityType.Abstract)
                    {
                        continue;
                    }

                    var entityTypeMappingFragment = entityTypeMapping.MappingFragments.Single();

                    entityTypeMappingFragment.SetDefaultDiscriminator(discriminatorColumn);

                    entityTypeMappingFragment
                        .AddDiscriminatorCondition(discriminatorColumn, entityTypeMapping.EntityType.Name);
                }
            }
        }
        private void GenerateDiscriminators(DbDatabaseMapping databaseMapping)
        {
            DebugCheck.NotNull(databaseMapping);

            foreach (var entitySetMapping in databaseMapping.GetEntitySetMappings())
            {
                if (entitySetMapping.EntityTypeMappings.Count() <= 1)
                {
                    continue;
                }

                var typeUsage
                    = _providerManifest.GetStoreType(DiscriminatorTypeUsage);

                var discriminatorColumn
                    = new EdmProperty(DiscriminatorColumnName, typeUsage)
                        {
                            Nullable = false
                        };

                entitySetMapping
                    .EntityTypeMappings
                    .First()
                    .MappingFragments
                    .Single()
                    .Table
                    .AddColumn(discriminatorColumn);

                foreach (var entityTypeMapping in entitySetMapping.EntityTypeMappings)
                {
                    var entityTypeMappingFragment = entityTypeMapping.MappingFragments.Single();

                    entityTypeMappingFragment.SetDefaultDiscriminator(discriminatorColumn);

                    entityTypeMappingFragment
                        .AddDiscriminatorCondition(discriminatorColumn, entityTypeMapping.EntityType.Name);
                }
            }
        }
Esempio n. 3
0
        private static void RemoveRedundantTables(DbDatabaseMapping databaseMapping)
        {
            DebugCheck.NotNull(databaseMapping);

            var tables
                = (from t in databaseMapping.Database.GetEntityTypes()
                   where databaseMapping.GetEntitySetMappings()
                                        .SelectMany(esm => esm.EntityTypeMappings)
                                        .SelectMany(etm => etm.MappingFragments).All(etmf => etmf.Table != t)
                         && databaseMapping.GetAssociationSetMappings().All(asm => asm.Table != t)
                   select t).ToList();

            tables.Each(
                t =>
                    {
                        var tableName = t.GetTableName();

                        if (tableName != null)
                        {
                            throw Error.OrphanedConfiguredTableDetected(tableName);
                        }

                        databaseMapping.Database.RemoveEntityType(t);
                    });
        }
Esempio n. 4
0
        private static void RemoveRedundantColumnConditions(DbDatabaseMapping databaseMapping)
        {
            DebugCheck.NotNull(databaseMapping);

            // Remove all the default discriminators where there is only one table using it
            (from esm in databaseMapping.GetEntitySetMappings()
             select new
                        {
                            Set = esm,
                            Fragments =
                 (from etm in esm.EntityTypeMappings
                  from etmf in etm.MappingFragments
                  group etmf by etmf.Table
                  into g
                  where g.Count(x => x.GetDefaultDiscriminator() != null) == 1
                  select g.Single(x => x.GetDefaultDiscriminator() != null))
                        })
                .Each(x => x.Fragments.Each(f => f.RemoveDefaultDiscriminator(x.Set)));
        }
        private void ConfigureModificationFunctions(
            DbDatabaseMapping databaseMapping, EntityType entityType, DbProviderManifest providerManifest)
        {
            DebugCheck.NotNull(entityType);
            DebugCheck.NotNull(databaseMapping);
            DebugCheck.NotNull(providerManifest);

            if (_modificationFunctionsConfiguration != null)
            {
                new ModificationFunctionMappingGenerator(providerManifest)
                    .Generate(entityType, databaseMapping);

                var modificationFunctionMapping
                    = databaseMapping.GetEntitySetMappings()
                        .SelectMany(esm => esm.ModificationFunctionMappings)
                        .SingleOrDefault(mfm => mfm.EntityType == entityType);

                if (modificationFunctionMapping != null)
                {
                    _modificationFunctionsConfiguration.Configure(modificationFunctionMapping, providerManifest);
                }
            }
        }
        internal void ConfigureFunctionParameters(DbDatabaseMapping databaseMapping, EntityType entityType)
        {
            DebugCheck.NotNull(databaseMapping);
            DebugCheck.NotNull(entityType);

            var parameterBindings
                = (from esm in databaseMapping.GetEntitySetMappings()
                   from mfm in esm.ModificationFunctionMappings
                   where mfm.EntityType == entityType
                   from pb in mfm.PrimaryParameterBindings
                   select pb)
                    .ToList();

            ConfigureFunctionParameters(parameterBindings);

            foreach (var derivedEntityType in databaseMapping.Model.EntityTypes.Where(et => et.BaseType == entityType))
            {
                ConfigureFunctionParameters(databaseMapping, derivedEntityType);
            }
        }
        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);
            }
        }
Esempio n. 8
0
 private static void RemoveDuplicateTphColumns(DbDatabaseMapping databaseMapping)
 {
     foreach (var table in databaseMapping.Database.EntityTypes)
     {
         var currentTable = table; // Prevent access to foreach variable in closure
         new TphColumnFixer(
             databaseMapping
                 .GetEntitySetMappings()
                 .SelectMany(e => e.EntityTypeMappings)
                 .SelectMany(e => e.MappingFragments)
                 .Where(f => f.Table == currentTable)
                 .SelectMany(f => f.ColumnMappings),
             currentTable,
             databaseMapping.Database).RemoveDuplicateTphColumns();
     }
 }
Esempio n. 9
0
        private static void UniquifyFunctionNames(DbDatabaseMapping databaseMapping)
        {
            DebugCheck.NotNull(databaseMapping);

            foreach (var modificationStoredProcedureMapping
                in databaseMapping
                    .GetEntitySetMappings()
                    .SelectMany(esm => esm.ModificationFunctionMappings))
            {
                var entityTypeConfiguration
                    = (EntityTypeConfiguration)modificationStoredProcedureMapping.EntityType.GetConfiguration();

                if (entityTypeConfiguration.ModificationStoredProceduresConfiguration == null)
                {
                    continue;
                }

                var modificationStoredProceduresConfiguration
                    = entityTypeConfiguration.ModificationStoredProceduresConfiguration;

                UniquifyFunctionName(
                    databaseMapping,
                    modificationStoredProceduresConfiguration.InsertModificationStoredProcedureConfiguration,
                    modificationStoredProcedureMapping.InsertFunctionMapping);

                UniquifyFunctionName(
                    databaseMapping,
                    modificationStoredProceduresConfiguration.UpdateModificationStoredProcedureConfiguration,
                    modificationStoredProcedureMapping.UpdateFunctionMapping);

                UniquifyFunctionName(
                    databaseMapping,
                    modificationStoredProceduresConfiguration.DeleteModificationStoredProcedureConfiguration,
                    modificationStoredProcedureMapping.DeleteFunctionMapping);
            }

            foreach (var modificationStoredProcedureMapping
                in databaseMapping
                    .GetAssociationSetMappings()
                    .Select(asm => asm.ModificationFunctionMapping)
                    .Where(asm => asm != null))
            {
                var navigationPropertyConfiguration
                    = (NavigationPropertyConfiguration)modificationStoredProcedureMapping
                                                           .AssociationSet.ElementType.GetConfiguration();

                if (navigationPropertyConfiguration.ModificationStoredProceduresConfiguration == null)
                {
                    continue;
                }

                UniquifyFunctionName(
                    databaseMapping,
                    navigationPropertyConfiguration.ModificationStoredProceduresConfiguration.InsertModificationStoredProcedureConfiguration,
                    modificationStoredProcedureMapping.InsertFunctionMapping);

                UniquifyFunctionName(
                    databaseMapping,
                    navigationPropertyConfiguration.ModificationStoredProceduresConfiguration.DeleteModificationStoredProcedureConfiguration,
                    modificationStoredProcedureMapping.DeleteFunctionMapping);
            }
        }