private static void CopyForeignKeyConstraint(EdmModel database, EntityType toTable, ForeignKeyBuilder fk,
                                                     Func <EdmProperty, EdmProperty> selector = null)
        {
            DebugCheck.NotNull(toTable);
            DebugCheck.NotNull(fk);

            var newFk
                = new ForeignKeyBuilder(
                      database,
                      database.EntityTypes.SelectMany(t => t.ForeignKeyBuilders).UniquifyName(fk.Name))
                {
                PrincipalTable = fk.PrincipalTable,
                DeleteAction   = fk.DeleteAction
                };

            newFk.SetPreferredName(fk.Name);

            var dependentColumns =
                GetDependentColumns(
                    selector != null
                        ? fk.DependentColumns.Select(selector)
                        : fk.DependentColumns,
                    toTable.Properties);

            if (!ContainsEquivalentForeignKey(toTable, newFk.PrincipalTable, dependentColumns))
            {
                toTable.AddForeignKey(newFk);

                newFk.DependentColumns = dependentColumns;
            }
        }
Пример #2
0
        private static void CopyForeignKeyConstraint(
            EdmModel database,
            EntityType toTable,
            ForeignKeyBuilder fk,
            Func <EdmProperty, EdmProperty> selector = null)
        {
            ForeignKeyBuilder foreignKeyBuilder = new ForeignKeyBuilder(database, ((IEnumerable <INamedDataModelItem>)database.EntityTypes.SelectMany <EntityType, ForeignKeyBuilder>((Func <EntityType, IEnumerable <ForeignKeyBuilder> >)(t => t.ForeignKeyBuilders))).UniquifyName(fk.Name))
            {
                PrincipalTable = fk.PrincipalTable,
                DeleteAction   = fk.DeleteAction
            };

            foreignKeyBuilder.SetPreferredName(fk.Name);
            IList <EdmProperty> dependentColumns = ForeignKeyPrimitiveOperations.GetDependentColumns(selector != null ? fk.DependentColumns.Select <EdmProperty, EdmProperty>(selector) : fk.DependentColumns, (IEnumerable <EdmProperty>)toTable.Properties);

            if (ForeignKeyPrimitiveOperations.ContainsEquivalentForeignKey(toTable, foreignKeyBuilder.PrincipalTable, (IEnumerable <EdmProperty>)dependentColumns))
            {
                return;
            }
            toTable.AddForeignKey(foreignKeyBuilder);
            foreignKeyBuilder.DependentColumns = (IEnumerable <EdmProperty>)dependentColumns;
        }