欢迎光临

木子屋

标题: Entity Framework出错Validation failed for one o&#114; more entities

作者: dnawo

日期: 2012-09-08 05:32 PM

分类: Win编程

内容: 使用Entity Framework保存记录时出错，提示：
引用内容Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
详细信息中没显示是哪个字段验证出错：
Validation failed for one or more entities解决方法
复制内容到剪贴板 程序代码try
{
Context.SaveChanges();
}
catch (DbEntityValidationException ex)
{
StringBuilder errors = new StringBuilder();
IEnumerable<DbEntityValidationResult> validationResult = ex.EntityValidationErrors;
foreach (DbEntityValidationResult result in validationResult)
{
ICollection<DbValidationError> validationError = result.ValidationErrors;
foreach (DbValidationError err in validationError)
{
errors.Append(err.PropertyName + ":" + err.ErrorMessage + "\r\n");
}
}
Console.WriteLine(errors.ToString());
//简写
//var validerr = ex.EntityValidationErrors.First().ValidationErrors.First();
//Console.WriteLine(validerr.PropertyName + ":" + validerr.ErrorMessage);
}
结果显示：
引用内容Imsi:The field Imsi must be a string or array type with a maximum length of '20'

+ 查看当前日志评论 (0)

登录

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 0.771 ms

返回

标题: Entity Framework出错Validation failed for one o&#114; more entities

你没有权限发表评论

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 0.771 ms

返回

暂无评论

返回

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 0.804 ms

返回