title: Entity Framework Complex Type Validation (DB First)
body: <p>I'm currently using Entity framework 6 to interface between the user interface and the back end database, however I have a problem with validation on fields in a complex type. I'm using the database first approach.</p>
  
  <p>I created my model based on existing database, then converted fields into a complex type. </p>
  
  <p>For example, user model.</p>
  
  <pre><code>public partial class User {
      public User() {
          this.DeliveryAddress = new Address();
          this.InvoiceAddress = new Address();
      }
  
      public Address DeliveryAddress { get; set; }
      public Address InvoiceAddress { get; set; }
  }
  </code></pre>
  
  <p>Complex type </p>
  
  <pre><code>public partial class Address {
      public string Address1 { get; set; }
      public string Address2 { get; set; }
      public string CompanyName { get; set; }
      public string Country { get; set; }
      public string County { get; set; }
      public string Firstname { get; set; }
      public string Postcode { get; set; }
      public string Surname { get; set; }
      public string TownCity { get; set; }
  }
  </code></pre>
  
  <p>I mapped the table correctly in the model browser and everything is compiling. However when trying to save it throws an error that validation failed. After a bit of investigation it turns out for some reason all the complex type fields are required. Which I find strange as they are strings which can be null and even in database the fields are nullable. As a work around I created a partial class of Address with a constructor to initialize all the fields to an empty string which is a temporary work around. However this isn't ideal.</p>
  
  <p>Code which saves the details. (Mvc Action)</p>
  
  <pre><code>public ActionResult UpdateDetails(UserDetailsViewModel info) {
      try {
          if (ModelState.IsValid) {
  
              var user = db.Users.Find(info.UserID);
              if (user != null) {
                  user.DeliveryAddress = new Data.Address {
                      CompanyName = info.DeliveryCompanyName,
                      Address1 = info.DeliveryAddress1,
                      Address2 = info.DeliveryAddress2,
                      Firstname = info.DeliveryFirstname,
                      Postcode = info.DeliveryPostcode,
                      Surname = info.DeliverySurname,
                      TownCity = info.DeliveryCity,
                  };
                  user.InvoiceAddress = new Data.Address();
  
                  if (!info.IsInvoiceAddress) {
                      user.InvoiceAddress = new Data.Address {
                          Address1 = info.InvoiceAddress1,
                          Address2 = info.InvoiceAddress2,
                          Firstname = info.InvoiceFirstname,
                          Postcode = info.InvoicePostcode,
                          Surname = info.InvoiceSurname,
                          TownCity = info.InvoiceCity,
                          CompanyName = info.InvoiceCompanyName
                      };
                  }
  
                  db.SaveChanges();
              }
  
              return Json(new { Msg = "Ok" });
          }
      } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx) {
          foreach (var valErs in dbEx.EntityValidationErrors) {
              foreach (var valEr in valErs.ValidationErrors) {
                  System.Diagnostics.Trace.TraceInformation("Property: {0} Error: {1}", valEr.PropertyName, valEr.ErrorMessage);
              }
          }
  
          return Json(new { Msg = "Err" });
      } catch {
          return Json(new { Msg = "Err" });
      }
  
      return PartialView("UserDetailsForm", info);
  }
  </code></pre>
  
  <p>Has anyone come across a similar situation? Maybe Im missing something which needs to be configured. </p>