实体框架SaveChanges函数不会提交我的更改 [英] Entity Framework SaveChanges function won't commit my changes
本文介绍了实体框架SaveChanges函数不会提交我的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我有一个初步选择,我将其放入列表中。我使用列表循环遍历每个记录,并且它符合某些标准,我通过一系列插入,删除和更新运行。最后调用SaveChanges()方法提交更改。
代码运行时不会引发异常,但数据库中没有反映。我一直在网上搜索没有运气。
我正在使用VS2008和SQL2008后端&以下代码片段。
请帮忙?
提前致谢,
GP
Hi All,
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 & code snippet below.
Please help?
Thanks in advance,
GP
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;
推荐答案
您的数据库有可能是本地数据库吗?通过这个,我的意思是项目的本地?如果是这样,请检查调试目录中是否已更新数据库。一个常见的问题是开发环境中有一个master数据库,每次运行应用程序时都会覆盖debug目录中的master数据库,因为它总是被设置为覆盖。
By any chance is your database a local database? By this, I mean local to the project? If so, check to see if the database has been updated in your debug directory. A common problem is for there being a master database in the development environment which overwrites the one in the debug directory every time the application runs because it is set to overwrite always.
@Pete,
如果我解释一下,也许它会有所帮助。
我在解决方案中有2个项目
- 网络应用程序
- Windows服务应用程序
主数据库驻留在Web应用程序上,我创建了一个新的实体(在服务应用程序上)直接指向Web应用程序。在创建新实体后,它看起来像是从Web应用程序到Windows服务应用程序的数据库副本......这不是我的意图.....我只想引用数据库中的网络应用程序。
我是否以正确的方式引用它?这是正确的方法
根据你的回答,我认为答案可能在某处。
干杯,
GP
@Pete,
Maybe it'll help if I explain a bit more.
I have 2 projects within the solution
- Web App
- Windows Service App
The main DB resides on the Web App and I created a new Entity (on the Service app) pointing directly to the Web App. After creating the new Entity it looked like it made a copy of the DB from the web app to the Windows service app...... which was not my intention..... I only wanted to "reference" the DB in the web app.
Did I reference it in the correct manner? Is it the right way to do it
Based on your response I think that the answer might lie here somewhere.
Cheers,
GP
这篇关于实体框架SaveChanges函数不会提交我的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文