简体   繁体   English

发生System.Data.Entity.Infrastructure.DbUpdateException

[英]System.Data.Entity.Infrastructure.DbUpdateException occurred

So I have made a custom controller by parsing objects in a model to a database in my project and inside that controller, there is a post method called Edit which includes the database fields such as Id, Title, Description, FileName, FileType, FileSize, Author,DateUploaded 因此,我通过将模型中的对象解析到项目中的数据库中来创建了一个自定义控制器,该控制器内部有一个称为Edit的post方法,其中包括数据库字段,例如Id,Title,Description,FileName,FileType,FileSize,作者,日期已上传

In the edit html view, I removed some elements because all I want to edit is "Title" and "Description" and I have also removed the fields in the edit method in the controller I created. 在html编辑视图中,我删除了一些元素,因为我要编辑的只是“标题”和“描述”,并且还删除了我创建的控制器中edit方法中的字段。

To explain that; 解释一下;

public ActionResult Edit([Bind(Include = FileSharing "Id,Title,Description,FileName,FileType,FileSize,Author,DateUploaded")] FileSharing fileSharing)

into: 变成:

public ActionResult Edit([Bind(Include = "Id,Title,Description")] FileSharing fileSharing)

When I try to edit a title or description. 当我尝试编辑标题或描述时。 It will give a exception error saying 它将给出异常错误说

System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll but was not handled in user code System.Data.Entity.Infrastructure.DbUpdateException'发生在EntityFramework.dll中,但未在用户代码中处理

Why am I getting this error and how can I get around it? 为什么会出现此错误,如何解决?

Edit method Edit方式

public ActionResult Edit([Bind(Include = "Id,Title,Description")] FileSharing fileSharing)
    {
        if (ModelState.IsValid)
        {
            try
            {
                db.Entry(fileSharing).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch (Exception)
            {
                ViewBag.EditFail = "Error editing file details.";
            }
        }
        return View(fileSharing);
    }

Here's how I handle that exception. 这是我处理该异常的方法。

private Exception HandleDbUpdateException(DbUpdateException dbu)
        {
            var builder = new StringBuilder("A DbUpdateException was caught while saving changes. ");
            if (!(dbu.InnerException is System.Data.Entity.Core.UpdateException) ||
                !(dbu.InnerException.InnerException is System.Data.SqlClient.SqlException))
            {
                try
                {
                    foreach (var result in dbu.Entries)
                    {
                        builder.AppendFormat("Type: {0} was part of the problem. ", result.Entity.GetType().Name);
                    }
                }
                catch (Exception e)
                {
                    builder.Append("Error parsing DbUpdateException: " + e);
                }
            }
            else
            {
                var sqlException = (System.Data.SqlClient.SqlException)dbu.InnerException.InnerException;
                for (int i = 0; i < sqlException.Errors.Count; i++)
                {
                    builder.AppendLine("    SQL Message: " + sqlException.Errors[i].Message);
                    builder.AppendLine("    SQL procedure: " + sqlException.Errors[i].Procedure);
                }
            }

            string message = builder.ToString();
            return new Exception(message, dbu);
        }

Then at least the exception has more relevant information, which probably makes it easy for you to figure out what's wrong (like some DB validation issue). 然后,至少该异常具有更多相关信息,这可能使您很容易找出问题所在(例如某些数据库验证问题)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EntityFramework.dll 中发生“System.Data.Entity.Infrastructure.DbUpdateException” - 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll 发生类型为“ System.Data.Entity.Infrastructure.DbUpdateException”的异常 - An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred System.Data.Entity.Infrastructure.DbUpdateException - System.Data.Entity.Infrastructure.DbUpdateException System.Data.Entity.Infrastructure.DbUpdateException问题 - System.Data.Entity.Infrastructure.DbUpdateException Issue EntityFramework.dll中发生类型为&#39;System.Data.Entity.Infrastructure.DbUpdateException&#39;的异常 - An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll MVC EF-System.Data.Entity.Infrastructure.DbUpdateException - MVC EF - System.Data.Entity.Infrastructure.DbUpdateException 如何解决 System.Data.Entity.Infrastructure.DbUpdateException - How to solve System.Data.Entity.Infrastructure.DbUpdateException 递归树方法上的System.Data.Entity.Infrastructure.DbUpdateException - System.Data.Entity.Infrastructure.DbUpdateException on recursive Tree method 将数据添加到数据库(错误:System.Data.Entity.Infrastructure.DbUpdateException) - Adding Data to Database (Error: System.Data.Entity.Infrastructure.DbUpdateException) 如何避免System.Data.Entity.Infrastructure.DbUpdateException - How to avoid System.Data.Entity.Infrastructure.DbUpdateException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM