一个或多个实体的验证失败。(Validation failed for one or more entities. See 'EntityValidationErrors' pro

编程入门 行业动态 更新时间:2024-10-27 10:28:00
一个或多个实体的验证失败。(Validation failed for one or more entities. See 'EntityValidationErrors' property for more details)

当我的数据库以代码第一种方式播种时,我遇到了这个错误。

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

说实话,我不知道如何检查验证错误的内容,Visual Studio向我显示它有一个有8个对象的数组。 所以8验证错误。

这是我以前的模型,但是我做了一些修改,我下面解释:

我有一个名为Status的枚举,我将其更改为一个名为Status的类 我把类别ApplicantsPositionHistory更改为有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; } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Entity; using System.IO; namespace Data.Model { public class HRContextInitializer : DropCreateDatabaseAlways<HRContext> { protected override void Seed(HRContext context) { #region Status Status applied = new Status() { status = "Applied" }; Status reviewedByHR = new Status() { status = "Reviewed By HR" }; Status approvedByHR = new Status() { status = "Approved by HR" }; Status rejectedByHR = new Status() { status = "Rejected by HR" }; Status assignedToTechnicalDepartment = new Status() { status = "Assigned to Technical Department" }; Status approvedByTechnicalDepartment = new Status() { status = "Approved by Technical Department" }; Status rejectedByTechnicalDepartment = new Status() { status = "Rejected by Technical Department" }; Status assignedToGeneralManager = new Status() { status = "Assigned to General Manager" }; Status approvedByGeneralManager = new Status() { status = "Approved by General Manager" }; Status rejectedByGeneralManager = new Status() { status = "Rejected by General Manager" }; context.Status.Add(applied); context.Status.Add(reviewedByHR); context.Status.Add(approvedByHR); context.Status.Add(rejectedByHR); context.Status.Add(assignedToTechnicalDepartment); context.Status.Add(approvedByTechnicalDepartment); context.Status.Add(rejectedByTechnicalDepartment); context.Status.Add(assignedToGeneralManager); context.Status.Add(approvedByGeneralManager); context.Status.Add(rejectedByGeneralManager); #endregion #region Position Position netdeveloper = new Position() { name = ".net developer", yearsExperienceRequired = 5 }; Position javadeveloper = new Position() { name = "java developer", yearsExperienceRequired = 5 }; context.Positions.Add(netdeveloper); context.Positions.Add(javadeveloper); #endregion #region Applicants Applicant luis = new Applicant() { name = "Luis", skypeuser = "le.valencia", telephone = "0491732825", photo = File.ReadAllBytes(@"C:\Users\LUIS.SIMBIOS\Documents\Visual Studio 2010\Projects\SlnHR\HRRazorForms\Content\pictures\1.jpg") }; Applicant john = new Applicant() { name = "John", skypeuser = "jo.valencia", telephone = "3435343543", photo = File.ReadAllBytes(@"C:\Users\LUIS.SIMBIOS\Documents\Visual Studio 2010\Projects\SlnHR\HRRazorForms\Content\pictures\2.jpg") }; context.Applicants.Add(luis); context.Applicants.Add(john); #endregion #region ApplicantsPositions ApplicantPosition appicantposition = new ApplicantPosition() { Applicant = luis, Position = netdeveloper, appliedDate = DateTime.Today, StatusID = 1 }; ApplicantPosition appicantposition2 = new ApplicantPosition() { Applicant = john, Position = javadeveloper, appliedDate = DateTime.Today, StatusID = 1 }; context.ApplicantsPositions.Add(appicantposition); context.ApplicantsPositions.Add(appicantposition2); #endregion context.SaveChanges(); --->> Error here } } }

I am having this error when seeding my database with code first approach.

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

To be honest I don't know how to check the content of the validation errors. Visual Studio shows me that it's an array with 8 objects, so 8 validation errors.

This was working with my previous model, but I made a few changes that I explain below:

I had an enum called Status, I changed it to a class called Status I changed the class ApplicantsPositionHistory to have 2 foreign key to the same table

Excuse me for the long code, but I have to paste it all. The exception is thrown in the last line of the following code.

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; } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Entity; using System.IO; namespace Data.Model { public class HRContextInitializer : DropCreateDatabaseAlways<HRContext> { protected override void Seed(HRContext context) { #region Status Status applied = new Status() { status = "Applied" }; Status reviewedByHR = new Status() { status = "Reviewed By HR" }; Status approvedByHR = new Status() { status = "Approved by HR" }; Status rejectedByHR = new Status() { status = "Rejected by HR" }; Status assignedToTechnicalDepartment = new Status() { status = "Assigned to Technical Department" }; Status approvedByTechnicalDepartment = new Status() { status = "Approved by Technical Department" }; Status rejectedByTechnicalDepartment = new Status() { status = "Rejected by Technical Department" }; Status assignedToGeneralManager = new Status() { status = "Assigned to General Manager" }; Status approvedByGeneralManager = new Status() { status = "Approved by General Manager" }; Status rejectedByGeneralManager = new Status() { status = "Rejected by General Manager" }; context.Status.Add(applied); context.Status.Add(reviewedByHR); context.Status.Add(approvedByHR); context.Status.Add(rejectedByHR); context.Status.Add(assignedToTechnicalDepartment); context.Status.Add(approvedByTechnicalDepartment); context.Status.Add(rejectedByTechnicalDepartment); context.Status.Add(assignedToGeneralManager); context.Status.Add(approvedByGeneralManager); context.Status.Add(rejectedByGeneralManager); #endregion #region Position Position netdeveloper = new Position() { name = ".net developer", yearsExperienceRequired = 5 }; Position javadeveloper = new Position() { name = "java developer", yearsExperienceRequired = 5 }; context.Positions.Add(netdeveloper); context.Positions.Add(javadeveloper); #endregion #region Applicants Applicant luis = new Applicant() { name = "Luis", skypeuser = "le.valencia", telephone = "0491732825", photo = File.ReadAllBytes(@"C:\Users\LUIS.SIMBIOS\Documents\Visual Studio 2010\Projects\SlnHR\HRRazorForms\Content\pictures\1.jpg") }; Applicant john = new Applicant() { name = "John", skypeuser = "jo.valencia", telephone = "3435343543", photo = File.ReadAllBytes(@"C:\Users\LUIS.SIMBIOS\Documents\Visual Studio 2010\Projects\SlnHR\HRRazorForms\Content\pictures\2.jpg") }; context.Applicants.Add(luis); context.Applicants.Add(john); #endregion #region ApplicantsPositions ApplicantPosition appicantposition = new ApplicantPosition() { Applicant = luis, Position = netdeveloper, appliedDate = DateTime.Today, StatusID = 1 }; ApplicantPosition appicantposition2 = new ApplicantPosition() { Applicant = john, Position = javadeveloper, appliedDate = DateTime.Today, StatusID = 1 }; context.ApplicantsPositions.Add(appicantposition); context.ApplicantsPositions.Add(appicantposition2); #endregion context.SaveChanges(); --->> Error here } } }

更多推荐

本文发布于:2023-08-06 08:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1445044.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   实体   Validation   failed   details

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!