【问题标题】:Error: Validation failed for one or more entities (C# MVC)错误:一个或多个实体的验证失败 (C# MVC)
【发布时间】:2018-05-17 07:48:09
【问题描述】:

我正在尝试让我的 JqGrid 更新我的数据库。我不断收到错误消息:“一个或多个实体的验证失败。有关详细信息,请参阅 'EntityValidationErrors' 属性。”

如何找出我的实际错误或导致错误的原因?

 // TODO:insert a new row to the grid logic here  
    [HttpPost]
    public string Create([Bind(Exclude = "Id")] AspNetUser obj)
    {
        //System.Diagnostics.Debug.WriteLine("Create");
        string msg;
        try
        {
            if (ModelState.IsValid)
            {
                db.AspNetUsers.Add(obj);
                //db.AspNetUsers.Add(new AspNetUser { UserName = obj.UserName, Email = obj.Email });
                db.SaveChanges();
                msg = "Saved Successfully";
            }
            else
            {
                msg = "Validation data not successfull";
            }
        }
        catch (Exception ex)
        {
            msg = "Error occured:" + ex.Message;
        }
        return msg;
    }

【问题讨论】:

  • 它已经在告诉你该怎么做 - 有关更多详细信息,请参阅 'EntityValidationErrors' 属性。"
  • 另外,如果 EntityValiationErrors 不在异常 ex 中,请尝试在内部异常中查找。
  • @stephenMuecke 我在哪里可以找到这个属性?
  • @codingYoshi 原谅我的无知,但我该怎么做呢?

标签: c# asp.net-mvc jqgrid


【解决方案1】:

通常这个异常意味着数据库验证失败。如果您在数据库中有一个设置了字符计数的字段,并且您尝试将超过数据库计数的字符串保存到该字段,则可能会发生这种情况。

例如,使用 'varchar(max)' 更新您的数据库字符串类型。

希望对你有帮助

【讨论】:

  • 原来“id”没有返回任何东西。
猜你喜欢
  • 2019-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-02
相关资源
最近更新 更多