Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Validation Errors - .NET Framework

RSS
Modified on Mon, Nov 11, 2013, 3:41 PM by Administrator Categorized as ·Net Framework
The following code shows how to see validation errors within Entity Framework 4.5 (and possibly earlier).

try 
{
    using (var context = new DbContext())
    {
        ...
        context.SaveChanges();
    }
}
catch (System.Data.Entity.Validation.DbEntityValidationException e)
{
    var sb = new System.Text.StringBuilder();

    foreach (var eve in e.EntityValidationErrors)
    {
        sb.AppendLine(string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
            eve.Entry.Entity.GetType().Name, eve.Entry.State));

        foreach (var ve in eve.ValidationErrors)
        {
            sb.AppendLine(string.Format("- Property: \"{0}\", Error: \"{1}\"",
                ve.PropertyName, ve.ErrorMessage));
        }
    }
    throw;
}

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.