我收到此(dbexception unhandled by user code)
上的{"更新条目时发生错误。看到内心 细节例外。"}
System.Data.UpdateException
例外
db.SaveChanges();
当我输入数据并单击“保存”按钮时。
//POST Create
[HttpPost]
public ActionResult Create(Information info)
{
if (ModelState.IsValid)
{
db.Information.Add(info);
db.SaveChanges(); //In this line shows Error
return Redirect("Home");
}
return View(info);
}
答案 0 :(得分:0)
此处您的(信息信息)参数可能为空或者超过字符长度...
例如info.name =" tttttttt ......&#34 ;;(字符串超过50个字符)但在db表中你声明为名称nvarchar(50)...
检查db表设计和输入对象....
答案 1 :(得分:0)
这样做是为了在文件中编写简化的异常,它会告诉你异常的确切原因:
try
{
// Your code...
// Could also be before try if you know the exception occurs in SaveChanges
db.SaveChanges();
}
catch (System.Data.Entity.Validation.DbEntityValidationException e)
{
var outputLines = new List<string>();
foreach (var eve in e.EntityValidationErrors)
{
outputLines.Add(string.Format(
"{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:",
DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State));
foreach (var ve in eve.ValidationErrors)
{
outputLines.Add(string.Format(
"- Property: \"{0}\", Error: \"{1}\"",
ve.PropertyName, ve.ErrorMessage));
}
}
System.IO.File.AppendAllLines(@"c:\temp\errors.txt", outputLines);
}