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

查看:1648
本文介绍了发生System.Data.Entity.Infrastructure.DbUpdateException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我做了一个自定义控制器通过解析模型中的对象到我的项目中的数据库和控制器内,有一个名为编辑的post方法,其中包括数据库字段(例如 Id,标题,说明,文件名,文件类型,文件大小,作者,上传日期



在编辑html视图中,因为我想要编辑的是标题和描述,我也删除了在我创建的控制器中的编辑方法中的字段。



要解释这一点;

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


$ b b

into:

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

当我尝试编辑标题或描述时。它会给一个异常错误说


System.Data.Entity.Infrastructure.DbUpdateException'发生在
EntityFramework.dll,但是不能在用户代码中处理


为什么会收到此错误,如何解决?



编辑方法

  public ActionResult [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 =编辑文件详细信息时出错。
}
}
return View(fileSharing);
}


解决方案

 私人异常HandleDbUpdateException(DbUpdateException dbu)
{
var builder = new StringBuilder(A DbUpdateException被捕获,同时保存更改。
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} is the problem 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);
}

然后至少该异常有更多的相关信息,这可能使你找出什么问题(像一些数据库验证问题)。


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

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.

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

Why am I getting this error and how can I get around it?

Edit method

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).

这篇关于发生System.Data.Entity.Infrastructure.DbUpdateException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆