chamara
chamara

Reputation: 12711

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

I have the following two domain Models.

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

Then I have an Update method in my Repository class

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

As you can see, I don't want to update all the fields in UserProfile (Ex: FirstName, LastName). When I run the EditDetails() method it throws an exception

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

This could be because I'm skipping some required fields in the domain model. How can I solve this issue?

Upvotes: 0

Views: 1817

Answers (2)

Edward N
Edward N

Reputation: 997

You can use this configuration to ignore the validation when update. In your case you only need to update some properties instead of all.

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

Upvotes: 0

user3559349
user3559349

Reputation:

The error suggests the values of the properties of user are not being included in your the model you query is returning. You can force these to be populated by adding .Include(x => x.user) to your query. Then it is only necessary to update those properties you need.

Upvotes: 1

Related Questions