0

I have a database driven asp.net mvc application It has many foreign key relationships but for some reason for one of the tables I'm having issues with the foreign keys and the drop down boxes, when I want to create a new record I enter all the fields and I select a value in the drop down boxes which are foreign keys but when i click save it throws this error.

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. Code First

I know that it's the dropdownlist that causes this because I ran a try catch that made a .txt file which outputs the errors specific details as the stack trace hasn't got much detail. I've not done anything different to normal.

I've done this as per in the model

    public virtual Employee Employee { get; set; }
    public virtual Employee Employee1 { get; set; }
    public virtual Fuel_Card Fuel_Card { get; set; }
    public virtual Vehicle Vehicle { get; set; }

This as per in the controllers 1st create method

    public ActionResult Create()
    {
        ViewBag.emp1 = new SelectList(db.Employees, "Employee_ID", "Employee_ID");
        ViewBag.emp2 = new SelectList(db.Employees, "Employee_ID", "Employee_ID");
        ViewBag.FuelCardNumber = new SelectList(db.Fuel_Card, "Fuel_Card_Number", "Fuel_Card_Number");
        ViewBag.Registration = new SelectList(db.Vehicles, "Registration", "Registration");
        return View();
    }

This in the 2nd

        ViewBag.emp1 = new SelectList(db.Employees, "Employee_ID", "Employee_ID", fuel_allocation.Requesting_Employee);
        ViewBag.emp2 = new SelectList(db.Employees, "Employee_ID", "Employee_ID", fuel_allocation.Authorising_Employee);
        ViewBag.FuelCardNumber = new SelectList(db.Fuel_Card, "Fuel_Card_Number", "Fuel_Card_Number", fuel_allocation.Fuel_Card_Number);
        ViewBag.Registration = new SelectList(db.Vehicles, "Registration", "Registration", fuel_allocation.Registration_Number);

This inside the create view

    <div class="editor-field">
        @Html.DropDownList("emp2", String.Empty)
        @Html.ValidationMessageFor(model => model.Authorising_Employee)
    </div>

I was wondering if anyone has had this issue before? I've tried allsorts of different things, the foreign key relationship works when i use EditorFor but it always says it's empty when I try submit it when I use the dropdownlist.

I thought it might be using the same database field for 2 select lists but thats not the case as I've tested them individually

Here is the error I get when i output to file

02/01/2014 14:52:56: Entity of type "Fuel_Allocation" in state "Added" has the following validation errors: - Property: "Authorising_Employee", Error: "The Authorising_Employee field is required."

Crouch
  • 846
  • 3
  • 17
  • 32
  • If you use **Entity Framework** you can have a look at my answer on [Solution for “Validation failed for one or more entities. See 'EntityValidationErrors' property for more details](http://stackoverflow.com/questions/21486072/solution-for-validation-failed-for-one-or-more-entities-see-entityvalidatione/29031857#29031857). Hope this helps... – Murat Yıldız Jan 26 '16 at 23:14

1 Answers1

1

The EntityValidationErrors should contain all the detail that you need. Try adding this code into your derived Context class:

public override int SaveChanges()
{
    try
    {
        base.SaveChanges();
    }
    catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
    {
        Exception exception = dbEx;
        foreach (var validationErrors in dbEx.EntityValidationErrors)
        {
            foreach (var validationError in validationErrors.ValidationErrors)
            {
                string message = string.Format("{0}:{1}", 
                    validationErrors.Entry.Entity.ToString(),
                    validationError.ErrorMessage);

                //create a new exception inserting the current one
                //as the InnerException
                exception = new InvalidOperationException(message, exception);
            }
        }
        throw exception;
    }
}
qujck
  • 14,388
  • 4
  • 45
  • 74
  • It already provides the information I need but it doesn't make any sense as to why the error is occurring there is a value being selected but it seems to disagree – Crouch Jan 02 '14 at 14:46
  • @Crouch if you already have the details of the validation errors would you mind adding them to the question as they are vital to assisting you with the underlying issue. – qujck Jan 02 '14 at 14:53
  • 1
    Ive added it to the orginal question now. Cheers – Crouch Jan 02 '14 at 14:55
  • Did you ever get anywhere with this @Crouch ? I'm currently experiencing the same issue with a required field but it's computed. – Rob Oct 08 '14 at 04:41
  • @Rob Sorry I'm afraid I can't remember now as this particular project got parked some months ago – Crouch Oct 14 '14 at 11:20