Update the Models/Movie.cs
file with the following code:
using System; using System.ComponentModel.DataAnnotations; namespace MvcMovie.Models { public class Movie { public int Id { get; set; } public string Title { get; set; } [DataType(DataType.Date)] public DateTime ReleaseDate { get; set; } public string Genre { get; set; } public decimal Price { get; set; } } }
Id
field, which is required by the database for the primary key.DataAnnotations are covered in a later tutorial.
Reference Resource [1] in order to see installation examples for Visual Studio for Mac.
Note: I haven't confirmed, but you may be able to get away with installing through dotnet CLI. From what I see later in the tutorial, NuGet may be required.
Packages required in this tutorial:
A database context class is needed to coordinate EF Core functionality (Create, Read, Update, Delete) for the Movie model. The database context is derived from Microsoft.EntityFrameworkCore.DbContext
and specifies the entities to include in the data model.
Create a Data folder.
Add a Data/MvcMovieContext.cs file with the following code:
using Microsoft.EntityFrameworkCore; using MvcMovie.Models; namespace MvcMovie.Data { public class MvcMovieContext : DbContext { public MvcMovieContext (DbContextOptions<MvcMovieContext> options) : base(options) { } public DbSet<Movie> Movie { get; set; } } }
The preceding code creates a DbSet<Movie>
property for the entity set. In Entity Framework terminology, an entity set typically corresponds to a database table. An entity corresponds to a row in the table.
ASP.NET Core is built with dependency injection (DI). Services (such as the EF Core DB context) must be registered with DI during application startup. Components that require these services (such as Razor Pages) are provided these services via constructor parameters. The constructor code that gets a DB context instance is shown later in the tutorial. In this section, you register the database context with the DI container.
Add to the top of Startup.cs:
using MvcMovie.Data; using Microsoft.EntityFrameworkCore;
Add the following for Startup.ConfigureServices
:
public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); services.AddDbContext<MvcMovieContext>(options => options.UseSqlite(Configuration.GetConnectionString("MvcMovieContext"))); }
Add ConnectionStrings
config to appsettings.json
:
{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "AllowedHosts": "*", "ConnectionStrings": { "MvcMovieContext": "Data Source=MvcMovie.db" } }
Use the scaffolding tool to produce Create, Read, Update, and Delete (CRUD) pages for the movie model.
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:
Parameter | Description |
---|---|
-m | The name of the model. |
-dc | The data context. |
-udl | Use the default layout. |
--relativeFolderPath | The relative output folder path to create the files. |
--useDefaultLayout | The default layout should be used for the views. |
--referenceScriptLibraries | Adds _ValidationScriptsPartial to Edit and Create pages |
Note: for more help run dotnet aspnet-codegenerator controller -h
Use the EF Core Migrations feature to create the database. Migrations is a set of tools that let you create and update a database to match your data model.
dotnet ef migrations add InitialCreate dotnet ef database update
Examine the Migrations/{timestamp}_InitialCreate.cs
migration file:
public partial class Initial : Migration { protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.CreateTable( name: "Movie", columns: table => new { Id = table.Column<int>(nullable: false) .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn), Title = table.Column<string>(nullable: true), ReleaseDate = table.Column<DateTime>(nullable: false), Genre = table.Column<string>(nullable: true), Price = table.Column<decimal>(nullable: false) }, constraints: table => { table.PrimaryKey("PK_Movie", x => x.Id); }); } protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropTable( name: "Movie"); } }
The Up
method creates the Movie
table and configures Id
as the primary key. The Down
method reverts the schema changes made by the Up
migration.
Finish off the tutorial notes from here
Note: Stopping since the context is enough for other work to be completed.