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

时间:2016-05-12 02:03:09

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

我有以下两个域模型。

[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'属性以获取更多详细信息。

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

{{1}}