public static EdmProperty CopyColumnAndAnyConstraints(
            EdmModel database,
            EntityType fromTable,
            EntityType toTable,
            EdmProperty column,
            bool useExisting,
            bool allowPkConstraintCopy)
        {
            DebugCheck.NotNull(fromTable);
            DebugCheck.NotNull(toTable);
            DebugCheck.NotNull(column);

            var movedColumn = column;

            if (fromTable != toTable)
            {
                movedColumn = TablePrimitiveOperations.IncludeColumn(toTable, column, useExisting);
                if (allowPkConstraintCopy || !movedColumn.IsPrimaryKeyColumn)
                {
                    ForeignKeyPrimitiveOperations.CopyAllForeignKeyConstraintsForColumn(
                        database, fromTable, toTable, column);
                }
            }

            return(movedColumn);
        }
示例#2
0
        public static void CopyPropertyMappingToFragment(
            ColumnMappingBuilder propertyMappingBuilder,
            MappingFragment fragment,
            Func <EdmProperty, bool> isCompatible,
            bool useExisting)
        {
            EdmProperty columnProperty = TablePrimitiveOperations.IncludeColumn(fragment.Table, propertyMappingBuilder.ColumnProperty, isCompatible, useExisting);

            fragment.AddColumnMapping(new ColumnMappingBuilder(columnProperty, propertyMappingBuilder.PropertyPath));
        }
        public static void CopyPropertyMappingToFragment(
            ColumnMappingBuilder propertyMappingBuilder, StorageMappingFragment fragment, bool useExisting)
        {
            // Ensure column is in the fragment's table
            var column = TablePrimitiveOperations.IncludeColumn(fragment.Table, propertyMappingBuilder.ColumnProperty, useExisting);

            // Add the property mapping
            fragment.AddColumnMapping(
                new ColumnMappingBuilder(column, propertyMappingBuilder.PropertyPath));
        }
示例#4
0
        public static EdmProperty IncludeColumn(
            EntityType table,
            EdmProperty templateColumn,
            Func <EdmProperty, bool> isCompatible,
            bool useExisting)
        {
            EdmProperty edmProperty = table.Properties.FirstOrDefault <EdmProperty>(isCompatible);

            templateColumn = edmProperty != null ? (useExisting || edmProperty.IsPrimaryKeyColumn ? edmProperty : templateColumn.Clone()) : templateColumn.Clone();
            TablePrimitiveOperations.AddColumn(table, templateColumn);
            return(templateColumn);
        }
        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);
            }
        }
示例#6
0
        public static EdmProperty MoveColumnAndAnyConstraints(
            EntityType fromTable,
            EntityType toTable,
            EdmProperty column,
            bool useExisting)
        {
            EdmProperty edmProperty = column;

            if (fromTable != toTable)
            {
                edmProperty = TablePrimitiveOperations.IncludeColumn(toTable, column, TablePrimitiveOperations.GetNameMatcher(column.Name), useExisting);
                TablePrimitiveOperations.RemoveColumn(fromTable, column);
                ForeignKeyPrimitiveOperations.MoveAllForeignKeyConstraintsForColumn(fromTable, toTable, column);
            }
            return(edmProperty);
        }
 public static void UpdateConditions(
     EdmModel database, EntityType fromTable, MappingFragment fragment)
 {
     // move the condition's column from the formTable to the table in fragment
     if (fromTable != fragment.Table)
     {
         fragment.ColumnConditions.Each(
             cc =>
         {
             cc.Column
                 = TableOperations.CopyColumnAndAnyConstraints(
                       database, fromTable, fragment.Table, cc.Column,
                       TablePrimitiveOperations.GetNameMatcher(cc.Column.Name),
                       useExisting: true);
         });
     }
 }
        public static MappingFragment CreateTypeMappingFragment(
            EntityTypeMapping entityTypeMapping, MappingFragment templateFragment, EntitySet tableSet)
        {
            var fragment = new MappingFragment(tableSet, entityTypeMapping, false);

            entityTypeMapping.AddFragment(fragment);

            // Move all PK mappings to the extra fragment
            foreach (
                var pkPropertyMapping in templateFragment.ColumnMappings.Where(pm => pm.ColumnProperty.IsPrimaryKeyColumn))
            {
                CopyPropertyMappingToFragment(
                    pkPropertyMapping, fragment, TablePrimitiveOperations.GetNameMatcher(pkPropertyMapping.ColumnProperty.Name),
                    useExisting: true);
            }
            return(fragment);
        }
        public static EdmProperty MoveColumnAndAnyConstraints(
            EntityType fromTable, EntityType toTable, EdmProperty column, bool useExisting)
        {
            DebugCheck.NotNull(fromTable);
            DebugCheck.NotNull(toTable);
            DebugCheck.NotNull(column);

            var movedColumn = column;

            if (fromTable != toTable)
            {
                movedColumn = TablePrimitiveOperations.IncludeColumn(toTable, column, useExisting);
                TablePrimitiveOperations.RemoveColumn(fromTable, column);
                ForeignKeyPrimitiveOperations.MoveAllForeignKeyConstraintsForColumn(fromTable, toTable, column);
            }

            return(movedColumn);
        }
示例#10
0
        public static EdmProperty CopyColumnAndAnyConstraints(
            EdmModel database,
            EntityType fromTable,
            EntityType toTable,
            EdmProperty column,
            Func <EdmProperty, bool> isCompatible,
            bool useExisting)
        {
            EdmProperty movedColumn = column;

            if (fromTable != toTable)
            {
                movedColumn = TablePrimitiveOperations.IncludeColumn(toTable, column, isCompatible, useExisting);
                if (!movedColumn.IsPrimaryKeyColumn)
                {
                    ForeignKeyPrimitiveOperations.CopyAllForeignKeyConstraintsForColumn(database, fromTable, toTable, column, movedColumn);
                }
            }
            return(movedColumn);
        }
示例#11
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));
     }
 }
示例#12
0
 public static void UpdateConditions(
     EdmModel database,
     EntityType fromTable,
     MappingFragment fragment)
 {
     if (fromTable == fragment.Table)
     {
         return;
     }
     fragment.ColumnConditions.Each <ConditionPropertyMapping>((Action <ConditionPropertyMapping>)(cc => cc.Column = TableOperations.CopyColumnAndAnyConstraints(database, fromTable, fragment.Table, cc.Column, TablePrimitiveOperations.GetNameMatcher(cc.Column.Name), true)));
 }
示例#13
0
        public static MappingFragment CreateTypeMappingFragment(
            EntityTypeMapping entityTypeMapping,
            MappingFragment templateFragment,
            EntitySet tableSet)
        {
            MappingFragment fragment = new MappingFragment(tableSet, (TypeMapping)entityTypeMapping, false);

            entityTypeMapping.AddFragment(fragment);
            foreach (ColumnMappingBuilder propertyMappingBuilder in templateFragment.ColumnMappings.Where <ColumnMappingBuilder>((Func <ColumnMappingBuilder, bool>)(pm => pm.ColumnProperty.IsPrimaryKeyColumn)))
            {
                EntityMappingOperations.CopyPropertyMappingToFragment(propertyMappingBuilder, fragment, TablePrimitiveOperations.GetNameMatcher(propertyMappingBuilder.ColumnProperty.Name), true);
            }
            return(fragment);
        }