--- uid: mvc/overview/older-versions/getting-started-with-aspnet-mvc4/adding-a-new-field-to-the-movie-model-and-table title: "Adding a New Field to the Movie Model and Table | Microsoft Docs" author: Rick-Anderson description: "Note: An updated version of this tutorial is available here that uses ASP.NET MVC 5 and Visual Studio 2013. It's more secure, much simpler to follow and demo..." ms.author: aspnetcontent manager: wpickett ms.date: 08/28/2012 ms.topic: article ms.assetid: 9ef2c4f1-a305-4e0a-9fb8-bfbd9ef331d9 ms.technology: dotnet-mvc ms.prod: .net-framework msc.legacyurl: /mvc/overview/older-versions/getting-started-with-aspnet-mvc4/adding-a-new-field-to-the-movie-model-and-table msc.type: authoredcontent --- Adding a New Field to the Movie Model and Table ==================== by [Rick Anderson](https://github.com/Rick-Anderson) > > [!NOTE] > > An updated version of this tutorial is available [here](../../getting-started/introduction/getting-started.md) that uses ASP.NET MVC 5 and Visual Studio 2013. It's more secure, much simpler to follow and demonstrates more features. In this section you'll use Entity Framework Code First Migrations to migrate some changes to the model classes so the change is applied to the database. By default, when you use Entity Framework Code First to automatically create a database, as you did earlier in this tutorial, Code First adds a table to the database to help track whether the schema of the database is in sync with the model classes it was generated from. If they aren't in sync, the Entity Framework throws an error. This makes it easier to track down issues at development time that you might otherwise only find (by obscure errors) at run time. ## Setting up Code First Migrations for Model Changes If you are using Visual Studio 2012, double click the *Movies.mdf* file from Solution Explorer to open the database tool. Visual Studio Express for Web will show Database Explorer, Visual Studio 2012 will show Server Explorer. If you are using Visual Studio 2010, use SQL Server Object Explorer. In the database tool (Database Explorer, Server Explorer or SQL Server Object Explorer), right click on `MovieDBContext` and select **Delete** to drop the movies database. ![](adding-a-new-field-to-the-movie-model-and-table/_static/image1.png) Navigate back to Solution Explorer. Right click on the *Movies.mdf* file and select **Delete** to remove the movies database. ![](adding-a-new-field-to-the-movie-model-and-table/_static/image2.png) Build the application to make sure there are no errors. From the **Tools** menu, click **Library Package Manager** and then **Package Manager Console**. ![Add Pack Man](adding-a-new-field-to-the-movie-model-and-table/_static/image3.png) In the **Package Manager Console** window at the `PM>` prompt enter "Enable-Migrations -ContextTypeName MvcMovie.Models.MovieDBContext". ![](adding-a-new-field-to-the-movie-model-and-table/_static/image4.png) The **Enable-Migrations** command (shown above) creates a *Configuration.cs* file in a new *Migrations* folder. ![](adding-a-new-field-to-the-movie-model-and-table/_static/image5.png) Visual Studio opens the *Configuration.cs* file. Replace the `Seed` method in the *Configuration.cs* file with the following code: [!code-csharp[Main](adding-a-new-field-to-the-movie-model-and-table/samples/sample1.cs)] Right click on the red squiggly line under `Movie` and select **Resolve** then **using** **MvcMovie.Models;** ![](adding-a-new-field-to-the-movie-model-and-table/_static/image6.png) Doing so adds the following using statement: [!code-csharp[Main](adding-a-new-field-to-the-movie-model-and-table/samples/sample2.cs)] > [!NOTE] > > Code First Migrations calls the `Seed` method after every migration (that is, calling **update-database** in the Package Manager Console), and this method updates rows that have already been inserted, or inserts them if they don't exist yet. **Press CTRL-SHIFT-B to build the project.**(The following steps will fail if your don't build at this point.) The next step is to create a `DbMigration` class for the initial migration. This migration to creates a new database, that's why you deleted the *movie.mdf* file in a previous step. In the **Package Manager Console** window, enter the command "add-migration Initial" to create the initial migration. The name "Initial" is arbitrary and is used to name the migration file created. ![](adding-a-new-field-to-the-movie-model-and-table/_static/image7.png) Code First Migrations creates another class file in the *Migrations* folder (with the name *{DateStamp}\_Initial.cs* ), and this class contains code that creates the database schema. The migration filename is pre-fixed with a timestamp to help with ordering. Examine the *{DateStamp}\_Initial.cs* file, it contains the instructions to create the Movies table for the Movie DB. When you update the database in the instructions below, this *{DateStamp}\_Initial.cs* file will run and create the the DB schema. Then the **Seed** method will run to populate the DB with test data. In the **Package Manager Console**, enter the command "update-database" to create the database and run the **Seed** method. ![](adding-a-new-field-to-the-movie-model-and-table/_static/image8.png) If you get an error that indicates a table already exists and can't be created, it is probably because you ran the application after you deleted the database and before you executed `update-database`. In that case, delete the *Movies.mdf* file again and retry the `update-database` command. If you still get an error, delete the migrations folder and contents then start with the instructions at the top of this page (that is delete the *Movies.mdf* file then proceed to Enable-Migrations). Run the application and navigate to the */Movies* URL. The seed data is displayed. ![](adding-a-new-field-to-the-movie-model-and-table/_static/image9.png) ## Adding a Rating Property to the Movie Model Start by adding a new `Rating` property to the existing `Movie` class. Open the *Models\Movie.cs* file and add the `Rating` property like this one: [!code-csharp[Main](adding-a-new-field-to-the-movie-model-and-table/samples/sample3.cs)] The complete `Movie` class now looks like the following code: [!code-csharp[Main](adding-a-new-field-to-the-movie-model-and-table/samples/sample4.cs?highlight=8)] Build the application using the **Build** >**Build Movie** menu command or by pressing CTRL-SHIFT-B. Now that you've updated the `Model` class, you also need to update the *\Views\Movies\Index.cshtml* and *\Views\Movies\Create.cshtml* view templates in order to display the new `Rating` property in the browser view. Open the*\Views\Movies\Index.cshtml* file and add a `