我在用代码优先方法播种数据库时遇到了这个错误。

一个或多个实体的验证失败。有关详细信息,请参阅“EntityValidationErrors”属性。

老实说,我不知道如何检查验证错误的内容。Visual Studio向我展示了它是一个包含8个对象的数组,因此有8个验证错误。

这适用于我以前的模型,但我做了一些更改,我将在下面解释:

我有一个名为Status的枚举,我将其更改为名为Status我将类ApplientsPositionHistory更改为具有同一表的2个外键

对不起,代码太长了,但我必须全部粘贴。异常在以下代码的最后一行中引发。

namespace Data.Model
{  
    public class Position
    {
        [DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.Identity)]   
        public int PositionID { get; set; }

        [Required(ErrorMessage = "Position name is required.")]
        [StringLength(20, MinimumLength = 3, ErrorMessage = "Name should not be longer than 20 characters.")]
        [Display(Name = "Position name")]              
        public string name { get; set; }

        [Required(ErrorMessage = "Number of years is required")] 
        [Display(Name = "Number of years")]        
        public int yearsExperienceRequired { get; set; }

        public virtual ICollection<ApplicantPosition> applicantPosition { get; set; }
    }

    public class Applicant
    {
        [DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.Identity)]      
        public int ApplicantID { get; set; }

        [Required(ErrorMessage = "Name is required")] 
        [StringLength(20, MinimumLength = 3, ErrorMessage="Name should not be longer than 20 characters.")]
        [Display(Name = "First and LastName")]
        public string name { get; set; }

        [Required(ErrorMessage = "Telephone number is required")] 
        [StringLength(10, MinimumLength = 3, ErrorMessage = "Telephone should not be longer than 20 characters.")]
        [Display(Name = "Telephone Number")]
        public string telephone { get; set; }

        [Required(ErrorMessage = "Skype username is required")] 
        [StringLength(10, MinimumLength = 3, ErrorMessage = "Skype user should not be longer than 20 characters.")]
        [Display(Name = "Skype Username")]
        public string skypeuser { get; set; }

        public byte[] photo { get; set; }

        public virtual ICollection<ApplicantPosition> applicantPosition { get; set; }
    }

    public class ApplicantPosition
    {
        [Key]
        [Column("ApplicantID", Order = 0)]
        public int ApplicantID { get; set; }

        [Key]
        [Column("PositionID", Order = 1)]
        public int PositionID { get; set; }

        public virtual Position Position { get; set; }

        public virtual Applicant Applicant { get; set; }

        [Required(ErrorMessage = "Applied date is required")] 
        [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
        [Display(Name = "Date applied")]     
        public DateTime appliedDate { get; set; }

        [Column("StatusID", Order = 0)]
        public int StatusID { get; set; }

        public Status CurrentStatus { get; set; }

        //[NotMapped]
        //public int numberOfApplicantsApplied
        //{
        //    get
        //    {
        //        int query =
        //             (from ap in Position
        //              where ap.Status == (int)Status.Applied
        //              select ap
        //                  ).Count();
        //        return query;
        //    }
        //}
    }

    public class Address
    {
        [StringLength(20, MinimumLength = 3, ErrorMessage = "Country should not be longer than 20 characters.")]
        public string Country { get; set; }

        [StringLength(20, MinimumLength = 3, ErrorMessage = "City  should not be longer than 20 characters.")]
        public string City { get; set; }

        [StringLength(50, MinimumLength = 3, ErrorMessage = "Address  should not be longer than 50 characters.")]
        [Display(Name = "Address Line 1")]     
        public string AddressLine1 { get; set; }

        [Display(Name = "Address Line 2")]
        public string AddressLine2 { get; set; }   
    }

    public class ApplicationPositionHistory
    {
        [DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.Identity)]
        public int ApplicationPositionHistoryID { get; set; }

        public ApplicantPosition applicantPosition { get; set; }

        [Column("oldStatusID")]
        public int oldStatusID { get; set; }

        [Column("newStatusID")]
        public int newStatusID { get; set; }

        public Status oldStatus { get; set; }

        public Status newStatus { get; set; }

        [StringLength(500, MinimumLength = 3, ErrorMessage = "Comments  should not be longer than 500 characters.")]
        [Display(Name = "Comments")]
        public string comments { get; set; }

        [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
        [Display(Name = "Date")]     
        public DateTime dateModified { get; set; }
    }

    public class Status
    {
        [DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.Identity)]
        public int StatusID { get; set; }

        [StringLength(20, MinimumLength = 3, ErrorMessage = "Status  should not be longer than 20 characters.")]
        [Display(Name = "Status")]
        public string status { get; set; }
    }
}

使用系统;使用System.Collections.Generic;使用System.Linq;使用System.Text;使用System.Data.Entity;使用System.IO;命名空间Data.Model{公共类HRContextInitializer:DropCreateDatabaseAlways<HRContext>{受保护的覆盖void Seed(HRContext上下文){#地区状态已应用状态=新状态(){Status=“已应用”};状态reviewedByHR=新状态(){Status=“人力资源审核”};状态approvedByHR=新状态(){Status=“已由HR批准”};Status rejectedByHR=新状态(){Status=“被HR拒绝”};分配给技术部门的状态=新状态(){Status=“分配给技术部”};技术部门批准的状态=新状态(){Status=“技术部门批准”};技术部门拒绝的状态=新状态(){Status=“技术部门拒绝”};Status assignedToGeneral Manager=新状态(){Status=“分配给General Manager”};状态approvedByGeneralManager=新状态(){Status=“已由总经理批准”};Status rejectedByGeneralManager=新状态(){Status=“被总经理拒绝”};context.Status.Add(已应用);context.Status.Add(由HR审查);context.Status.Add(已由HR批准);context.Status.Add(被HR拒绝);context.Status.Add(分配给技术部门);context.Status.Add(由技术部门批准);context.Status.Add(被技术部门拒绝);context.Status.Add(分配给GeneralManager);context.Status.Add(由GeneralManager批准);context.Status.Add(被GeneralManager拒绝);#末端区域#区域位置职位netdeveloper=new职位(){name=“.net developer”,所需年经验=5};职位javadeveloper=new Position(){name=“java developer”,yearsExperienceRequired=5};context.Positions.Add(netdeveloper);context.Positions.Add(javadeveloper);#末端区域#地区申请人申请人luis=新申请人(){name=“Luis”,skypeuser=“le.valencia”,电话=“0491732825”,photo=文件.ReadAllBytes(@“C:\Users\LUIS.SIMBIOS\Documents\Visual Studio 2010\Projects\SlnHR\HRRazorForms\Content\pictures\1.jpg”)};申请人john=新申请人(){name=“John”,skypeuser=“jo.valencia”,电话=“3435343543”,photo=文件.ReadAllBytes(@“C:\Users\LUIS.SIMBIOS\Documents\Visual Studio 2010\Projects\SlnHR\HRRazorForms\Content\pictures\2.jpg”)};context.申请人.添加(luis);context.申请人.Add(john);#末端区域#区域应用程序位置ApplicantPosition appicantposition=新的ApplicantPosition(){申请人=luis,位置=网络开发者,appliedDate=日期时间。今天,状态ID=1};ApplicantPosition appicantposition2=新的ApplicantPosition(){申请人=john,位置=javadeveloper,appliedDate=日期时间。今天,状态ID=1}; context.ApplicantsPosition.Add(applicantposition);context.ApplicantsPositions.Add(applicantposition2);#末端区域context.SaveChanges();--->>此处出错}}}


当前回答

使用@Slauma的答案,我制作了一个代码片段(用代码片段包围),以便更好地使用。

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <SnippetTypes>
        <SnippetType>SurroundsWith</SnippetType>
      </SnippetTypes>
      <Title>ValidationErrorsTryCatch</Title>
      <Author>Phoenix</Author>
      <Description>
      </Description>
      <HelpUrl>
      </HelpUrl>
      <Shortcut>
      </Shortcut>
    </Header>
    <Snippet>
      <Code Language="csharp"><![CDATA[try
{
    $selected$ $end$
}
catch (System.Data.Entity.Validation.DbEntityValidationException e)
{
    foreach (var eve in e.EntityValidationErrors)
    {
        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
            eve.Entry.Entity.GetType().Name, eve.Entry.State);
        foreach (var ve in eve.ValidationErrors)
        {
            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                ve.PropertyName, ve.ErrorMessage);
        }
    }
    throw;
}]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

其他回答

当您在catch{…}块中处于调试模式时,打开“QuickWatch”窗口(ctrl+alt+q)并粘贴到其中:

((System.Data.Enventity.Validation.DbEntityValidationException)ex).EEntityValidationErrors

or:

((System.Data.Enventity.Validation.DbEntityValidationException)$exception).EEntityValidationErrors

如果您不在try/catch中或无权访问异常对象。

这将允许您深入到ValidationErrors树中。这是我发现的最简单的方法,可以即时了解这些错误。

请根据数据库字段检查您传递的字段值是否有效。例如,在特定字段中传递的字符数少于数据库表字段中定义的字符数。

如果您只是捕获一个泛型异常,那么将其转换为DbEntityValidationException可能会有好处。这种类型的异常具有Validation Errors属性,如果继续扩展到这些属性,您将发现所有问题。

例如,如果您在捕捉中设置了断点,则可以将以下内容放入手表中:

((System.Data.Entity.Validation.DbEntityValidationException ) ex)

错误的一个例子是,如果一个字段不允许空值,而您有一个空字符串,您会看到它说该字段是必需的。

在调试中,您可以在QuickWatch表达式计算器输入字段中输入:

context.GetValidationErrors()

这是另一种方法,而不是使用foreach循环来查找EntityValidationErrors内部。当然,您可以根据自己的喜好设置邮件格式:

try {
        // your code goes here...
    } 
catch (DbEntityValidationException ex) 
    {
        Console.Write($"Validation errors: {string.Join(Environment.NewLine, ex.EntityValidationErrors.SelectMany(vr => vr.ValidationErrors.Select(err => $"{err.PropertyName} - {err.ErrorMessage}")))}", ex);
        throw;
    }