2016-12-17 14:54:23 +08:00
---
2016-11-17 08:24:57 +08:00
title: Adding a model | Microsoft Docs
2016-10-29 01:35:15 +08:00
author: rick-anderson
2017-03-04 05:35:47 +08:00
description: Add a model to a simple ASP.NET Core app.
2016-11-18 04:13:02 +08:00
keywords: ASP.NET Core,
2016-10-29 01:35:15 +08:00
ms.author: riande
manager: wpickett
2017-03-31 10:41:44 +08:00
ms.date: 03/30/2017
2016-10-29 01:35:15 +08:00
ms.topic: article
ms.assetid: 8dc28498-00ee-4d66-b903-b593059e9f39
2016-11-17 08:24:57 +08:00
ms.technology: aspnet
2017-03-03 08:50:36 +08:00
ms.prod: asp.net-core
2016-11-03 01:17:24 +08:00
uid: tutorials/first-mvc-app/adding-model
2016-10-29 01:35:15 +08:00
---
2017-03-31 10:41:44 +08:00
[!INCLUDE[adding-model ](../../includes/mvc-intro/adding-model1.md )]
2016-10-29 01:35:15 +08:00
2017-03-04 05:35:47 +08:00
In Solution Explorer, right click the **MvcMovie** project > **Add** > **New Folder** . Name the folder *Models* .
2016-10-29 01:35:15 +08:00
2017-03-04 05:35:47 +08:00
In Solution Explorer, right click the *Models* folder > **Add** > **Class** . Name the class **Movie** and add the following properties:
2016-10-29 01:35:15 +08:00
2017-04-16 04:10:18 +08:00
[!code-csharp[Main ](../../tutorials/first-mvc-app/start-mvc/sample/MvcMovie/Models/MovieNoEF.cs?name=snippet_1 )]
2017-03-31 10:41:44 +08:00
2017-04-16 04:10:18 +08:00
The `ID` field is required by the database for the primary key.
2016-10-29 01:35:15 +08:00
2017-04-16 04:10:18 +08:00
Build the project to verify you don't have any errors, and you've finally added a **M**odel to your **M**VC app.
2016-10-29 01:35:15 +08:00
2017-03-04 05:35:47 +08:00
## Scaffolding a controller
2016-10-29 01:35:15 +08:00
2017-03-04 05:35:47 +08:00
In **Solution Explorer** , right-click the *Controllers* folder ** > Add > Controller**.
2016-10-29 01:35:15 +08:00
2017-03-04 05:35:47 +08:00
![view of above step ](adding-model/_static/add_controller.png )
2016-10-29 01:35:15 +08:00
2017-03-04 05:35:47 +08:00
In the **Add MVC Dependencies** dialog, select **Minimal Dependencies** , and select **Add** .
2016-10-29 01:35:15 +08:00
2017-03-04 05:35:47 +08:00
![view of above step ](adding-model/_static/add_depend.png )
2016-10-29 01:35:15 +08:00
2017-04-02 02:13:57 +08:00
Visual Studio adds the dependencies needed to scaffold a controller, but the controller itself is not created. The next invoke of ** > Add > Controller** creates the controller.
2016-10-29 01:35:15 +08:00
In **Solution Explorer** , right-click the *Controllers* folder ** > Add > Controller**.
2017-03-04 05:35:47 +08:00
![view of above step ](adding-model/_static/add_controller.png )
2016-10-29 01:35:15 +08:00
In the **Add Scaffold** dialog, tap **MVC Controller with views, using Entity Framework > Add** .
2016-12-23 02:03:29 +08:00
![Add Scaffold dialog ](adding-model/_static/add_scaffold2.png )
2016-10-29 01:35:15 +08:00
2016-12-23 01:13:21 +08:00
Complete the **Add Controller** dialog:
2016-10-29 01:35:15 +08:00
2016-12-17 11:05:01 +08:00
* **Model class:** *Movie (MvcMovie.Models)*
2017-03-04 05:35:47 +08:00
* **Data context class:** Select the ** +** icon and add the default **MvcMovie.Models.MvcMovieContext**
![Add Data context ](adding-model/_static/dc.png )
2016-12-23 01:13:21 +08:00
* **Views:** Keep the default of each option checked
2016-10-29 01:35:15 +08:00
* **Controller name:** Keep the default *MoviesController*
* Tap **Add**
2016-12-23 02:03:29 +08:00
![Add Controller dialog ](adding-model/_static/add_controller2.png )
2016-10-29 01:35:15 +08:00
* A movies controller (*Controllers/MoviesController.cs*)
2017-04-16 04:10:18 +08:00
* Razor view files for Create, Delete, Details, Edit and Index pages (*Views/Movies/\*.cshtml*)
2016-10-29 01:35:15 +08:00
2017-04-16 04:10:18 +08:00
The automatic creation of [CRUD ](https://en.wikipedia.org/wiki/Create,_read,_update_and_delete ) (create, read, update, and delete) action methods and views is known as *scaffolding* . You'll soon have a fully functional web application that lets you manage a movie database.
2017-03-04 05:35:47 +08:00
If you run the app and click on the **Mvc Movie** link, you'll get an error similar to the following:
2017-04-16 04:10:18 +08:00
```
2017-03-04 05:35:47 +08:00
An unhandled exception occurred while processing the request.
SqlException: Cannot open database "MvcMovieContext-< GUID removed > "
requested by the login. The login failed.
Login failed for user Rick
```
2016-10-29 01:35:15 +08:00
2017-04-16 04:10:18 +08:00
You need to create the database, and you'll use the EF Core [Migrations ](xref:data/ef-mvc/migrations ) feature to do that. Migrations lets you create a database that matches your data model and update the database schema when your data model changes.
## Add EF tooling for Migrations
2016-10-29 01:35:15 +08:00
2017-03-04 05:35:47 +08:00
- In Solution Explorer, right click the **MvcMovie** project > **Edit MvcMovie.csproj** .
2016-10-29 01:35:15 +08:00
2017-03-04 05:35:47 +08:00
![SE meu showing Edit MvcMovie.csproj ](adding-model/_static/edit_csproj.png )
2016-10-29 01:35:15 +08:00
2017-03-04 05:35:47 +08:00
- Add the `"Microsoft.EntityFrameworkCore.Tools.DotNet"` NuGet package:
2016-10-29 01:35:15 +08:00
2017-03-04 05:35:47 +08:00
[!code-xml[Main ](start-mvc/sample/MvcMovie/MvcMovie.csproj?range=22-25&highlight=3 )]
2016-10-29 01:35:15 +08:00
2017-03-04 05:35:47 +08:00
Note: The version numbers shown above were correct at the time of writing.
2017-04-12 13:02:07 +08:00
## Add initial migration and update the database
* Open a command prompt and navigate to the project directory. (The directory containing the *Startup.cs* file).
* Run the following commands in the command prompt:
```console
dotnet restore
dotnet ef migrations add Initial
dotnet ef database update
```
2017-04-16 04:10:18 +08:00
### dotnet ef commands
2017-04-12 13:02:07 +08:00
* `dotnet` (.NET Core) is a cross-platform implementation of .NET. You can read about it [here ](http://go.microsoft.com/fwlink/?LinkID=517853 ).
* `dotnet restore` : Downloads the NuGet packages specified in the *.csproj* file.
2017-04-16 04:10:18 +08:00
* `dotnet ef migrations add Initial` Runs the Entity Framework .NET Core CLI migrations command and creates the initial migration. The parameter after "add" is a name that you assign to the migration. Here you're naming the migration "Initial" because it's the initial database migration. This operation creates the *Data/Migrations/\<date-time>_Initial.cs* file containing the migration commands to add the *Movie* table to the database.
* `dotnet ef database update` Updates the database with the migration we just created.
You'll learn more about data model changes in the [Add a field ](xref:tutorials/first-mvc-app/new-field ) tutorial.
2017-04-12 13:02:07 +08:00
2017-03-31 10:41:44 +08:00
[!INCLUDE[adding-model ](../../includes/mvc-intro/adding-model3.md )]
2016-10-29 01:35:15 +08:00
2016-12-23 02:03:29 +08:00
![Intellisense contextual menu on a Model item listing the available properties for ID, Price, Release Date, and Title ](adding-model/_static/ints.png )
2016-10-29 01:35:15 +08:00
2017-04-16 04:10:18 +08:00
## Additional resources
2016-10-29 01:35:15 +08:00
2017-03-31 10:41:44 +08:00
* [Tag Helpers ](xref:mvc/views/tag-helpers/intro )
* [Globalization and localization ](xref:fundamentals/localization )
2016-10-29 01:35:15 +08:00
2016-11-10 13:17:21 +08:00
>[!div class="step-by-step"]
2017-03-31 10:41:44 +08:00
[Previous Adding a View ](adding-view.md )
[Next Working with SQL ](working-with-sql.md )