Validation Error on SaveChanges() c# entity-framework


My mvc web application contains the following Action method:

public ActionResult Create(SDJoin sdj, FormCollection formValues)
        //code goes here
        repository.InsertOrUpdateSD(sdj.StorageDevice, User.Identity.Name, assetid);
    catch (Exception ex)
        //code goes here
    return View(sdj);

This invokes the following procedure:-

public void InsertOrUpdateSD(TMSStorageDevice sd, string username, long assetid)
    var resource = entities.Resources.AsNoTracking().SingleOrDefault(a => a.RESOURCEID == assetid);
    if (sd.TMSStorageDeviceID == default(int))
        // New entity
        int technologyypeID = GetTechnologyTypeID("Storage Device");
        Technology technology = new Technology
            IsDeleted = true,
            IsCompleted = false,
            TypeID = technologyypeID,
            Tag = "SD" + GetTagMaximumeNumber2(technologyypeID).ToString(),
            StartDate = DateTime.Now,
            IT360ID = assetid


        sd.TMSStorageDeviceID = technology.TechnologyID;

The following is my sample class:-

public partial class TMSStorageDevice
    public int TMSStorageDeviceID { get; set; }
    public string Name { get; set; }
    public Nullable<decimal> size { get; set; }
    public int RackID { get; set; }
    public string CustomerName { get; set; }
    public string Comment { get; set; }
    public byte[] timestamp { get; set; }

    public virtual Technology Technology { get; set; }
    public virtual TMSRack TMSRack { get; set; }

however, if I use the Create action method, I receive the subsequent exception:

System.Data.Entity.Validation.DbEntityValidationException was caught
  Message=Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
       at System.Data.Entity.Internal.InternalContext.SaveChanges()
       at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
       at System.Data.Entity.DbContext.SaveChanges()
       at TMS.Models.Repository.Save() in c:\Users\Administrator\Documents\Visual Studio 2012\Projects\TMS\TMS\Models\Repository.cs:line 1926
       at TMS.Controllers.StorageDeviceController.Create(SDJoin sdj, FormCollection formValues) in c:\Users\Administrator\Documents\Visual Studio 2012\Projects\TMS\TMS\Controllers\StorageDeviceController.cs:line 160

I have checked my code twice, and everything appears to be working well. Can somebody tell me what is wrong? Thanks

10/22/2013 3:11:00 PM

Accepted Answer

You have not displayed theSave() method but if you can add code to it like this, you'll get an exception with all the information you need.

catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
    Exception raise = dbEx;
    foreach (var validationErrors in dbEx.EntityValidationErrors)
        foreach (var validationError in validationErrors.ValidationErrors)
            string message = string.Format("{0}:{1}", 
            // raise a new exception nesting
            // the current instance as InnerException
            raise = new InvalidOperationException(message, raise);
    throw raise;
10/22/2013 2:59:05 PM

Popular Answer

Although the question has now been answered, I still hope that someone will find this information to be helpful.

  1. The Your variable didn't pass model validation, which resulted in an error. is most likely (and in fact, based on your remarks)
  2. The QuickWatch window can be used to look at the exception in order to see the mistakes you weren't able to observe (not modifying the code). The issue is that EntityValidationErrors are not displayed in the 'View Details' window, which may be opened from the exception tooltip. Additionally, you cannot view any exception variables in QuickWatch without a catch block. Fortunately, QuickWatch supports pseudovariables. Therefore, you can quickly check the currently unhandled exception in the QuickWatch window by using the pseudovariable "$exception". (Please, see screenshots below) both zzz-26 and zzz-30

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow