title: Entity Framework 5.0, Code First, relationships, fluent API and seeding the database
body: <p>I am using Visual Studio 2010, C# 4.0 and Entity Framework 5.0.</p>
  
  <p>My (simplified) models are:</p>
  
  <pre><code>public class Case
  {
      public int CaseID { get; set; }
      public int CaseStatusID { get; set; }
      public DateTime DateOfNotification { get; set; }        
  
      public User User { get; set; }
  
      public Case()
      {                  
      }
  }
  
  public class User
  {
      public int UserID {get;set;}
      public string Name { get; set; }
  
      public Case Case { get; set; }
  
      public User()
      {
      }
  }
  </code></pre>
  
  <p>The data context is:</p>
  
  <pre><code>public class DataContext : DbContext
  {
      public DbSet&lt;Case&gt; Cases { get; set; }
      public DbSet&lt;User&gt; Users { get; set; }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;Case&gt;()
             .HasKey(m =&gt; m.CaseID)
             .Property(m =&gt; m.CaseID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
  
          modelBuilder.Entity&lt;Case&gt;()
              .HasRequired(m =&gt; m.User)
              .WithRequiredPrincipal(m =&gt; m.Case);
      }
  }
  </code></pre>
  
  <p>And I am seeding the database with:</p>
  
  <pre><code>protected override void Seed(DataContext context)
      {
          Case Case = new Case();
          Case.CaseStatusID = 1;
          Case.DateOfNotification = DateTime.Today;
  
          Case.User = new User();
  
          Case.User.UserID = Case.CaseID;
          Case.User.UsersFamilyNameEnc = "Smith";
          Case.User.UsersGivenNameEnc = "Petra";
  
          context.Cases.AddOrUpdate(Case);         
          context.SaveChanges();
      }
  </code></pre>
  
  <p>Using Migrations with no seed the database deploys with no errors and I can populate the database manually.</p>
  
  <p>If I comment out the <code>User</code> class the database seeds correctly.</p>
  
  <p><strong>THE PROBLEM</strong></p>
  
  <p>As soon as I add the <code>user</code> relationship and run the migration command I get this error</p>
  
  <pre><code>Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
  </code></pre>
  
  <p>I cannot see the EntityValidationErrors and any break points and not reached. </p>
  
  <p>The problem seems to be in the relationship. I have tried different ways of specifying this but the result is always the same.</p>
  
  <p><strong>THE ANSWER</strong></p>
  
  <p><a href="https://stackoverflow.com/questions/5345890/getting-exact-error-type-in-from-dbvalidationexception">Getting exact error type in from DbValidationException</a>
  was the first part of the solution - understanding the error. I then needed to add </p>
  
  <pre><code>catch (DbEntityValidationException dbEx)
  {
      StringBuilder sb = new StringBuilder();
  
      foreach (var validationErrors in dbEx.EntityValidationErrors)
      {
          foreach (var validationError in validationErrors.ValidationErrors)
          {
              Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
              sb.AppendFormat("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
          }
      }
      throw new Exception(sb.ToString());
  }
  </code></pre>
  
  <p>(an amalgm of two other answers) with a try to get the errorto the Package Manager console.</p>
  
  <p>Finally, I found that my 'real' model contained the following:</p>
  
  <pre><code>[NotMapped]
  [Required(ErrorMessage = "The family name/surname is required")]
  [Display(Name = "Family name/surname")]
  public string UserName { get; set; }
  </code></pre>
  
  <p>I thought that the <code>NotMapped</code> would exclude the field but the <code>Required</code> seems to override this. Once I put in a value for this it all started working.</p>