2017-03-31 10:41:44 +08:00
---
2017-07-01 07:47:15 +08:00
title: Adding a model
2017-03-31 10:41:44 +08:00
author: rick-anderson
description: Add a model to a simple ASP.NET Core app.
keywords: ASP.NET Core,
ms.author: riande
manager: wpickett
ms.date: 03/30/2017
2017-06-01 05:55:29 +08:00
ms.topic: get-started-article
2017-03-31 10:41:44 +08:00
ms.assetid: 8dc28498-eeee-4666-b903-b593059e9f39
ms.technology: aspnet
ms.prod: asp.net-core
uid: tutorials/first-mvc-app-xplat/adding-model
---
2017-04-08 11:45:50 +08:00
2017-04-16 04:10:18 +08:00
[!INCLUDE[adding-model1 ](../../includes/mvc-intro/adding-model1.md )]
2017-03-31 10:41:44 +08:00
* Add a class to the *Models* folder named *Movie.cs* .
* Add the following code to the *Models/Movie.cs* file:
2017-06-21 06:37:36 +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.
2017-03-31 10:41:44 +08:00
2017-04-16 04:10:18 +08:00
Build the app to verify you don't have any errors, and you've finally added a **M**odel to your **M**VC app.
2017-03-31 10:41:44 +08:00
## Prepare the project for scaffolding
- Add the following highlighted NuGet packages to the *MvcMovie.csproj* file:
2017-08-29 04:30:32 +08:00
[!code-csharp[Main ](start-mvc/sample/MvcMovie/MvcMovie.csproj?highlight=7,10 )]
2017-03-31 10:41:44 +08:00
2017-04-08 11:45:50 +08:00
- Save the file and select **Restore** to the **Info** message "There are unresolved dependencies".
2017-03-31 11:41:07 +08:00
- Create a *Models/MvcMovieContext.cs* file and add the following `MvcMovieContext` class:
[!code-csharp[Main ](start-mvc/sample/MvcMovie/Models/MvcMovieContext.cs )]
2017-04-16 04:10:18 +08:00
- Open the *Startup.cs* file and add two usings:
2017-03-31 10:41:44 +08:00
[!code-csharp[Main ](start-mvc/sample/MvcMovie/Startup.cs?name=snippet1&highlight=1,2 )]
- Add the database context to the *Startup.cs* file:
2017-04-08 11:45:50 +08:00
[!code-csharp[Main ](start-mvc/sample/MvcMovie/Startup.cs?name=snippet2&highlight=6-7 )]
2017-03-31 10:41:44 +08:00
2017-04-16 04:10:18 +08:00
This tells Entity Framework which model classes are included in the data model. You're defining one *entity set* of Movie objects, which will be represented in the database as a Movie table.
- Build the project to verify there are no errors.
2017-03-31 10:41:44 +08:00
2017-04-08 11:45:50 +08:00
## Scaffold the MovieController
2017-03-31 10:41:44 +08:00
Open a terminal window in the project folder and run the following commands:
2017-04-16 04:10:18 +08:00
```
2017-03-31 10:41:44 +08:00
dotnet restore
2017-04-13 05:47:18 +08:00
dotnet aspnet-codegenerator controller -name MoviesController -m Movie -dc MvcMovieContext --relativeFolderPath Controllers --useDefaultLayout --referenceScriptLibraries
2017-03-31 10:41:44 +08:00
```
2017-04-18 11:37:52 +08:00
> [!NOTE]
> If you get an error when the scaffolding command runs, see [issue 444 in the scaffolding repository](https://github.com/aspnet/scaffolding/issues/444) for a workaround.
2017-03-31 10:41:44 +08:00
The scaffolding engine creates the following:
2017-04-16 04:10:18 +08:00
* A movies controller (*Controllers/MoviesController.cs*)
* Razor view files for Create, Delete, Details, Edit and Index pages (*Views/Movies/\*.cshtml*)
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-08-29 04:30:32 +08:00
[!INCLUDE[adding-model 2x ](../../includes/mvc-intro/adding-model2xp.md )]
2017-03-31 10:41:44 +08:00
[!INCLUDE[adding-model ](../../includes/mvc-intro/adding-model3.md )]
You now have a database and pages to display, edit, update and delete data. In the next tutorial, we'll work with the database.
### Additional resources
* [Tag Helpers ](xref:mvc/views/tag-helpers/intro )
* [Globalization and localization ](xref:fundamentals/localization )
>[!div class="step-by-step"]
2017-04-15 01:46:51 +08:00
[Previous - Add a view ](adding-view.md )
[Next - Working with SQLite ](working-with-sql.md )