【问题标题】:Validation failed for one or more entities. See EntityValidationErrors property for more details一个或多个实体的验证失败。有关详细信息,请参阅 EntityValidationErrors 属性
【发布时间】:2016-05-12 02:03:09
【问题描述】:

我有以下两个领域模型。

[Table("tblActual_AgencyProfile")]
public class AgencyModel
{
    public AgencyModel()
    {
        CategoryItemList = new List<CategoryModel>();
        user = new UserProfile();
    }

    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }
    public string UniqueURL { get; set; }
    public string Name { get; set; }
    public string Title { get; set; }
    public string Address { get; set; }
    public string Phone { get; set; }
    public string Fax { get; set; }
    public string Email { get; set; }
    public string ABN { get; set; }
    public string CAN { get; set; }
    public string PhotoURL { get; set; }
    public string Summary { get; set; }
    public string Specialities { get; set; }
    public string LocationsServices { get; set; }
    public DateTime? ImportedDateTime { get; set; }
    public int? UserID { get; set; }

    [ForeignKey("UserID")]
    public UserProfile user { get; set; }

    public ICollection<CategoryModel> CategoryItemList { get; set; }
}


[Table("UserProfile")]
public class UserProfile
{
    public UserProfile()
    {
        Actual_Category_List = new List<CategoryViewModel>();
    }

    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public string UserName { get; set; }
    [Required]
    public string UserType { get; set; }
    [RequiredIfAttribute("UserType", "Agency")]
    public string AgencyName { get; set; }
    public string Title { get; set; }
    [Required]
    public string FirstName { get; set; }
    [Required]
    public string LastName { get; set; }
    [Required]
    public string Address { get; set; }
    [Required]
    public string Suburb { get; set; }
    [Required]
    public string State { get; set; }
    [Required]
    public string PostCode { get; set; }
    [Required]
    public string Phone { get; set; }
    public string Fax { get; set; }
    public string Mobile { get; set; }
    [EmailAddress]
    [Required]
    public string EmailAddress { get; set; }
    public ICollection<CategoryViewModel> Actual_Category_List { get; set; }
}

然后我的 Repository 类中有一个 Update 方法

public void EditDetails(AgencyViewModel model)
{
    try
    {
        using (WebScrapperDBContext contex = new WebScrapperDBContext())
        {
            using (TransactionScope scope = new TransactionScope())
            {
                AgencyModel agency = (from tb in contex.agencies where tb.ID == model.ID select tb).SingleOrDefault();
                agency.Address = model.Address;
                agency.Email = model.Email;
                agency.Fax = model.Fax;
                agency.LocationsServices = model.LocationsServices;
                agency.Name = model.Name;
                agency.Phone = model.Phone;
                if (!string.IsNullOrEmpty(agency.PhotoURL))
                    agency.PhotoURL = model.PhotoURL;
                agency.Specialities = model.Specialities;
                agency.Summary = model.Summary;

                agency.user.Address = model.Address;
                agency.user.AgencyName = model.Name;
                agency.user.EmailAddress = model.Email;
                agency.user.Fax = model.Fax;
                agency.user.Mobile = model.Mobile;
                agency.user.Phone = model.Phone;
                agency.user.PostCode = model.PostCode;
                agency.user.State = model.State;
                agency.user.Suburb = model.Suburb;

                contex.SaveChanges();

                scope.Complete();
            }
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

如您所见,我不想更新UserProfile 中的所有字段(例如:FirstNameLastName)。当我运行EditDetails() 方法时,它会引发异常

一个或多个实体的验证失败。看 'EntityValidationErrors' 属性以获取更多详细信息。

这可能是因为我跳过了域模型中的一些必填字段。我该如何解决这个问题?

【问题讨论】:

  • See 'EntityValidationErrors' property for more details告诉你错误的什么?
  • @StephenMuecke 我刚刚检查了 EntityValidationErrors。正如我认为的那样,它会触发必填字段的验证错误。
  • 您可能需要在查询中包含.Include(x =&gt; x.user),以便user 属性填充其现有值,这样您就可以只更新您需要的内容。
  • 或者,您可以根据this answer排除属性更新
  • 嗨 @StephenMuecke 添加 .Include("user") 工作谢谢。如果可以,请发表您的评论作为答案。

标签: c# asp.net entity-framework asp.net-mvc-4 ef-code-first


【解决方案1】:

该错误表明user 的属性值未包含在您查询返回的模型中。您可以通过在查询中添加 .Include(x =&gt; x.user) 来强制填充这些内容。那么只需要更新那些你需要的属性。

【讨论】:

    【解决方案2】:

    您可以使用此配置在更新时忽略验证。在您的情况下,您只需要更新一些属性而不是全部。

    context.Configuration.ValidateOnSaveEnabled = false;
    context.SaveChanges();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-20
      • 2011-12-09
      • 1970-01-01
      • 2021-05-25
      • 2014-02-24
      • 2018-01-10
      • 2013-05-13
      • 1970-01-01
      相关资源
      最近更新 更多