title: Why must I have a parameterless constructor for Code First / Entity Framework
body: <p>This is more of a question of "Why we do things" as my actual problem was solved but I don't know why.</p>
  
  <p>I was dealing with the following code inside my CountyRepository:</p>
  
  <pre><code>public IEnumerable&lt;County&gt; GetCounties(string stateAbbr)
      {
          using (var db = new AppDbContext())
          {
              State state = (from s in db.States
                           where s.Abbr == stateAbbr
                           select s).First();
  
              return context.Counties.Where(c =&gt; c.StateID == state.StateID).ToList();
          }
      }
  </code></pre>
  
  <p>The AppDbContext I created above would go to a custom Initializer: </p>
  
  <pre><code>  public class AppDbContextInitializer : DropCreateDatabaseIfModelChanges&lt;AppDbContext&gt; 
  {
      protected override void Seed(AppDbContext context)
      {
          StatesList states = new StatesList();
          context.States.AddRange(states);
          context.Counties.AddRange(new CountiesList(states));
  
          context.SaveChanges();
      }
  }
  </code></pre>
  
  <p>The problem was, when I executed the code the AppDbContext would load the State and County information correctly in the Initializer, but when it came back into the County Repository, the AppDbContext was empty and would error due to "State has no parameterless constructor".    I didn't want my State object to have a parameterless constructor so I looked all day for a solution to why the AppDbContext woulding load in the County Repository.  I finally found the following solution:</p>
  
  <p><a href="https://stackoverflow.com/questions/22666607/exception-when-loading-related-objects-entity-framework">Exception when loading related objects. Entity Framework</a></p>
  
  <p>It was a simple solution.  Add the parameterless constructor and mark it Obsolete.  I did this and it worked perfectly.  </p>
  
  <p>My question is, WHY must I do this?  I went through multiple examples of CodeFirst using custom Initializer and none of them mentioned requiring an empty constructor or marking it Obsolete.  </p>
  
  <p>Is there a better solution or at least an explanation so I can go forward with knowledge instead of confusion?</p>