如何解决Validation failed for one or more entities.错误

如何解决Validation failed for one or more entities. See ‘EntityValidationErrors’ property for more details.错误

开发工具与关键技术:Visual Studio 与C#

作者:黄灿

撰写时间:2019.6.22

相信大家都知道



try

            {

                //其他代码

            }

            catch (Exception e)

            {

                Console.Write(e);

                returnJson.Text = "数据异常";

            }


捕获异常,查看异常

Exception能让我们轻松知道具体是那一个字段出现问题,Exception捕获出Validation failed for one or more entities. See
‘EntityValidationErrors’ property for more details.错误,相信大家很多人都遇到过,这个错误到底是个啥错误呢!怎么解决呢!我们还是使用try和catch捕获异常,不过我们把Exception更改为DbEntityValidationException查看错误,使用DbEntityValidationException捕获异常需要using System.Data.Entity.Validation

在这里插入图片描述



try

            {

                //其他代码

            }

            catch (DbEntityValidationException dbEx)

            {

                Console.Write(dbEx);

                returnJson.Text = "数据异常";

            }


在dbEx中我们就可以看到

在这里插入图片描述

这样子我们就能看到EntityValidationErrors所有的ValidationErrors详细信息了,具体是什么错误也可以看出来了,这样子我们就可以轻松的具体错误具体解决了

猜你喜欢

转载自blog.csdn.net/weixin_44542088/article/details/93634590