DbUpdateException未处理?

时间:2015-11-26 12:13:15

标签: c# sql-server entity-framework-6 savechanges

嗨,所以每次调用savechanges()时都会遇到此异常。还有一篇文章有​​多个答案但是,我无法确定哪个答案适合我的问题。此外,似乎每个人对此例外都有不同的看法。

链接到其他帖子:[链接] Entity Framework: "Store update, insert, or delete statement affected an unexpected number of rows (0)."

我的例外:

  

未处理的类型异常   发生'System.Data.Entity.Infrastructure.DbUpdateException'   EntityFramework.dll

     

其他信息:更新条目时发生错误。   有关详细信息,请参阅内部异常。

我正在尝试将邮件保存到我的存储空间中。我正在使用实体框架6.1.3和SQL Server 2014。

这是我存储邮件的方法:

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;
}

这是我的获取邮件方法,确实有效:

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;
}

为什么savechanges()不起作用以及如何使其工作?

谢谢。

1 个答案:

答案 0 :(得分:0)

编写你的db.SaveChanges(); try块内的方法。它会告诉你确切的问题

try
{
     db.SaveChanges();
}
catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
{
     Exception raise = dbEx;
     foreach (var validationErrors in dbEx.EntityValidationErrors)
     {
         foreach (var validationError in validationErrors.ValidationErrors)
         {
              string message = string.Format("{0}:{1}",
              validationErrors.Entry.Entity.ToString(),
              validationError.ErrorMessage);

              raise = new InvalidOperationException(message, raise);
         }
      }
      throw raise;
}