title: Entity Framework 5.0 (C#): Seed method only run if Update-Database is run twice
body: <p>I have the following model:</p>
  
  <pre><code>public class User
  {
      public int UserId { get; set; }
      public string EmailAddress { get; set; }
      public byte[] PasswordHash { get; set; }
      public byte[] PasswordSalt { get; set; }
  
      public virtual List&lt;Role&gt; Roles { get; set; }
      public virtual List&lt;Job&gt; Jobs { get; set; }
      public virtual List&lt;Project&gt; Projects { get; set; }
      public virtual List&lt;Submission&gt; Submissions { get; set; }
      public virtual List&lt;PROMembership&gt; PROMemberships { get; set; }
  
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public string Address { get; set; }
      public string ZipCode { get; set; }
      public string City { get; set; }
      public string Country { get; set; }
      public string Phone { get; set; }
      public string Notes { get; set; }
      public Guid? PasswordRetrievalToken { get; set; }
  
      [NotMapped]
      [Display(Name = "Full name")]
      public string FullName
      {
          get
          {
              if ((FirstName != null &amp;&amp; FirstName.Length &gt; 0) || (LastName != null &amp;&amp; LastName.Length &gt; 0))
              {
                  return (FirstName + " " + LastName).Trim();
              }
              else
              {
                  return EmailAddress;
              }
          }
      }
  }
  </code></pre>
  
  <p>With the following fluent configuration class:</p>
  
  <pre><code>public class UserConfiguration : EntityTypeConfiguration&lt;User&gt;
  {
      public const int EmailAddressMinLength = 3;
      public const int EmailAddressMaxLength = 62;
      public const int FirstNameMaxLength = 50;
      public const int LastNameMaxLength = 50;
      public const int AddressMaxLength = 100;
      public const int ZipCodeMaxLength = 20;
      public const int CityMaxLength = 50;
      public const int CountryMaxLength = 50;
      public const int PhoneMaxLength = 50;
      public const int NotesMaxLength = 1000;
  
      public UserConfiguration()
      {
          ToTable("Users");
          HasKey(u =&gt; u.UserId).Property(u =&gt; u.UserId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
          Property(u =&gt; u.EmailAddress).IsRequired();
          Property(u =&gt; u.EmailAddress).HasMaxLength(EmailAddressMaxLength);
          Property(u =&gt; u.PasswordHash).IsRequired();
          Property(u =&gt; u.PasswordSalt).IsRequired();
          Property(u =&gt; u.FirstName).HasMaxLength(FirstNameMaxLength);
          Property(u =&gt; u.LastName).HasMaxLength(LastNameMaxLength);
          Property(u =&gt; u.Address).HasMaxLength(AddressMaxLength);
          Property(u =&gt; u.ZipCode).HasMaxLength(ZipCodeMaxLength);
          Property(u =&gt; u.City).HasMaxLength(CityMaxLength);
          Property(u =&gt; u.Country).HasMaxLength(CountryMaxLength);
          Property(u =&gt; u.Phone).HasMaxLength(PhoneMaxLength);
          Property(u =&gt; u.Notes).HasMaxLength(NotesMaxLength);
          Property(u =&gt; u.PasswordRetrievalToken).IsOptional(); // somehow the DB is generated with this field required if this is ommited
          HasMany(u =&gt; u.Roles).WithMany(r =&gt; r.Users).Map(m =&gt; { m.MapLeftKey("UserId"); m.MapRightKey("RoleId"); m.ToTable("UsersInRoles"); });
          HasMany(u =&gt; u.Jobs).WithMany(j =&gt; j.Composers).Map(m =&gt; { m.MapLeftKey("UserId"); m.MapRightKey("JobId"); m.ToTable("ComposersInJobs"); });
      }
  }
  </code></pre>
  
  <p>And with this context configuration class:</p>
  
  <pre><code>internal sealed class Configuration : DbMigrationsConfiguration&lt;WebProject.DataAccess.WebProjectContext&gt;
  {
      private readonly bool _pendingMigrations;
  
      public Configuration()
      {
          AutomaticMigrationsEnabled = true;
          AutomaticMigrationDataLossAllowed = true;
          var migrator = new DbMigrator(this);
          _pendingMigrations = migrator.GetPendingMigrations().Any();
      }
  
      protected override void Seed(WebProject.DataAccess.WebProjectContext context)
      {
          if (!_pendingMigrations)
              return;
          // imagine seeding code here
  </code></pre>
  
  <p>So far, so good. When I execute Update-Database, everything works as expected and the Database is created/updated correctly and in the end, the seed method in my Context-Configuration class is executed.</p>
  
  <p>Now I had to add some fields to the model. Using Add-Migration Command:</p>
  
  <pre><code>public partial class AddClientRepresentation : DbMigration
  {
      public override void Up()
      {
          AddColumn("dbo.Users", "MiddleName", c =&gt; c.String(maxLength: 50));
          AddColumn("dbo.Users", "Company", c =&gt; c.String(maxLength: 50));
          AddColumn("dbo.Users", "State", c =&gt; c.String(maxLength: 50));
          AddColumn("dbo.Users", "SecondaryPhone", c =&gt; c.String(maxLength: 50));
          AddColumn("dbo.Users", "SecondaryEmailAddress", c =&gt; c.String(maxLength: 62));
      }
  
      public override void Down()
      {
          DropColumn("dbo.Users", "SecondaryEmailAddress");
          DropColumn("dbo.Users", "SecondaryPhone");
          DropColumn("dbo.Users", "State");
          DropColumn("dbo.Users", "Company");
          DropColumn("dbo.Users", "MiddleName");
      }
  }
  </code></pre>
  
  <p>And so I have the following classes:</p>
  
  <pre><code>public class User
  {
      public int UserId { get; set; }
      public string EmailAddress { get; set; }
      public byte[] PasswordHash { get; set; }
      public byte[] PasswordSalt { get; set; }
  
      public virtual List&lt;Role&gt; Roles { get; set; }
      public virtual List&lt;Job&gt; Jobs { get; set; }
      public virtual List&lt;Project&gt; Projects { get; set; }
      public virtual List&lt;Submission&gt; Submissions { get; set; }
      public virtual List&lt;PROMembership&gt; PROMemberships { get; set; }
  
      public string FirstName { get; set; }
      public string MiddleName { get; set; }
      public string LastName { get; set; }
      public string Company { get; set; }
      public string Address { get; set; }
      public string ZipCode { get; set; }
      public string City { get; set; }
      public string State { get; set; }
      public string Country { get; set; }
      public string Phone { get; set; }
      public string SecondaryPhone { get; set; }
      public string SecondaryEmailAddress { get; set; }
      public string Notes { get; set; }
      public Guid? PasswordRetrievalToken { get; set; }
  
      [NotMapped]
      [Display(Name = "Full name")]
      public string FullName
      {
          get
          {
              if ((FirstName != null &amp;&amp; FirstName.Length &gt; 0) || (LastName != null &amp;&amp; LastName.Length &gt; 0))
              {
                  return (FirstName + " " + LastName).Trim();
              }
              else
              {
                  return EmailAddress;
              }
          }
      }
  }
  
  
  public class UserConfiguration : EntityTypeConfiguration&lt;User&gt;
  {
      public const int EmailAddressMinLength = 3;
      public const int EmailAddressMaxLength = 62;
      public const int FirstNameMaxLength = 50;
      public const int MiddleNameMaxLength = 50;
      public const int LastNameMaxLength = 50;
      public const int CompanyMaxLength = 50;
      public const int AddressMaxLength = 100;
      public const int ZipCodeMaxLength = 20;
      public const int CityMaxLength = 50;
      public const int StateMaxLength = 50;
      public const int CountryMaxLength = 50;
      public const int PhoneMaxLength = 50;
      public const int NotesMaxLength = 1000;
  
      public UserConfiguration()
      {
          ToTable("Users");
          HasKey(u =&gt; u.UserId).Property(u =&gt; u.UserId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
          Property(u =&gt; u.EmailAddress).IsRequired();
          Property(u =&gt; u.EmailAddress).HasMaxLength(EmailAddressMaxLength);
          Property(u =&gt; u.PasswordHash).IsRequired();
          Property(u =&gt; u.PasswordSalt).IsRequired();
          Property(u =&gt; u.FirstName).HasMaxLength(FirstNameMaxLength);
          Property(u =&gt; u.MiddleName).HasMaxLength(MiddleNameMaxLength);
          Property(u =&gt; u.LastName).HasMaxLength(LastNameMaxLength);
          Property(u =&gt; u.Company).HasMaxLength(CompanyMaxLength);
          Property(u =&gt; u.Address).HasMaxLength(AddressMaxLength);
          Property(u =&gt; u.ZipCode).HasMaxLength(ZipCodeMaxLength);
          Property(u =&gt; u.City).HasMaxLength(CityMaxLength);
          Property(u =&gt; u.State).HasMaxLength(StateMaxLength);
          Property(u =&gt; u.Country).HasMaxLength(CountryMaxLength);
          Property(u =&gt; u.Phone).HasMaxLength(PhoneMaxLength);
          Property(u =&gt; u.SecondaryPhone).HasMaxLength(PhoneMaxLength);
          Property(u =&gt; u.SecondaryEmailAddress).HasMaxLength(EmailAddressMaxLength);
          Property(u =&gt; u.Notes).HasMaxLength(NotesMaxLength);
          Property(u =&gt; u.PasswordRetrievalToken).IsOptional(); // somehow the DB is generated with this field required if this is ommited
          HasMany(u =&gt; u.Roles).WithMany(r =&gt; r.Users).Map(m =&gt; { m.MapLeftKey("UserId"); m.MapRightKey("RoleId"); m.ToTable("UsersInRoles"); });
          HasMany(u =&gt; u.Jobs).WithMany(j =&gt; j.Composers).Map(m =&gt; { m.MapLeftKey("UserId"); m.MapRightKey("JobId"); m.ToTable("ComposersInJobs"); });
      }
  }
  </code></pre>
  
  <p>When I NOW run Update-Database, the migrations run fine, but the Seed method is NOT executed (no matter whether I use -Force).
  If I run the Update-Database command a second time, the seed method is executed, but returns immediately because there are no pending migrations.</p>
  
  <p>I digged into it step by step and found out, that if I comment out all the new fields (MiddleName, Company, State, SecondaryPhone, SecondaryEmailAddress) in the EntityTypeConfiguration, the seed method is executed the first time I run the Update command without any problems.</p>
  
  <p>Did anyone experience the same issue? What can be the reason?</p>