title: Insert data using entity framework code first
body: <p>I'm trying to save the data using EF code first and ASP.NET MVC 3</p>
  
  <p>My POCO class</p>
  
  <pre><code>public class UserAccount { 
      [Key]
      public int Id { get; set; }
      public string Name { get; set; }
     [Required(ErrorMessage="Username is required")]
      public string Username { get; set; }
      [Required(ErrorMessage = "Password is required")]
      public string Password { get; set; }
      public string Department { get; set; }
      public string Category_ID { get; set; }
  
  
  }
  </code></pre>
  
  <p>contex</p>
  
  <pre><code>public class OnlineTest : System.Data.Entity.DbContext {
      public DbSet&lt;UserAccount&gt; UserAccounts { get; set; }
      public OnlineTest()
          : base("OnlineTest")
      {
  
      }
  }
  
  
  public class OnlineTestInitializer : DropCreateDatabaseIfModelChanges&lt;OnlineTest&gt; {
      protected override void Seed(OnlineTest context)
      {
          context.UserAccounts.Add(new UserAccount { Name = "Admin", Username = "admin", Password = "adminpass", Department="All", Category_ID = "1" });
          base.Seed(context);
          context.SaveChanges();
      }
  }
  </code></pre>
  
  <p>My Applicaiton start</p>
  
  <pre><code>protected void Application_Start() {
      Database.SetInitializer(new OnlineTestMVC.Models.OnlineTestInitializer());
      var contex = new OnlineTest();
      contex.Database.Initialize(true);
      AreaRegistration.RegisterAllAreas();
  
      // Use LocalDB for Entity Framework by default
      Database.DefaultConnectionFactory = new SqlConnectionFactory(@"Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True");
  
      RegisterGlobalFilters(GlobalFilters.Filters);
      RegisterRoutes(RouteTable.Routes);
  }
  </code></pre>
  
  <p>I can initialize the database with default seeding data. My problem is, I can't insert data using below code.</p>
  
  <pre><code>var User = new UserAccount { Name = "abc", Username = "abc123", Password = "passwrd", Department = "CS", Category_ID = "2" };
  
  
  using (var db = new OnlineTest())
  {
      db.UserAccounts.Add(User);
      db.SaveChanges();
  }
  </code></pre>