Hi so keep encountering this exception every time when savechanges() is called. There is an other post that has multiple answers but, I cannot pin point which answer is suitable for my problem. Also it seems that everyone has a different opinion about this exception.
Link to other post: [a link] Entity Framework: "Store update, insert, or delete statement affected an unexpected number of rows (0)."
My Exception:
An unhandled exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll
Additional information: An error occurred while updating the entries. See the inner exception for details.
I am trying to save mails into my storage. I am using entity framework 6.1.3 and SQL server 2014.
This is my method that stores mails:
public int StoreMail(PhishingMail PhishingMail)
{
using (var phishingMailStorage = new PhishFinderDBModel())
{
try
{
//// var manager = ((IObjectContextAdapter)phishingMailStorage).ObjectContext.ObjectStateManager;
//// phishingMailStorage.PhishingMail.Attach(PhishingMail);
phishingMailStorage.Entry(PhishingMail).State = PhishingMail.PhishingMailId == 0 ? EntityState.Added : EntityState.Modified;
phishingMailStorage.SaveChanges();
//// manager.ChangeObjectState(PhishingMail, EntityState.Modified);
//// phishingMailStorage.SaveChanges();
Console.WriteLine("Het is gelukt");
}
catch (OptimisticConcurrencyException)
{
var ctx = ((IObjectContextAdapter)phishingMailStorage).ObjectContext;
ctx.Refresh(RefreshMode.ClientWins, phishingMailStorage.PhishingMail);
phishingMailStorage.SaveChanges();
}
}
return PhishingMail.PhishingMailId;
}
This is my get mails method, that does work:
public List<PhishingMail> GetEmails()
{
phishingMailList = new List<PhishingMail>();
FolderId InboxId = new FolderId(WellKnownFolderName.Inbox, "******@******.nl");
FindItemsResults<Item> findResults = service.FindItems(InboxId, new ItemView(20));
foreach (Item phishingmail in findResults.Items)
{
if (!((EmailMessage)phishingmail).IsRead)
{
/// ((EmailMessage)phishingmail).IsRead = true;
((EmailMessage)phishingmail).Update(ConflictResolutionMode.AutoResolve);
}
PhishingMail mail = MailMapper.Map((EmailMessage)phishingmail);
//// ((EmailMessage)phishingmail).Load(new PropertySet(BasePropertySet.IdOnly, EmailMessageSchema.IsRead));
phishingMailList.Add(mail);
/// Console.WriteLine(mail.Type);
}
return phishingMailList;
}
Why does savechanges() not work and how do I make it work?
Thank you.