title: Entity Framework Code First in class library
body: <p>I just started trying my hands on EF4 code first this morning and I created my POCO, data context and Initializer classes in a separate class library, I believe it's the regular boiler plate type code. I reference the class in an MVC3 application and set the initializer in the Global.asax. On running the app, I notice the following problems<br>
  1. No database is created anywhere (Then I add an entry in the web.config for a connection string named after the Context class, still no result)<br>
  2. When I try to access the initalized values, I get a null error, obviously because there is no data.</p>
  
  <p>Can anyone please help me with pointers on how to get thi thing to work (would be a shame if I spent my entire christmas day learning this and I still can't get it to work :( )
  Thanks<br>
  p.s. I tried inserting break points and I hit the app initialization method, but it never hits the Seed method in the initializer even though I add a break point there as well!!<br>
  Thanks.<br>
  Initializer class</p>
  
  <pre><code>    using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Data.Entity;
      using F2AController.Models;
  
      namespace F2AController.DataObjects
      {
          public class F2AInitializer : DropCreateDatabaseAlways&lt;F2AContext&gt;
          {
              protected override void Seed(F2AContext context)
              {
                  var countries = new List&lt;Country&gt; 
                  {
                      new Country(){ CountryName="Germany", Active = true},
                      new Country(){ CountryName="Britain", Active = true}
                  };
                  countries.ForEach(s =&gt; context.Countries.Add(s));
                  context.SaveChanges();
                  var providers = new List&lt;Providers&gt;() 
                  {
                      new Providers(){ ProviderName="InfoBip", ContactDetails="Rturo Manovic", Active=true, MessageRates= new List&lt;ProviderRates&gt;(){new ProviderRates(){ CountryId=1, DateCreated=DateTime.Now, DateModified=DateTime.Now, Rate=0.05M, Active=true}}}
                  };
                  providers.ForEach(p =&gt; context.Providers.Add(p));
                  context.SaveChanges();
                  var usermobiles = new List&lt;MobileTerminal&gt;()
                  {
                      new MobileTerminal(){ Active= true, Credits=200, DateCreated=DateTime.Now, MSISDN="4477565444865"}
                  };
                  usermobiles.ForEach(u =&gt; context.MobileTerminals.Add(u));
  
                  context.SaveChanges();
              }
  
  
          }
      }
  </code></pre>
  
  <p>Context Class</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Data.Entity;
  
  namespace F2AController.Models
  {
      public class F2AContext : DbContext
      {
          public DbSet&lt;Country&gt; Countries;
          public DbSet&lt;MobileTerminal&gt; MobileTerminals;
          public DbSet&lt;Providers&gt; Providers;
          public DbSet&lt;ProviderRates&gt; ProviderRates;
          public DbSet&lt;Property&gt; Properties;
          public DbSet&lt;ShortMessage&gt; ShortMessages;
          public DbSet&lt;UserProperties&gt; UserProperties;
  
          protected override void OnModelCreating(DbModelBuilder modelBuilder)
          {
              base.OnModelCreating(modelBuilder);
          }
      }
  }
  </code></pre>
  
  <p>Global.asax App initialization method</p>
  
  <pre><code>    protected void Application_Start()
      {
          AreaRegistration.RegisterAllAreas();
          Database.DefaultConnectionFactory = new SqlConnectionFactory(ConfigurationManager.ConnectionStrings["F2AContext"].ConnectionString);
          Database.SetInitializer&lt;F2AContext&gt;(new F2AInitializer());
  
          RegisterGlobalFilters(GlobalFilters.Filters);
          RegisterRoutes(RouteTable.Routes);
      }
  </code></pre>