/// <summary>
        /// Executes an action for each element in the collection transactionally and showing the progress in the Console.
        /// Also it disables identity behaviour in the Schema and in Sql Server.
        /// <param name="action">Use LogWriter to write in the Console and the file at the same time</param>
        /// </summary>
        public static void ProgressForeachDisableIdentity <T>(this IEnumerable <T> collection, Type tableType, Func <T, string> elementID, string fileName, Action <T, LogWriter> action)
        {
            Table table = Schema.Current.Table(tableType);

            if (!table.IdentityBehaviour)
            {
                throw new InvalidOperationException("Identity is false already");
            }

            table.IdentityBehaviour = false;
            try
            {
                collection.ProgressForeach(elementID, fileName, (item, writer) =>
                {
                    using (table.PrimaryKey.Default != null ? null: Administrator.DisableIdentity(table.Name))
                        action(item, writer);
                });
            }
            finally
            {
                table.IdentityBehaviour = true;
                if (!Console.IsOutputRedirected)
                {
                    SafeConsole.ClearSameLine();
                }
            }
        }
 public static void SaveListDisableIdentity <T>(IEnumerable <T> entities)
     where T : Entity
 {
     using (Transaction tr = new Transaction())
         using (Administrator.DisableIdentity <T>())
         {
             Database.SaveList(entities);
             tr.Commit();
         }
 }
        /// <summary>
        /// Executes an action for each element in the collection transactionally and showing the progress in the Console
        /// </summary>
        public static void ProgressForeach <T>(this IEnumerable <T> collection,
                                               Func <T, string>?elementID      = null,
                                               Action <T>?action               = null,
                                               bool transactional              = true,
                                               bool showProgress               = true,
                                               LogWriter?writer                = null,
                                               ParallelOptions?parallelOptions = null,
                                               Type?disableIdentityFor         = null)
        {
            if (action == null)
            {
                throw new InvalidOperationException("no action specified");
            }

            if (elementID == null)
            {
                elementID = e => e !.ToString() !;
            }
            if (writer == null)
            {
                writer = GetConsoleWriter();
            }


            if (disableIdentityFor == null)
            {
                collection.ProgressForeachInternal(elementID, writer, parallelOptions, transactional, showProgress, action);
            }
            else
            {
                if (!transactional)
                {
                    throw new InvalidOperationException("disableIdentity has to be transactional");
                }

                Table table = Schema.Current.Table(disableIdentityFor);

                collection.ProgressForeachInternal(elementID, writer, parallelOptions, transactional, showProgress, action: item =>
                {
                    using (Transaction tr = Transaction.ForceNew())
                    {
                        using (table.PrimaryKey.Default != null ? null : Administrator.DisableIdentity(table))
                            action !(item);
                        tr.Commit();
                    }
                });
            }
        }
Exemple #4
0
        public static int BulkInsertTable <T>(IEnumerable <T> entities,
                                              SqlBulkCopyOptions copyOptions = SqlBulkCopyOptions.Default,
                                              bool preSaving       = true,
                                              bool validateFirst   = true,
                                              bool disableIdentity = false,
                                              int?timeout          = null,
                                              string?message       = null)
            where T : Entity
        {
            using (HeavyProfiler.Log(nameof(BulkInsertTable), () => typeof(T).TypeName()))
            {
                if (message != null)
                {
                    return(SafeConsole.WaitRows(message == "auto" ? $"BulkInsering {entities.Count()} {typeof(T).TypeName()}" : message,
                                                () => BulkInsertTable(entities, copyOptions, preSaving, validateFirst, disableIdentity, timeout, message: null)));
                }

                if (disableIdentity)
                {
                    copyOptions |= SqlBulkCopyOptions.KeepIdentity;
                }

                if (copyOptions.HasFlag(SqlBulkCopyOptions.UseInternalTransaction))
                {
                    throw new InvalidOperationException("BulkInsertDisableIdentity not compatible with UseInternalTransaction");
                }

                var list = entities.ToList();

                if (preSaving)
                {
                    Saver.PreSaving(() => GraphExplorer.FromRoots(list));
                }

                if (validateFirst)
                {
                    Validate <T>(list);
                }

                var  t = Schema.Current.Table <T>();
                bool disableIdentityBehaviour = copyOptions.HasFlag(SqlBulkCopyOptions.KeepIdentity);

                DataTable dt      = new DataTable();
                var       columns = t.Columns.Values.Where(c => !(c is SystemVersionedInfo.SqlServerPeriodColumn) && (disableIdentityBehaviour || !c.IdentityBehaviour)).ToList();
                foreach (var c in columns)
                {
                    dt.Columns.Add(new DataColumn(c.Name, ConvertType(c.Type)));
                }

                using (disableIdentityBehaviour ? Administrator.DisableIdentity(t, behaviourOnly: true) : null)
                {
                    foreach (var e in list)
                    {
                        if (!e.IsNew)
                        {
                            throw new InvalidOperationException("Entites should be new");
                        }
                        t.SetToStrField(e);
                        dt.Rows.Add(t.BulkInsertDataRow(e));
                    }
                }

                using (Transaction tr = new Transaction())
                {
                    Schema.Current.OnPreBulkInsert(typeof(T), inMListTable: false);

                    Executor.BulkCopy(dt, columns, t.Name, copyOptions, timeout);

                    foreach (var item in list)
                    {
                        item.SetNotModified();
                    }

                    return(tr.Commit(list.Count));
                }
            }
        }
Exemple #5
0
        /// <summary>
        /// Executes an action for each element in the collection transactionally and showing the progress in the Console
        /// </summary>
        public static void ProgressForeach <T>(this IEnumerable <T> collection,
                                               Func <T, string> elementID      = null,
                                               Action <T> action               = null,
                                               bool transactional              = true,
                                               LogWriter writer                = null,
                                               ParallelOptions parallelOptions = null,
                                               Type disableIdentityFor         = null)
        {
            if (action == null)
            {
                throw new InvalidOperationException("no action specified");
            }

            if (elementID == null)
            {
                elementID = e => e.ToString();
            }
            if (writer == null)
            {
                writer = GetConsoleWriter();
            }


            if (disableIdentityFor == null)
            {
                collection.ProgressForeachInternal(elementID, writer, parallelOptions, transactional, action);
            }
            else
            {
                if (!transactional)
                {
                    throw new InvalidOperationException("disableIdentity has to be transactional");
                }

                Table table = Schema.Current.Table(disableIdentityFor);

                if (!table.IdentityBehaviour)
                {
                    throw new InvalidOperationException("Identity is false already");
                }

                table.IdentityBehaviour = false;
                try
                {
                    collection.ProgressForeachInternal(elementID, writer, parallelOptions, transactional: true, action: item =>
                    {
                        using (Transaction tr = Transaction.ForceNew())
                        {
                            using (table.PrimaryKey.Default != null
                                ? null
                                : Administrator.DisableIdentity(table.Name))
                                action(item);
                            tr.Commit();
                        }
                    });
                }
                finally
                {
                    table.IdentityBehaviour = true;
                }
            }
        }