Create Model
In this section we'll create model class. The model class are use with EF Core (entity framework core) EF work with database EF core is ORM (object-relational mapping) the framework simplifies the data access code you we’ll write The model class created are known as POCO classes (Plain Old CLR Object) POCO class don’t have any dependency on EF Core, they only define the properties of the data to be stored in database
Add a data model class
Add file to the model folder name it Movie.cs Update movie file with code plonge
using System; using System.ComponentModel.DataAnnotations; namespace mvcProjectName.Models { public class Movie { public int id { get; set; } public string Titel { get; set; } [DataType(DataType.Date)] public DateTime ReleaseDate { get; set; } public string Genre { get; set; } public decimal Price { get; set; } } }
- The movie class contains an
id
field, which is required by the database for the primary key.- The datatype attribute on
ReleaseDate
specifies the type of data (Date) with this attribute The user isn’t required to enter time information in the date field Only the date is display not time information
Add NuGet packages
Add following .net CLI commands
dotnet tool install --global dotnet-ef
this command add the command-line interface (CLI) tools for EF Core
dotnet tool install --global dotnet-aspnet-codegenerator
To add The
aspnet-codegenerator
scaffolding tool.
dotnet add package Microsoft.EntityFrameworkCore.Design
Design time tools for EF Core
dotnet add package Microsoft.EntityFrameworkCore.SQLite
The EF Core SQLite provider, which installs the EF Core package as a dependency.
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design dotnet add package Microsoft.EntityFrameworkCore.SqlServer
This packages needed for scaffolding
Scaffold movie pages
Open a command window in project directory run the following command
dotnet-aspnet-codegenerator controller -name MoviesController -m Movie -dc MvcMovieContext --relativeFolderPath Controllers --useDefaultLayout --referenceScriptLibraries
The following table details the ASP.NET Core code generator parameters: Use the
h
switch to get help on theaspnet-codegenerator
controller command:
dotnet aspnet-codegenerator controller -h