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

后端 未结 2 1531
你的背包
你的背包 2020-12-24 03:55

I get this error:

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

when i try to updat

2条回答
  •  庸人自扰
    2020-12-24 04:29

    I don't know why writing to the VS output window doesn't work and how to make it work. But as a last resort just write the errors into a text file which should work independent of the type of application you have:

    try
    {
        context.SaveChanges();
    }
    catch (System.Data.Entity.Validation.DbEntityValidationException e)
    {
        var outputLines = new List();
        foreach (var eve in e.EntityValidationErrors)
        {
            outputLines.Add(string.Format(
                "{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:",
                DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State));
            foreach (var ve in eve.ValidationErrors)
            {
                outputLines.Add(string.Format(
                    "- Property: \"{0}\", Error: \"{1}\"",
                    ve.PropertyName, ve.ErrorMessage));
            }
        }
        //Write to file
        System.IO.File.AppendAllLines(@"c:\temp\errors.txt", outputLines);
        throw;
    
        // Showing it on screen
        throw new Exception( string.Join(",", outputLines.ToArray()));
    
    }
    

提交回复
热议问题