代码之家  ›  专栏  ›  技术社区  ›  Mike U

Linq2Sql更改跟踪不工作

  •  1
  • Mike U  · 技术社区  · 10 年前

    我运行下面的代码,根据每天早上发送给我们的银行交易历史文件更新一些记录。这是很基本的东西,但出于某种原因,当我到达终点时, dbContext.GetChangeSet() 报告所有操作的“0”。

    public void ProcessBatchFile(string fileName)
    {
        List<string[]> failed = new List<string[]>();
        int recCount = 0;
        DateTime dtStart = DateTime.Now;
        using (ePermitsDataContext dbContext = new ePermitsDataContext())
        {
            try
            {
                // A transaction must be begun before any data is read.
                dbContext.BeginTransaction();
                dbContext.ObjectTrackingEnabled = true;
    
                // Load all the records for this batch file.
                var batchRecords = (from b in dbContext.AmegyDailyFiles
                                    where b.FileName == fileName
                                    && b.BatchProcessed == false
                                    && (b.FailReason == null || b.FailReason.Trim().Length < 1)
                                    select b);
    
                // Loop through the loaded records
                int paymentID;
                foreach (var r in batchRecords)
                {
                    paymentID = 0;
                    try
                    {
                        // We have to 'parse' the primary key, since it's stored as a string value with leading zero's.
                        if (!int.TryParse(r.TransAct.TrimStart('0'), out paymentID))
                            throw new Exception("TransAct value is not a valid integer: " + r.TransAct);
    
                        // Store the parsed, Int32 value in the original record and read the "real" record from the database.
                        r.OrderPaymentID = paymentID;
                        var orderPayment = this.GetOrderPayment(dbContext, paymentID);
    
                        if (string.IsNullOrWhiteSpace(orderPayment.AuthorizationCode))
                            // If we haven't processed this payment "Payment Received" do it now.
                            this.PaymentReceived(orderPayment, r.AuthorizationNumber);
    
                        // Update the PaymentTypeDetailID (type of Credit Card--all other types will return NULL).
                        var paymentTypeDetail = dbContext.PaymentTypes.FirstOrDefault(w => w.PaymentType1 == r.PayType);
                        orderPayment.PaymentTypeDetailID = (paymentTypeDetail != null ? (int?)paymentTypeDetail.PaymentTypeID : null);
    
                        // Match the batch record as processed.
                        r.BatchProcessed = true;
                        r.BatchProcessedDateTime = DateTime.Now;
                        dbContext.SubmitChanges();
                    }
                    catch (Exception ex)
                    {
                        // If there's a problem, just record the error message and add it to the "failed" list for logging and notification.
                        if (paymentID > 0)
                            r.OrderPaymentID = paymentID;
                        r.BatchProcessed = false;
                        r.BatchProcessedDateTime = null;
                        r.FailReason = ex.Message;
                        failed.Add(new string[] { r.TransAct, ex.Message });
                        dbContext.SubmitChanges();
                    }
                    recCount++;
                }
    
                dbContext.CommitTransaction();
            }
            // Any transaction will already be commited, if the process completed successfully.  I just want to make
            //   absolutely certain that there's no chance of leaving a transaction open.
            finally { dbContext.RollbackTransaction(); }
        }
    
        TimeSpan procTime = DateTime.Now.Subtract(dtStart);
    
        // Send an email notification that the processor completed.
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.AppendFormat("<p>Processed {0} batch records from batch file '{1}'.</p>", recCount, fileName);
        if (failed.Count > 0)
        {
            sb.AppendFormat("<p>The following {0} records failed:</p>", failed.Count);
            sb.Append("<ul>");
            for (int i = 0; i < failed.Count; i++)
                sb.AppendFormat("<li>{0}: {1}</li>", failed[i][0], failed[i][1]);
            sb.Append("<ul>");
        }
        sb.AppendFormat("<p>Time taken: {0}:{1}:{2}.{3}</p>", procTime.Hours, procTime.Minutes, procTime.Seconds, procTime.Milliseconds);
        EMailHelper.SendAdminEmailNotification("Batch Processing Complete", sb.ToString(), true);
    }
    

    这个 dbContext.BeginTransaction() 方法是我添加到DataContext中的东西,只是为了方便使用显式事务。我很有信心这不是问题,因为它在应用程序的其他地方被广泛使用。我们的数据库设计使得有必要为一些特定的操作使用显式事务,而对“PaymentReceived”的调用恰好就是其中之一。

    我已逐一检查代码并确认 Rollback() 事务本身的方法没有开始调用,我还检查了 dbContext.GetChangeSet() 在呼叫之前 CommitTransaction() 发生同样的结果。

    我已包括 BeginTransaction() , 提交事务() 和 RollbackTransaction() 下面的方法体,只是为了清楚起见。

    /// <summary>
    /// Begins a new explicit transaction on this context.  This is useful if you need to perform a call to SubmitChanges multiple times due to "circular" foreign key linkage, but still want to maintain an atomic write.
    /// </summary>
    public void BeginTransaction()
    {
        if (this.HasOpenTransaction)
            return;
    
        if (this.Connection.State != System.Data.ConnectionState.Open)
            this.Connection.Open();
    
        System.Data.Common.DbTransaction trans = this.Connection.BeginTransaction();
        this.Transaction = trans;
        this._openTrans = true;
    }
    /// <summary>
    /// Commits the current transaction (if active) and submits all changes on this context.
    /// </summary>
    public void CommitTransaction()
    {
        this.SubmitChanges();
        if (this.Transaction != null)
            this.Transaction.Commit();
        this._openTrans = false;
        this.RollbackTransaction(); // Since the transaction has already been committed, this just disposes and decouples the transaction object itself.
    }
    /// <summary>
    /// Disposes and removes an existing transaction on the this context.  This is useful if you want to use the context again after an explicit transaction has been used.
    /// </summary>
    public void RollbackTransaction()
    {
        // Kill/Rollback the transaction, as necessary.
        try
        {
            if (this.Transaction != null)
            {
                if (this._openTrans)
                    this.Transaction.Rollback();
                this.Transaction.Dispose();
                this.Transaction = null;
            }
            this._openTrans = false;
        }
        catch (ObjectDisposedException) { } // If this gets called after the object is disposed, we don't want to let it throw exceptions.
        catch { throw; }
    }
    
    1 回复  |  直到 10 年前
        1
  •  1
  •   Mike U    10 年前

    我刚刚发现了问题:我的DBA在为我创建表时没有将主键放在表上,所以LinqToSql没有在实体类中生成任何“PropertyChanged”事件/处理程序,这就是为什么DataContext没有意识到正在进行更改的原因。显然,如果您的表没有主键,Linq2Sql将不会跟踪对该表的任何更改,这是有道理的,但如果有某种通知,这将是很好的。我确信我的DBA没有考虑过这一点,因为这只是一种“跟踪”文本文件中哪些行项目已被处理,并且与其他任何表没有直接关系的方法。