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

发布于 2024-11-08 03:11:52 字数 5358 浏览 8 评论 0原文

我有一个初始选择,并将其放入列表中。我使用列表循环遍历每条记录,当它满足某些条件时,我会执行一系列插入、删除和更新。最后调用 SaveChanges() 方法来提交更改。

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

我正在使用 VS2008 和 SQL2008 后端。

请帮忙?

            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;

问候, 探地雷达。

I have an initial selection which I place into list. I use the list to loop through each record and where it meets certain criteria I run trough a series of inserts, deletes and updates. Finally call the SaveChanges() method to commit changes.

The code runs through without raising an exception but no changes reflect in the database. I have been searching the web with no luck.

I'm using VS2008 with SQL2008 backend.

Please help?

            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;

Regards,
GPR.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

や莫失莫忘 2024-11-15 03:11:52

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

请参阅 http://blogs.msdn.com/b/ smartclientdata/archive/2005/08/26/456886.aspx 了解更多信息

Are you using a local database file? You may be looking for changes in the wrong place. By default, when the program starts, VS copies the database file into the debug or release folder. Then the program runs and changes are made, and saved, to the file in the debug or release folder. The program ends, and when you look at the database in your source folder it looks the same. You can change the connection string in the app.config to use an absolute path to avoid this.

See http://blogs.msdn.com/b/smartclientdata/archive/2005/08/26/456886.aspx for more info

日暮斜阳 2024-11-15 03:11:52

如果您不将对 SaveChanges 的调用放入其中,TransactionScope 将毫无用处。
要么将对 SaveChanges 的调用移入其中,要么完全删除 TransactionScope。

The TransactionScope is useless if you do not put the call to SaveChanges into it.
Either move the call to SaveChanges into it or remove the TransactionScope completely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文