0

I'm trying to update 4 fields on my userprofile table, but i get the following error

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

My UserProfile class is constructed with many [Required] fields but I only display four which are

  1. First name
  2. Surname
  3. Email Address
  4. Website

Now my code currently looks like this

 public void UpdateUserProfile(UserProfile userProfile)
    {
        using (var context = new Context())
        {
            context.UserProfile.Attach(userProfile);

            var entry = context.Entry(userProfile);
            entry.Property(e => e.Firstname).IsModified = true;
            entry.Property(e => e.Surname).IsModified = true;
            entry.Property(e => e.EmailAddress).IsModified = true;
            entry.Property(e => e.UserWebsite).IsModified = true;

            context.SaveChanges();
        }
    }

I've googled the error and from what I have read it seems I need to load everything thats linked to the UserId and then make the modifications? but to me that seems to much over head just to update four fields?

Image

Code Ratchet
  • 5,758
  • 18
  • 77
  • 141
  • 4
    Did you have a look at the exception property called 'EntityValidationErrors' as the exception mentions? – Chief Wiggum Oct 30 '14 at 22:39
  • Certainly did and it says Count = 1 when I open it I get {System.Data.Entity.Validation.DbEntityValidationResult} that's all I can see @JamesBlond – Code Ratchet Oct 30 '14 at 22:41
  • You should be able to see the properties of that object. – jamesSampica Oct 30 '14 at 22:43
  • @Shoe all I can see is what I have written above. – Code Ratchet Oct 30 '14 at 22:43
  • And when you look inside the result? Have a look at the screenshot in this question: http://stackoverflow.com/a/22638624/2360972 – Chief Wiggum Oct 30 '14 at 22:45
  • @JamesBlond just added my exception to the above question – Code Ratchet Oct 30 '14 at 22:51
  • Ah, ok, when you have you exception close the Exception window and add a Watch for the exception. In the Watch window you can dig further into the result. For some reason you can't see the properties of the result in the exception window. If you don't capture the exception you can add a Watch for the param $exception. – Chief Wiggum Oct 30 '14 at 23:47

0 Answers0