实体框架SaveChanges函数不会提交我的更改

时间:2011-05-18 06:16:21

标签: sql-server-2008 entity-framework-4 savechanges

我有一个初始选择,我将其放入列表中。我使用列表循环遍历每个记录,并且它符合某些标准,我通过一系列插入,删除和更新运行。最后调用SaveChanges()方法提交更改。

代码运行时没有引发异常,但数据库中没有反映。我一直在网上搜索没有运气。

我正在使用带有SQL2008后端的VS2008。

请帮帮忙?

            using (SMSEntities db = new SMSEntities())
        {
            try
            {

                //Get SMS's to send from Inbox
                List<Inbox> tmpInbox = (from c in db.Inboxes where c.Status != "NEW" && c.Status != "SUCCESS" select c).ToList();// new { Inbox.InboxID, Inbox.StatusTrackingID, Inbox.Status, Inbox.NoOfAttempts, Inbox.CellNo, Inbox.SourceCellNo, Inbox.Header, Inbox.Message, Inbox.MessageDate, Inbox.AccountID, Inbox.LastAttemptDate }).ToList();

                foreach (Inbox tmpInboxIndex in tmpInbox)
                {
                    bool success = false;

                    //Check status here
                    string SentStatus = CheckSMSSentToProvider(tmpInboxIndex.StatusTrackingID);

                    // Define a transaction scope for the operations.
                    using (TransactionScope transaction = new TransactionScope())
                        {
                        try
                        {
                            if ((SentStatus == "DELIVERED") || (SentStatus == "NOTFOUND") || (SentStatus == "DELETED") || (SentStatus == "REJECTED") || (SentStatus == "UNDELIVERED"))
                            {

                                //Insert the Log row
                                Log newLog = new Log();
                                newLog.InboxID = tmpInboxIndex.InboxID;
                                newLog.CellNo = tmpInboxIndex.CellNo;
                                newLog.SourceCellNo = tmpInboxIndex.SourceCellNo;
                                newLog.Message = tmpInboxIndex.Message;
                                newLog.Header = tmpInboxIndex.Header;
                                newLog.MessageDate = tmpInboxIndex.MessageDate;
                                newLog.AccountID = tmpInboxIndex.AccountID;
                                newLog.ProcessedDate = DateTime.Now;
                                newLog.Status = tmpInboxIndex.Status;
                                newLog.StatusTrackingID = tmpInboxIndex.StatusTrackingID;
                                newLog.NoOfAttempts = tmpInboxIndex.NoOfAttempts;
                                newLog.LastAttemptDate = tmpInboxIndex.LastAttemptDate;
                                db.Logs.AddObject(newLog);

                                //Delete the Inbox row
                                if (tmpInbox != null)
                                {
                                    var deleteInbox = (from c in db.Inboxes where c.InboxID == tmpInboxIndex.InboxID select c).FirstOrDefault();
                                    if (deleteInbox != null)
                                    {
                                        db.DeleteObject(deleteInbox);
                                        //db.SaveChanges(SaveOptions.DetectChangesBeforeSave);
                                    }
                                }
                            }
                            else
                            {
                                //Update inbox status 
                                var tmpUpdateInbox = (from c in db.Inboxes where c.InboxID == tmpInboxIndex.InboxID select c).FirstOrDefault();
                                tmpUpdateInbox.Status = SentStatus;
                                tmpUpdateInbox.NoOfAttempts = tmpInboxIndex.NoOfAttempts + 1;
                                tmpUpdateInbox.LastAttemptDate = DateTime.Now;
                                //db.SaveChanges(SaveOptions.DetectChangesBeforeSave);
                            }
                            // Mark the transaction as complete.
                            transaction.Complete();
                            success = true;
                            //break;
                        }
                        catch (Exception ex)
                        {
                            // Handle errors and deadlocks here and retry if needed.
                            // Allow an UpdateException to pass through and 
                            // retry, otherwise stop the execution.
                            if (ex.GetType() != typeof(UpdateException))
                            {
                                Console.WriteLine("An error occured. "
                                    + "The operation cannot be retried."
                                    + ex.Message);
                                break;
                            }
                            // If we get to this point, the operation will be retried.
                        }
                    }

                    if (success)
                    {
                        // Reset the context since the operation succeeded.
                        //db.AcceptAllChanges();
                        db.SaveChanges();
                    }
                }

                // Dispose the object context.
                db.Dispose();

            }

            catch (Exception exp)
            {
                throw new Exception("ERROR - " + exp.Message.ToString(), exp);
            }
        }
        return true;

此致 GPR。

2 个答案:

答案 0 :(得分:1)

您使用的是本地数据库文件吗?您可能正在寻找错误位置的变化。默认情况下,程序启动时,VS会将数据库文件复制到调试或发布文件夹中。然后程序运行并进行更改并保存到调试或发布文件夹中的文件中。程序结束,当您查看源文件夹中的数据库时,它看起来是一样的。您可以更改app.config中的连接字符串以使用绝对路径来避免这种情况。

有关详细信息,请参阅http://blogs.msdn.com/b/smartclientdata/archive/2005/08/26/456886.aspx

答案 1 :(得分:0)

如果您不调用SaveChanges,则TransactionScope无用。 将调用SaveChanges移入其中或完全删除TransactionScope。

相关问题