title: Create table in Database using Entity Framework
body: <p>I'm trying to link my MVC project with a database using Entity Framework, but I can't figure out how to store new records to a database. So far I have the following code:</p>
  
  <pre><code>//model
  
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Data.Entity;
  using System.ComponentModel;
  using System.ComponentModel.DataAnnotations;
  using MvcApplication1.Controllers;
  
  namespace MvcApplication1.Models
  {
  public class CarUser
  {
      public int ID { get; set; }
      [DisplayName ("First Name")]
      public string Fname { get; set; }
      [DisplayName ("Surname")]
      public string Sname { get; set; }
  }
  }
  
  //controller
  
  using System;
  using System.Collections.Generic;
  using System.Data;
  using System.Data.Entity;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
  using MvcApplication1.Models;
  
  namespace MvcApplication1.Controllers
  {
  public class CarUserController : Controller
  {
      private FormDataContext db = new FormDataContext();
  
      //
      // GET: /CarUser/
  
      public ActionResult Index()
      {
          return View(db.CarUsers.ToList());
      }
  
      //
      // GET: /CarUser/Details/5
  
      public ActionResult Details(int id = 0)
      {
          CarUser caruser = db.CarUsers.Find(id);
          if (caruser == null)
          {
              return HttpNotFound();
          }
          return View(caruser);
      }
  
      //
      // GET: /CarUser/Create
  
      public ActionResult Create()
      {
          return View();
      }
  
      //
      // POST: /CarUser/Create
  
      [HttpPost]
      public ActionResult Create(CarUser caruser)
      {
          if (ModelState.IsValid)
          {
              db.CarUsers.Add(caruser);
              db.SaveChanges();
              return RedirectToAction("Index");
          }
  
          return View(caruser);
      }
  
      //
      // GET: /CarUser/Edit/5
  
      public ActionResult Edit(int id = 0)
      {
          CarUser caruser = db.CarUsers.Find(id);
          if (caruser == null)
          {
              return HttpNotFound();
          }
          return View(caruser);
      }
  
      //
      // POST: /CarUser/Edit/5
  
      [HttpPost]
      public ActionResult Edit(CarUser caruser)
      {
          if (ModelState.IsValid)
          {
              db.Entry(caruser).State = EntityState.Modified;
              db.SaveChanges();
              return RedirectToAction("Index");
          }
          return View(caruser);
      }
  
      //
      // GET: /CarUser/Delete/5
  
      public ActionResult Delete(int id = 0)
      {
          CarUser caruser = db.CarUsers.Find(id);
          if (caruser == null)
          {
              return HttpNotFound();
          }
          return View(caruser);
      }
  
      //
      // POST: /CarUser/Delete/5
  
      [HttpPost, ActionName("Delete")]
      public ActionResult DeleteConfirmed(int id)
      {
          CarUser caruser = db.CarUsers.Find(id);
          db.CarUsers.Remove(caruser);
          db.SaveChanges();
          return RedirectToAction("Index");
      }
  
      protected override void Dispose(bool disposing)
      {
          db.Dispose();
          base.Dispose(disposing);
      }
  }
  }
  
  //formdatacontext
  
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Data.Entity;
  
  namespace MvcApplication1.Models
  {
  public class FormDataContext : DbContext
  {
      public DbSet&lt;CarUser&gt; CarUsers
      { get; set; }
  }
  }
  
  //formdatainitialiser
  
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Data.Entity;
  using System.IO;
  
  namespace MvcApplication1.Models
  {
  public class FormDataInitializer : DropCreateDatabaseAlways&lt;FormDataContext&gt;
  {
      protected override void Seed(FormDataContext context)
      {
          base.Seed(context);
  
          var CarUsers = new List&lt;CarUser&gt;
          {
              new CarUser {
                  ID = 1,
                  Fname = "Craig",
                  Sname = "Higginson",
              }
          };
  
          CarUsers.ForEach(s =&gt; context.CarUsers.Add(s));
          context.SaveChanges();
  
          }
      }
  
  }
  </code></pre>
  
  <p>I've also got the following in my web.config file</p>
  
  <pre><code>&lt;connectionStrings&gt;
  &lt;add name="FormDataContext"
       connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=FormData.mdf;Integrated Security=True" providerName="System.Data.SqlClient" /&gt;
  </code></pre>
  
  <p></p>
  
  <p>This all builds fine, and when I navigate to /caruser i am able to create new records. These records aren't being stored in my database.</p>
  
  <p>Do I need to create the table in the database first or should Entity Framework create this for me, if i have referenced the database correctly?</p>