简体   繁体   中英

Can't update data in database because of Entity validation exception - ASP.Net MVC

I wish to update data found in three related tables in the database. I'm actually sending all the needed data to the database, but can't succeed in updating them. I get the [DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.] SQL Exception.

Below is what I actually have

ViewModel ChauffeurVM:

public class ChauffeurVM
{
    public int ChauffeurId { get; set; }

    public virtual PERSONNE Personne { get; set; }
    public virtual PIECEIDENTITE PieceIdentite { get; set; }
    public string NumeroPermis { get; set; }
}

Controller:

            public ActionResult ModifierChauffeur(ChauffeurVM ChauffeurVM, HttpPostedFileBase postedPhoto, string Sexe)
    {
        CHAUFFEUR chauffeur = new CHAUFFEUR();

        ChauffeurVM.Personne.Sexe = Sexe;

        using (IDAL dal = new Dal())
        {
            ChauffeurVM.Personne.Photo = dal.UploadandGetImagePath(postedPhoto);
            chauffeur.ChauffeurId = dal.UpdateChauffeur(ChauffeurVM);
            return RedirectToAction("ListeChauffeur");
        }

    }

Dal method:

    public int UpdateChauffeur(ChauffeurVM chauffeur)
    {
        CHAUFFEUR c = new CHAUFFEUR();
        try
        {
            c = ChauffeurParId(chauffeur.ChauffeurId);

            c.NumeroPermis = chauffeur.NumeroPermis;

            bdd.Entry(c).State = EntityState.Modified;
            bdd.SaveChanges();
        }
        catch
        {

            throw;
        }
        //Try to assign the value chauffeur.Personne.PersonneId to the pId
        int pId = chauffeur.Personne.PersonneId;


        c.Personne = new PERSONNE();
        PERSONNE p = detailsChauffeurparPersonneId(chauffeur.Personne.PersonneId);
        try
        {
            if (p != null)
            {
                p.Nom = chauffeur.Personne.Nom;
                p.Prenom = chauffeur.Personne.Prenom;
                p.Sexe = chauffeur.Personne.Sexe;
                p.Telephone = chauffeur.Personne.Telephone;
                p.Photo = chauffeur.Personne.Photo;
                p.LieuNaissance = chauffeur.Personne.LieuNaissance;
                p.DateNaissance = chauffeur.Personne.DateNaissance;
                p.CodePostal = chauffeur.Personne.CodePostal;
                p.Adresse = chauffeur.Personne.Adresse;
                p.Email = chauffeur.Personne.Email;
                p.AdresseBoulot = chauffeur.Personne.AdresseBoulot;
                p.AdresseDomicile = chauffeur.Personne.AdresseDomicile;
                p.PersonneId = chauffeur.Personne.PersonneId;

                bdd.Entry(p).State = EntityState.Modified;
                bdd.SaveChanges();
            }
            else
            {

            }

        }
        catch
        {

            throw;
        }

        try
        {
            PIECEIDENTITE pi = detailsPieceIdentiteparPersonneId(chauffeur.Personne.PersonneId);

            pi.NumeroPiece = chauffeur.NumeroPiece;
            pi.LieuDelivrance = chauffeur.LieuDelivrance;
            pi.DateDelivrance = chauffeur.DateDelivrance;
            pi.DateExpiration = chauffeur.DateExpiration;
            pi.Autorite = chauffeur.Autorite;

            bdd.Entry(pi).State = EntityState.Modified;
            bdd.SaveChanges();
        }
        catch
        {

            throw;
        }
        return c.ChauffeurId;
    }

I expect to update the data in the database. But I get the following exception : [DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.]

When I add breakpoints, I succeed in seing all the data send from the form. I can't figure out which field has a null value.

Kindly help, please!

You have a DbEntityValidationException that should point you to the member causing this issue. You should catch the DbEntityValidationException as follows to get the validation messages. Look in the EntityValidationErrors list for further information.

try
{
}
catch(DbEntityValidationException ex)
{
   var firstErrorMessage = ex.EntityValidationErrors.First().ValidationErrors.First()
                   .ErrorMessage;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM