public static string FormatMessage(DbException ex, SqlMigrationRun sqlRun)
        {
            string errorMsg = string.Empty;

            if (ex is SqlException)
            {
                var sqlEx = ex as SqlException;
                var no    = sqlEx.Number;
                switch (no)
                {
                case 515:
                    errorMsg = "把一个可空字段变更为非空字段时,由于存在不可空约束,所以数据库该表中不能有该字段为空的数据行。\r\n";
                    break;

                case 547:
                    errorMsg = "把一个字段变更为非空外键字段时,由于外键约束,所以数据库该表中不能有该字段数据不满足引用约束的数据行。\r\n";
                    break;

                default:
                    break;
                }
            }

            errorMsg += ex.Message;

            var error = "执行数据库迁移时出错:" + errorMsg;

            if (sqlRun != null)
            {
                error += Environment.NewLine + "对应的 SQL:" + sqlRun.Sql;
            }

            return(error);
        }
        public static string FormatMessage(DbException ex, SqlMigrationRun sqlRun)
        {
            string errorMsg = string.Empty;

            if (ex is SqlException)
            {
                var sqlEx = ex as SqlException;
                var no = sqlEx.Number;
                switch (no)
                {
                    case 515:
                        errorMsg = "把一个可空字段变更为非空字段时,由于存在不可空约束,所以数据库该表中不能有该字段为空的数据行。\r\n";
                        break;
                    case 547:
                        errorMsg = "把一个字段变更为非空外键字段时,由于外键约束,所以数据库该表中不能有该字段数据不满足引用约束的数据行。\r\n";
                        break;
                    default:
                        break;
                }
            }

            errorMsg += ex.Message;

            var error = "执行数据库迁移时出错:" + errorMsg;

            if (sqlRun != null)
            {
                error += Environment.NewLine + "对应的 SQL:" + sqlRun.Sql;
            }

            return error;
        }
Esempio n. 3
0
        private void RunMigration(DbMigration migration, bool up)
        {
            var runList = this.GenerateRunList(migration, up);

            if (runList.Count == 0) return;

            this.ExecuteWithoutDebug(() =>
            {
                var dba = this.DBA;
                if (runList.Count > 1)
                {
                    /*********************** 代码块解释 *********************************
                     *
                     * 如果执行多句 SQL,则需要主动打开连接,
                     * 否则 DBA 可能会打开之后再把连接关闭,造成多次打开连接,引起分布式事务。
                     * 而分布式事务在一些数据库中并不支持,例如 SQLCE。
                     *
                    **********************************************************************/
                    bool opend = false;
                    var con = dba.Connection;

                    try
                    {
                        if (con.State != ConnectionState.Open)
                        {
                            con.Open();
                            opend = true;
                        }

                        using (var tran = new TransactionScope())
                        {
                            foreach (var item in runList)
                            {
                                this._currentRun = item as SqlMigrationRun;
                                item.Run(dba);
                            }

                            tran.Complete();
                        }
                    }
                    finally
                    {
                        if (opend)
                        {
                            con.Close();
                        }
                    }
                }
                else
                {
                    var singleRun = runList[0];
                    this._currentRun = singleRun as SqlMigrationRun;
                    singleRun.Run(dba);
                }
            });
        }