AspNetCore.Docs/aspnetcore/tutorials/first-mvc-app/adding-model.md

5.2 KiB

title author description keywords ms.author manager ms.date ms.topic ms.assetid ms.technology ms.prod uid
Adding a model | Microsoft Docs rick-anderson Add a model to a simple ASP.NET Core app. ASP.NET Core, riande wpickett 03/30/2017 article 8dc28498-00ee-4d66-b903-b593059e9f39 aspnet asp.net-core tutorials/first-mvc-app/adding-model

[!INCLUDEadding-model]

In Solution Explorer, right click the MvcMovie project > Add > New Folder. Name the folder Models.

In Solution Explorer, right click the Models folder > Add > Class. Name the class Movie and add the following properties:

[!code-csharpMain]

The ID field is required by the database for the primary key.

Build the project to verify you don't have any errors, and you've finally added a Model to your MVC app.

Scaffolding a controller

In Solution Explorer, right-click the Controllers folder > Add > Controller.

view of above step

In the Add MVC Dependencies dialog, select Minimal Dependencies, and select Add.

view of above step

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.

In Solution Explorer, right-click the Controllers folder > Add > Controller.

view of above step

In the Add Scaffold dialog, tap MVC Controller with views, using Entity Framework > Add.

Add Scaffold dialog

Complete the Add Controller dialog:

  • Model class: Movie (MvcMovie.Models)
  • Data context class: Select the + icon and add the default MvcMovie.Models.MvcMovieContext

Add Data context

  • Views: Keep the default of each option checked
  • Controller name: Keep the default MoviesController
  • Tap Add

Add Controller dialog

  • 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 (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.

If you run the app and click on the Mvc Movie link, you'll get an error similar to the following:

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

You need to create the database, and you'll use the EF Core 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

  • In Solution Explorer, right click the MvcMovie project > Edit MvcMovie.csproj.

    SE meu showing Edit MvcMovie.csproj

  • Add the "Microsoft.EntityFrameworkCore.Tools.DotNet" NuGet package:

[!code-xmlMain]

Note: The version numbers shown above were correct at the time of writing.

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:

    dotnet restore
    dotnet ef migrations add Initial
    dotnet ef database update
    

dotnet ef commands

  • dotnet (.NET Core) is a cross-platform implementation of .NET. You can read about it here.
  • dotnet restore: Downloads the NuGet packages specified in the .csproj file.
  • 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 tutorial.

[!INCLUDEadding-model]

Intellisense contextual menu on a Model item listing the available properties for ID, Price, Release Date, and Title

Additional resources

[!div class="step-by-step"] Previous Adding a View Next Working with SQL