internal static void CleanupUnmappedArtifacts(DbDatabaseMapping databaseMapping, EntityType table)
        {
            var associationMappings = databaseMapping.EntityContainerMappings
                                      .SelectMany(ecm => ecm.AssociationSetMappings)
                                      .Where(asm => asm.Table == table)
                                      .ToArray();

            var entityFragments = databaseMapping.EntityContainerMappings
                                  .SelectMany(ecm => ecm.EntitySetMappings)
                                  .SelectMany(esm => esm.EntityTypeMappings)
                                  .SelectMany(etm => etm.MappingFragments).Where(f => f.Table == table).ToArray();

            if (!associationMappings.Any() &&
                !entityFragments.Any())
            {
                databaseMapping.Database.RemoveEntityType(table);

                databaseMapping.Database.AssociationTypes
                .Where(t => t.SourceEnd.GetEntityType() == table ||
                       t.TargetEnd.GetEntityType() == table)
                .ToArray()
                .Each(t => databaseMapping.Database.RemoveAssociationType(t));
            }
            else
            {
                // check the columns of table to see if they are actually used in any fragment
                foreach (var column in table.Properties.ToArray())
                {
                    if (entityFragments.SelectMany(f => f.ColumnMappings).All(pm => pm.ColumnProperty != column)
                        &&
                        entityFragments.SelectMany(f => f.ColumnConditions).All(cc => cc.ColumnProperty != column)
                        &&
                        associationMappings.SelectMany(am => am.SourceEndMapping.PropertyMappings).All(pm => pm.ColumnProperty != column) &&
                        associationMappings.SelectMany(am => am.SourceEndMapping.PropertyMappings).All(pm => pm.ColumnProperty != column))
                    {
                        // Remove table FKs that refer to this column, and then remove the column
                        ForeignKeyPrimitiveOperations.RemoveAllForeignKeyConstraintsForColumn(table, column);
                        TablePrimitiveOperations.RemoveColumn(table, column);
                    }
                }

                // Remove FKs where Principal Table == Dependent Table and the PK == FK (redundant)
                table.ForeignKeyBuilders
                .Where(fk => fk.PrincipalTable == table && fk.DependentColumns.SequenceEqual(table.KeyProperties))
                .ToArray()
                .Each(table.RemoveForeignKey);
            }
        }
예제 #2
0
 internal static void CleanupUnmappedArtifacts(
     DbDatabaseMapping databaseMapping,
     EntityType table)
 {
     AssociationSetMapping[] array1 = databaseMapping.EntityContainerMappings.SelectMany <EntityContainerMapping, AssociationSetMapping>((Func <EntityContainerMapping, IEnumerable <AssociationSetMapping> >)(ecm => ecm.AssociationSetMappings)).Where <AssociationSetMapping>((Func <AssociationSetMapping, bool>)(asm => asm.Table == table)).ToArray <AssociationSetMapping>();
     MappingFragment[]       array2 = databaseMapping.EntityContainerMappings.SelectMany <EntityContainerMapping, EntitySetMapping>((Func <EntityContainerMapping, IEnumerable <EntitySetMapping> >)(ecm => ecm.EntitySetMappings)).SelectMany <EntitySetMapping, EntityTypeMapping>((Func <EntitySetMapping, IEnumerable <EntityTypeMapping> >)(esm => (IEnumerable <EntityTypeMapping>)esm.EntityTypeMappings)).SelectMany <EntityTypeMapping, MappingFragment>((Func <EntityTypeMapping, IEnumerable <MappingFragment> >)(etm => (IEnumerable <MappingFragment>)etm.MappingFragments)).Where <MappingFragment>((Func <MappingFragment, bool>)(f => f.Table == table)).ToArray <MappingFragment>();
     if (!((IEnumerable <AssociationSetMapping>)array1).Any <AssociationSetMapping>() && !((IEnumerable <MappingFragment>)array2).Any <MappingFragment>())
     {
         databaseMapping.Database.RemoveEntityType(table);
         ((IEnumerable <AssociationType>)databaseMapping.Database.AssociationTypes.Where <AssociationType>((Func <AssociationType, bool>)(t =>
         {
             if (t.SourceEnd.GetEntityType() != table)
             {
                 return(t.TargetEnd.GetEntityType() == table);
             }
             return(true);
         })).ToArray <AssociationType>()).Each <AssociationType>((Action <AssociationType>)(t => databaseMapping.Database.RemoveAssociationType(t)));
     }
     else
     {
         foreach (EdmProperty edmProperty in table.Properties.ToArray <EdmProperty>())
         {
             EdmProperty column = edmProperty;
             if (((IEnumerable <MappingFragment>)array2).SelectMany <MappingFragment, ColumnMappingBuilder>((Func <MappingFragment, IEnumerable <ColumnMappingBuilder> >)(f => f.ColumnMappings)).All <ColumnMappingBuilder>((Func <ColumnMappingBuilder, bool>)(pm => pm.ColumnProperty != column)) && ((IEnumerable <MappingFragment>)array2).SelectMany <MappingFragment, ConditionPropertyMapping>((Func <MappingFragment, IEnumerable <ConditionPropertyMapping> >)(f => f.ColumnConditions)).All <ConditionPropertyMapping>((Func <ConditionPropertyMapping, bool>)(cc => cc.Column != column)) && (((IEnumerable <AssociationSetMapping>)array1).SelectMany <AssociationSetMapping, ScalarPropertyMapping>((Func <AssociationSetMapping, IEnumerable <ScalarPropertyMapping> >)(am => (IEnumerable <ScalarPropertyMapping>)am.SourceEndMapping.PropertyMappings)).All <ScalarPropertyMapping>((Func <ScalarPropertyMapping, bool>)(pm => pm.Column != column)) && ((IEnumerable <AssociationSetMapping>)array1).SelectMany <AssociationSetMapping, ScalarPropertyMapping>((Func <AssociationSetMapping, IEnumerable <ScalarPropertyMapping> >)(am => (IEnumerable <ScalarPropertyMapping>)am.SourceEndMapping.PropertyMappings)).All <ScalarPropertyMapping>((Func <ScalarPropertyMapping, bool>)(pm => pm.Column != column))))
             {
                 ForeignKeyPrimitiveOperations.RemoveAllForeignKeyConstraintsForColumn(table, column, databaseMapping);
                 TablePrimitiveOperations.RemoveColumn(table, column);
             }
         }
         ((IEnumerable <ForeignKeyBuilder>)table.ForeignKeyBuilders.Where <ForeignKeyBuilder>((Func <ForeignKeyBuilder, bool>)(fk =>
         {
             if (fk.PrincipalTable == table)
             {
                 return(fk.DependentColumns.SequenceEqual <EdmProperty>((IEnumerable <EdmProperty>)table.KeyProperties));
             }
             return(false);
         })).ToArray <ForeignKeyBuilder>()).Each <ForeignKeyBuilder>(new Action <ForeignKeyBuilder>(table.RemoveForeignKey));
     }
 }