Labels

slider

Recent

Navigation

Entity Framework Error: Validation failed for one or more entities

Validation failed for one or more entities, EntityValidationErrors, DbEntityValidationException, Entity Framework Error

Introduction

While running on C# app using Entity Framework, me came me the issue Validation failed for one or more entities. Today, I've prepared a whole solution to conquer issue Validation failed for one or more entities while playing with Entity Framework (MS SQL Server database) using C#, MVC, ASP.Net, VN.net etc. Recently, I've absolutely explained entity framework error Store update insert or delete statement affected an unexpected number of rows (0)  in my recent article, this day going to resolve another one entity framework error Validation failed for one or more entities.

Main Error Causes

This error primarily once there's issue with our database entities, example like we've given text message length 40 characters and attempting to insert  more than 40 characters then we'll face relevant error message whereas playing with entity framework. Here, I've actually developed how to diagnose this issue and then after get red of same issue.

Exception Event

try
{
    // doing here my logic
    _context.savechanges();
}
catch (DbEntityValidationException dbEx)
 {
    foreach (var validationErrors in dbEx.EntityValidationErrors)
      {
         foreach (var validationError in validationErrors.ValidationErrors)
           {
              Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
           }
      }
}

Error Description

DbEntityValidationException was caught
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

Solution

This error happens as a result of data type length was short and I was attempting long data length to insert in to that particular column, after I went into deeply analysis then came up that data length is short in database therefore it needs to extend data storage capability. Whereas increase the length in database and save the database table then attempt once again with same input data then it starts to operate functioning fine. I've increased FirstName data Type length nvarchar(20) to nvarchar(50).

Validation failed for one or more entities
Validation failed for one or more entities

Conclusion

Error Validation failed for one or more entities often happened whereas working in MVC, C#, ASP.Net application using Entity Framework (MS SQL Database, MySQL, Oracle, MongoDB etc). Above mentioned solution in detail would help to resolve your this issue completely, if anymore help required anyone can approach me.

Relevant Reading

Share

Anjan kant

Outstanding journey in Microsoft Technologies (ASP.Net, C#, SQL Programming, WPF, Silverlight, WCF etc.), client side technologies AngularJS, KnockoutJS, Javascript, Ajax Calls, Json and Hybrid apps etc. I love to devote free time in writing, blogging, social networking and adventurous life

Post A Comment:

5 comments:

  1. Hi I have the same problem but in the id field is the one that is giving me problems is int
    // GET: /Empleado/Create
    public ActionResult Create(int? id)
    {

    if (id == null)
    {
    ViewBag.Id_Area = new SelectList(db.SIMAC_Area, "Id", "Nombre_Gerencia");
    ViewBag.Id_Compañia = new SelectList(db.SIMAC_Compañia, "Id", "Nombre_Compañia");
    ViewBag.Id_Departamento = new SelectList(db.SIMAC_Departamento, "Id", "nombre_depto");
    ViewBag.estado = new SelectList(db.SIMAC_Estado, "Id", "Nombre_Estado");
    ViewBag.pais = new SelectList(db.SIMAC_Pais, "PaisCodigo", "PaisNombre");
    ViewBag.Id_Puesto = new SelectList(db.SIMAC_Puesto, "Id", "Nombre_Puesto");
    var SIMAC_Empleado = new SIMAC_Empleado();
    SIMAC_Empleado.fecha_creacion = DateTime.Now;
    SIMAC_Empleado.estatus_empleado = 1;

    db.SIMAC_Empleado.Add(SIMAC_Empleado);
    try
    {
    db.SaveChanges(); //Marca error en la validación
    }
    catch (DbEntityValidationException ex)
    {
    foreach (var entityValidationErrors in ex.EntityValidationErrors)
    {
    foreach (var validationError in entityValidationErrors.ValidationErrors)
    {
    Response.Write("Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage);
    }
    }
    }

    int IdEmpleado = SIMAC_Empleado.Id;
    ViewBag.IdEmpleado = IdEmpleado;

    return RedirectToAction("Edit", "Empleado", new { id = IdEmpleado });
    }
    else {
    SIMAC_Empleado simac_empleado = db.SIMAC_Empleado.Find(id);
    if (simac_empleado == null)
    {
    return HttpNotFound();
    }

    ViewBag.Id_Area = new SelectList(db.SIMAC_Area, "Id", "Nombre_Gerencia",simac_empleado.Id_Area);
    ViewBag.Id_Compañia = new SelectList(db.SIMAC_Compañia, "Id", "Nombre_Compañia",simac_empleado.Id_Compañia);
    ViewBag.Id_Departamento = new SelectList(db.SIMAC_Departamento, "Id", "nombre_depto",simac_empleado.Id_Departamento);
    ViewBag.estado = new SelectList(db.SIMAC_Estado, "Id", "Nombre_Estado",simac_empleado.estado);
    ViewBag.pais = new SelectList(db.SIMAC_Pais, "PaisCodigo", "PaisNombre",simac_empleado.pais);
    ViewBag.Id_Puesto = new SelectList(db.SIMAC_Puesto, "Id", "Nombre_Puesto",simac_empleado.Id_Puesto);
    return View(simac_empleado);
    }
    }

    ReplyDelete
    Replies
    1. did you set identity of primary key in relevant table & also check for data column size (should be restrict data length according to database length size).

      Delete
  2. I'm also having the same problem here. But for sure, my column entity length is very large so running out of space is definitely not my problem source. Any ideas? Thanks!

    ReplyDelete
    Replies
    1. yes, it is clearly indicating that you need to increase identity size from database and also get done changes from front end.

      Delete
  3. thank you sooooo muchhhh,i have been wandering for a long time and couldn't find any solution......thank you :)

    ReplyDelete