ref: https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/razor-pages-start?view=aspnetcore-2.2&tabs=visual-studio-code
Create a Razor Pages web app
Type in the terminal
|
1 2 3 4 5 6 7 8 9 |
teddy@teddy:~/Documents/dotnet$ dotnet new webApp -o RazorPagesMovie The template "ASP.NET Core Web App" was created successfully. This template contains technologies from parties other than Microsoft, see https://aka.ms/aspnetcore-template-3pn-210 for details. Processing post-creation actions... Running 'dotnet restore' on RazorPagesMovie/RazorPagesMovie.csproj... Restore completed in 476.81 ms for /home/teddy/Documents/dotnet/RazorPagesMovie/RazorPagesMovie.csproj. Restore succeeded. |
Open it on VSCode
|
1 |
teddy@teddy:~/Documents/dotnet$ code -r RazorPagesMovie |
After the status bar’s OmniSharp flame icon turns green, a dialog asks Required assets to build and debug are missing from ‘RazorPagesMovie’. Add them? Select Yes.
A .vscode directory, containing launch.json and tasks.json files, is added to the project’s root directory.
Run the app
On the VSCode, open the terminal by pressing CTRL + ~
then type
|
1 2 |
teddy@teddy:~/Documents/dotnet/RazorPagesMovie$ dotnet dev-certs https A valid HTTPS certificate is already present. |
NOTE: I FOUND THIS COMMAND: ‘dotnet dev-certs https –trust’ IS NOT WORKING
Then Press Ctrl-F5 to run without the debugger. It’ll open the browser automatically with this url: https://localhost:5001/
Add a data model
Add a folder named Models.
Add a class to the Models folder named Movie.cs.
Add the following properties to the Movie class:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using System; using System.ComponentModel.DataAnnotations; namespace RazorPagesMovie.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;} } } |
Add a database context class
Add the following RazorPagesMovieContext class to the Data folder:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using Microsoft.EntityFrameworkCore; namespace RazorPagesMovie.Models { public class RazorPagesMovieContext : DbContext { public RazorPagesMovieContext(DbContextOptions<RazorPagesMovieContext> options) : base(options) { } public DbSet<RazorPagesMovie.Models.Movie> Movie {get; set;} } } |
Add a database connection string
Add a connection string to the appsettings.json file as shown in the following highlighted code:
|
1 2 3 4 5 6 7 8 9 10 11 |
{ "Logging": { "LogLevel": { "Default": "Warning" } }, "AllowedHosts": "*", "ConnectionStrings": { "MovieContext": "DataSource=MvcMovie.db" } } |
Add required NuGet packages
Run the following .NET Core CLI command to add SQLite and CodeGeneration.Design to the project:
|
1 2 3 |
dotnet add package Microsoft.EntityFrameworkCore.SQLite dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design dotnet add package Microsoft.EntityFrameworkCore.Design |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
teddy@teddy:~/Documents/dotnet/RazorPagesMovie$ dotnet add package Microsoft.EntityFrameworkCore.SQLite Writing /tmp/tmp6s15JK.tmp info : Adding PackageReference for package 'Microsoft.EntityFrameworkCore.SQLite' into project '/home/teddy/Documents/dotnet/RazorPagesMovie/RazorPagesMovie.csproj'. info : Restoring packages for /home/teddy/Documents/dotnet/RazorPagesMovie/RazorPagesMovie.csproj... info : GET https://api.nuget.org/v3-flatcontainer/microsoft.entityframeworkcore.sqlite/index.json info : OK https://api.nuget.org/v3-flatcontainer/microsoft.entityframeworkcore.sqlite/index.json 1010ms info : GET https://api.nuget.org/v3-flatcontainer/microsoft.entityframeworkcore.sqlite/2.2.4/microsoft.entityframeworkcore.sqlite.2.2.4.nupkg info : OK https://api.nuget.org/v3-flatcontainer/microsoft.entityframeworkcore.sqlite/2.2.4/microsoft.entityframeworkcore.sqlite.2.2.4.nupkg 34ms info : GET https://api.nuget.org/v3-flatcontainer/microsoft.entityframeworkcore.sqlite.core/index.json info : GET https://api.nuget.org/v3-flatcontainer/sqlitepclraw.bundle_green/index.json info : OK https://api.nuget.org/v3-flatcontainer/microsoft.entityframeworkcore.sqlite.core/index.json 998ms info : GET https://api.nuget.org/v3-flatcontainer/microsoft.entityframeworkcore.sqlite.core/2.2.4/microsoft.entityframeworkcore.sqlite.core.2.2.4.nupkg info : OK https://api.nuget.org/v3-flatcontainer/microsoft.entityframeworkcore.sqlite.core/2.2.4/microsoft.entityframeworkcore.sqlite.core.2.2.4.nupkg 32ms info : GET https://api.nuget.org/v3-flatcontainer/microsoft.entityframeworkcore.relational/index.json info : GET https://api.nuget.org/v3-flatcontainer/microsoft.data.sqlite.core/index.json info : OK https://api.nuget.org/v3-flatcontainer/sqlitepclraw.bundle_green/index.json 1136ms info : GET https://api.nuget.org/v3-flatcontainer/sqlitepclraw.bundle_green/1.1.12/sqlitepclraw.bundle_green.1.1.12.nupkg info : OK https://api.nuget.org/v3-flatcontainer/sqlitepclraw.bundle_green/1.1.12/sqlitepclraw.bundle_green.1.1.12.nupkg 33ms info : GET https://api.nuget.org/v3-flatcontainer/sqlitepclraw.core/index.json info : GET https://api.nuget.org/v3-flatcontainer/sqlitepclraw.provider.e_sqlite3.netstandard11/index.json info : GET https://api.nuget.org/v3-flatcontainer/sqlitepclraw.lib.e_sqlite3.v110_xp/index.json info : GET https://api.nuget.org/v3-flatcontainer/sqlitepclraw.lib.e_sqlite3.osx/index.json info : GET https://api.nuget.org/v3-flatcontainer/sqlitepclraw.lib.e_sqlite3.linux/index.json info : OK https://api.nuget.org/v3-flatcontainer/microsoft.entityframeworkcore.relational/index.json 1002ms info : GET https://api.nuget.org/v3-flatcontainer/microsoft.entityframeworkcore.relational/2.2.4/microsoft.entityframeworkcore.relational.2.2.4.nupkg info : OK https://api.nuget.org/v3-flatcontainer/microsoft.entityframeworkcore.relational/2.2.4/microsoft.entityframeworkcore.relational.2.2.4.nupkg 31ms info : OK https://api.nuget.org/v3-flatcontainer/microsoft.data.sqlite.core/index.json 1100ms info : GET https://api.nuget.org/v3-flatcontainer/microsoft.data.sqlite.core/2.2.4/microsoft.data.sqlite.core.2.2.4.nupkg info : GET https://api.nuget.org/v3-flatcontainer/microsoft.entityframeworkcore/index.json info : OK https://api.nuget.org/v3-flatcontainer/microsoft.data.sqlite.core/2.2.4/microsoft.data.sqlite.core.2.2.4.nupkg 33ms info : OK https://api.nuget.org/v3-flatcontainer/sqlitepclraw.lib.e_sqlite3.v110_xp/index.json 1120ms info : GET https://api.nuget.org/v3-flatcontainer/sqlitepclraw.lib.e_sqlite3.v110_xp/1.1.12/sqlitepclraw.lib.e_sqlite3.v110_xp.1.1.12.nupkg info : OK https://api.nuget.org/v3-flatcontainer/sqlitepclraw.lib.e_sqlite3.linux/index.json 1122ms info : GET https://api.nuget.org/v3-flatcontainer/sqlitepclraw.lib.e_sqlite3.linux/1.1.12/sqlitepclraw.lib.e_sqlite3.linux.1.1.12.nupkg info : OK https://api.nuget.org/v3-flatcontainer/sqlitepclraw.lib.e_sqlite3.osx/index.json 1123ms info : GET https://api.nuget.org/v3-flatcontainer/sqlitepclraw.lib.e_sqlite3.osx/1.1.12/sqlitepclraw.lib.e_sqlite3.osx.1.1.12.nupkg info : OK https://api.nuget.org/v3-flatcontainer/sqlitepclraw.provider.e_sqlite3.netstandard11/index.json 1146ms info : GET https://api.nuget.org/v3-flatcontainer/sqlitepclraw.provider.e_sqlite3.netstandard11/1.1.12/sqlitepclraw.provider.e_sqlite3.netstandard11.1.1.12.nupkg info : OK https://api.nuget.org/v3-flatcontainer/sqlitepclraw.lib.e_sqlite3.v110_xp/1.1.12/sqlitepclraw.lib.e_sqlite3.v110_xp.1.1.12.nupkg 32ms info : OK https://api.nuget.org/v3-flatcontainer/sqlitepclraw.lib.e_sqlite3.linux/1.1.12/sqlitepclraw.lib.e_sqlite3.linux.1.1.12.nupkg 33ms info : OK https://api.nuget.org/v3-flatcontainer/sqlitepclraw.lib.e_sqlite3.osx/1.1.12/sqlitepclraw.lib.e_sqlite3.osx.1.1.12.nupkg 36ms info : OK https://api.nuget.org/v3-flatcontainer/sqlitepclraw.provider.e_sqlite3.netstandard11/1.1.12/sqlitepclraw.provider.e_sqlite3.netstandard11.1.1.12.nupkg 31ms info : OK https://api.nuget.org/v3-flatcontainer/sqlitepclraw.core/index.json 1476ms info : GET https://api.nuget.org/v3-flatcontainer/sqlitepclraw.core/1.1.12/sqlitepclraw.core.1.1.12.nupkg info : OK https://api.nuget.org/v3-flatcontainer/sqlitepclraw.core/1.1.12/sqlitepclraw.core.1.1.12.nupkg 139ms info : OK https://api.nuget.org/v3-flatcontainer/microsoft.entityframeworkcore/index.json 1026ms info : GET https://api.nuget.org/v3-flatcontainer/microsoft.entityframeworkcore/2.2.4/microsoft.entityframeworkcore.2.2.4.nupkg info : OK https://api.nuget.org/v3-flatcontainer/microsoft.entityframeworkcore/2.2.4/microsoft.entityframeworkcore.2.2.4.nupkg 158ms info : GET https://api.nuget.org/v3-flatcontainer/microsoft.entityframeworkcore.abstractions/index.json info : GET https://api.nuget.org/v3-flatcontainer/microsoft.entityframeworkcore.analyzers/index.json info : OK https://api.nuget.org/v3-flatcontainer/microsoft.entityframeworkcore.abstractions/index.json 997ms info : GET https://api.nuget.org/v3-flatcontainer/microsoft.entityframeworkcore.abstractions/2.2.4/microsoft.entityframeworkcore.abstractions.2.2.4.nupkg info : OK https://api.nuget.org/v3-flatcontainer/microsoft.entityframeworkcore.analyzers/index.json 1003ms info : GET https://api.nuget.org/v3-flatcontainer/microsoft.entityframeworkcore.analyzers/2.2.4/microsoft.entityframeworkcore.analyzers.2.2.4.nupkg info : OK https://api.nuget.org/v3-flatcontainer/microsoft.entityframeworkcore.abstractions/2.2.4/microsoft.entityframeworkcore.abstractions.2.2.4.nupkg 33ms info : OK https://api.nuget.org/v3-flatcontainer/microsoft.entityframeworkcore.analyzers/2.2.4/microsoft.entityframeworkcore.analyzers.2.2.4.nupkg 33ms info : Installing Microsoft.EntityFrameworkCore.Analyzers 2.2.4. info : Installing Microsoft.EntityFrameworkCore.Abstractions 2.2.4. info : Installing Microsoft.EntityFrameworkCore 2.2.4. info : Installing SQLitePCLRaw.lib.e_sqlite3.linux 1.1.12. info : Installing SQLitePCLRaw.lib.e_sqlite3.osx 1.1.12. info : Installing SQLitePCLRaw.lib.e_sqlite3.v110_xp 1.1.12. info : Installing SQLitePCLRaw.core 1.1.12. info : Installing SQLitePCLRaw.provider.e_sqlite3.netstandard11 1.1.12. info : Installing Microsoft.EntityFrameworkCore.Relational 2.2.4. info : Installing Microsoft.Data.Sqlite.Core 2.2.4. info : Installing SQLitePCLRaw.bundle_green 1.1.12. info : Installing Microsoft.EntityFrameworkCore.Sqlite.Core 2.2.4. info : Installing Microsoft.EntityFrameworkCore.Sqlite 2.2.4. info : Package 'Microsoft.EntityFrameworkCore.SQLite' is compatible with all the specified frameworks in project '/home/teddy/Documents/dotnet/RazorPagesMovie/RazorPagesMovie.csproj'. info : PackageReference for package 'Microsoft.EntityFrameworkCore.SQLite' version '2.2.4' added to file '/home/teddy/Documents/dotnet/RazorPagesMovie/RazorPagesMovie.csproj'. info : Committing restore... info : Writing assets file to disk. Path: /home/teddy/Documents/dotnet/RazorPagesMovie/obj/project.assets.json log : Restore completed in 9.79 sec for /home/teddy/Documents/dotnet/RazorPagesMovie/RazorPagesMovie.csproj. teddy@teddy:~/Documents/dotnet/RazorPagesMovie$ dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design Writing /tmp/tmpUPJJcI.tmp info : Adding PackageReference for package 'Microsoft.VisualStudio.Web.CodeGeneration.Design' into project '/home/teddy/Documents/dotnet/RazorPagesMovie/RazorPagesMovie.csproj'. info : Restoring packages for /home/teddy/Documents/dotnet/RazorPagesMovie/RazorPagesMovie.csproj... info : GET https://api.nuget.org/v3-flatcontainer/microsoft.visualstudio.web.codegeneration.design/index.json info : OK https://api.nuget.org/v3-flatcontainer/microsoft.visualstudio.web.codegeneration.design/index.json 1296ms info : GET https://api.nuget.org/v3-flatcontainer/microsoft.visualstudio.web.codegeneration.design/2.2.3/microsoft.visualstudio.web.codegeneration.design.2.2.3.nupkg info : OK https://api.nuget.org/v3-flatcontainer/microsoft.visualstudio.web.codegeneration.design/2.2.3/microsoft.visualstudio.web.codegeneration.design.2.2.3.nupkg 35ms info : GET https://api.nuget.org/v3-flatcontainer/microsoft.visualstudio.web.codegenerators.mvc/index.json info : OK https://api.nuget.org/v3-flatcontainer/microsoft.visualstudio.web.codegenerators.mvc/index.json 1004ms info : GET https://api.nuget.org/v3-flatcontainer/microsoft.visualstudio.web.codegenerators.mvc/2.2.3/microsoft.visualstudio.web.codegenerators.mvc.2.2.3.nupkg info : OK https://api.nuget.org/v3-flatcontainer/microsoft.visualstudio.web.codegenerators.mvc/2.2.3/microsoft.visualstudio.web.codegenerators.mvc.2.2.3.nupkg 34ms info : GET https://api.nuget.org/v3-flatcontainer/microsoft.visualstudio.web.codegeneration/index.json info : OK https://api.nuget.org/v3-flatcontainer/microsoft.visualstudio.web.codegeneration/index.json 1001ms info : GET https://api.nuget.org/v3-flatcontainer/microsoft.visualstudio.web.codegeneration/2.2.3/microsoft.visualstudio.web.codegeneration.2.2.3.nupkg info : OK https://api.nuget.org/v3-flatcontainer/microsoft.visualstudio.web.codegeneration/2.2.3/microsoft.visualstudio.web.codegeneration.2.2.3.nupkg 31ms info : GET https://api.nuget.org/v3-flatcontainer/microsoft.visualstudio.web.codegeneration.entityframeworkcore/index.json info : OK https://api.nuget.org/v3-flatcontainer/microsoft.visualstudio.web.codegeneration.entityframeworkcore/index.json 1001ms info : GET https://api.nuget.org/v3-flatcontainer/microsoft.visualstudio.web.codegeneration.entityframeworkcore/2.2.3/microsoft.visualstudio.web.codegeneration.entityframeworkcore.2.2.3.nupkg info : OK https://api.nuget.org/v3-flatcontainer/microsoft.visualstudio.web.codegeneration.entityframeworkcore/2.2.3/microsoft.visualstudio.web.codegeneration.entityframeworkcore.2.2.3.nupkg 32ms info : GET https://api.nuget.org/v3-flatcontainer/microsoft.visualstudio.web.codegeneration.core/index.json info : OK https://api.nuget.org/v3-flatcontainer/microsoft.visualstudio.web.codegeneration.core/index.json 1004ms info : GET https://api.nuget.org/v3-flatcontainer/microsoft.visualstudio.web.codegeneration.core/2.2.3/microsoft.visualstudio.web.codegeneration.core.2.2.3.nupkg info : OK https://api.nuget.org/v3-flatcontainer/microsoft.visualstudio.web.codegeneration.core/2.2.3/microsoft.visualstudio.web.codegeneration.core.2.2.3.nupkg 34ms info : GET https://api.nuget.org/v3-flatcontainer/microsoft.visualstudio.web.codegeneration.templating/index.json info : OK https://api.nuget.org/v3-flatcontainer/microsoft.visualstudio.web.codegeneration.templating/index.json 994ms info : GET https://api.nuget.org/v3-flatcontainer/microsoft.visualstudio.web.codegeneration.templating/2.2.3/microsoft.visualstudio.web.codegeneration.templating.2.2.3.nupkg info : OK https://api.nuget.org/v3-flatcontainer/microsoft.visualstudio.web.codegeneration.templating/2.2.3/microsoft.visualstudio.web.codegeneration.templating.2.2.3.nupkg 31ms info : GET https://api.nuget.org/v3-flatcontainer/microsoft.visualstudio.web.codegeneration.utils/index.json info : OK https://api.nuget.org/v3-flatcontainer/microsoft.visualstudio.web.codegeneration.utils/index.json 1002ms info : GET https://api.nuget.org/v3-flatcontainer/microsoft.visualstudio.web.codegeneration.utils/2.2.3/microsoft.visualstudio.web.codegeneration.utils.2.2.3.nupkg info : OK https://api.nuget.org/v3-flatcontainer/microsoft.visualstudio.web.codegeneration.utils/2.2.3/microsoft.visualstudio.web.codegeneration.utils.2.2.3.nupkg 31ms info : GET https://api.nuget.org/v3-flatcontainer/microsoft.visualstudio.web.codegeneration.contracts/index.json info : GET https://api.nuget.org/v3-flatcontainer/microsoft.codeanalysis.csharp.workspaces/index.json info : GET https://api.nuget.org/v3-flatcontainer/nuget.frameworks/index.json info : OK https://api.nuget.org/v3-flatcontainer/microsoft.visualstudio.web.codegeneration.contracts/index.json 1000ms info : GET https://api.nuget.org/v3-flatcontainer/microsoft.visualstudio.web.codegeneration.contracts/2.2.3/microsoft.visualstudio.web.codegeneration.contracts.2.2.3.nupkg info : OK https://api.nuget.org/v3-flatcontainer/microsoft.visualstudio.web.codegeneration.contracts/2.2.3/microsoft.visualstudio.web.codegeneration.contracts.2.2.3.nupkg 31ms info : OK https://api.nuget.org/v3-flatcontainer/microsoft.codeanalysis.csharp.workspaces/index.json 1106ms info : GET https://api.nuget.org/v3-flatcontainer/microsoft.codeanalysis.csharp.workspaces/2.8.0/microsoft.codeanalysis.csharp.workspaces.2.8.0.nupkg info : OK https://api.nuget.org/v3-flatcontainer/nuget.frameworks/index.json 1132ms info : GET https://api.nuget.org/v3-flatcontainer/nuget.frameworks/4.7.0/nuget.frameworks.4.7.0.nupkg info : OK https://api.nuget.org/v3-flatcontainer/microsoft.codeanalysis.csharp.workspaces/2.8.0/microsoft.codeanalysis.csharp.workspaces.2.8.0.nupkg 35ms info : OK https://api.nuget.org/v3-flatcontainer/nuget.frameworks/4.7.0/nuget.frameworks.4.7.0.nupkg 39ms info : GET https://api.nuget.org/v3-flatcontainer/microsoft.codeanalysis.workspaces.common/index.json info : OK https://api.nuget.org/v3-flatcontainer/microsoft.codeanalysis.workspaces.common/index.json 1029ms info : GET https://api.nuget.org/v3-flatcontainer/microsoft.codeanalysis.workspaces.common/2.8.0/microsoft.codeanalysis.workspaces.common.2.8.0.nupkg info : OK https://api.nuget.org/v3-flatcontainer/microsoft.codeanalysis.workspaces.common/2.8.0/microsoft.codeanalysis.workspaces.common.2.8.0.nupkg 32ms info : GET https://api.nuget.org/v3-flatcontainer/system.composition/index.json info : GET https://api.nuget.org/v3-flatcontainer/system.linq.parallel/index.json info : OK https://api.nuget.org/v3-flatcontainer/system.composition/index.json 1004ms info : GET https://api.nuget.org/v3-flatcontainer/system.composition/1.0.31/system.composition.1.0.31.nupkg info : OK https://api.nuget.org/v3-flatcontainer/system.linq.parallel/index.json 1006ms info : GET https://api.nuget.org/v3-flatcontainer/system.linq.parallel/4.3.0/system.linq.parallel.4.3.0.nupkg info : OK https://api.nuget.org/v3-flatcontainer/system.composition/1.0.31/system.composition.1.0.31.nupkg 32ms info : GET https://api.nuget.org/v3-flatcontainer/system.composition.attributedmodel/index.json info : GET https://api.nuget.org/v3-flatcontainer/system.composition.convention/index.json info : GET https://api.nuget.org/v3-flatcontainer/system.composition.hosting/index.json info : GET https://api.nuget.org/v3-flatcontainer/system.composition.runtime/index.json info : GET https://api.nuget.org/v3-flatcontainer/system.composition.typedparts/index.json info : OK https://api.nuget.org/v3-flatcontainer/system.linq.parallel/4.3.0/system.linq.parallel.4.3.0.nupkg 39ms info : OK https://api.nuget.org/v3-flatcontainer/system.composition.attributedmodel/index.json 997ms info : GET https://api.nuget.org/v3-flatcontainer/system.composition.attributedmodel/1.0.31/system.composition.attributedmodel.1.0.31.nupkg info : OK https://api.nuget.org/v3-flatcontainer/system.composition.convention/index.json 1042ms info : GET https://api.nuget.org/v3-flatcontainer/system.composition.convention/1.0.31/system.composition.convention.1.0.31.nupkg info : OK https://api.nuget.org/v3-flatcontainer/system.composition.attributedmodel/1.0.31/system.composition.attributedmodel.1.0.31.nupkg 76ms info : OK https://api.nuget.org/v3-flatcontainer/system.composition.convention/1.0.31/system.composition.convention.1.0.31.nupkg 35ms info : OK https://api.nuget.org/v3-flatcontainer/system.composition.typedparts/index.json 1109ms info : GET https://api.nuget.org/v3-flatcontainer/system.composition.typedparts/1.0.31/system.composition.typedparts.1.0.31.nupkg info : OK https://api.nuget.org/v3-flatcontainer/system.composition.runtime/index.json 1120ms info : GET https://api.nuget.org/v3-flatcontainer/system.composition.runtime/1.0.31/system.composition.runtime.1.0.31.nupkg info : OK https://api.nuget.org/v3-flatcontainer/system.composition.typedparts/1.0.31/system.composition.typedparts.1.0.31.nupkg 33ms info : OK https://api.nuget.org/v3-flatcontainer/system.composition.hosting/index.json 1149ms info : GET https://api.nuget.org/v3-flatcontainer/system.composition.hosting/1.0.31/system.composition.hosting.1.0.31.nupkg info : OK https://api.nuget.org/v3-flatcontainer/system.composition.runtime/1.0.31/system.composition.runtime.1.0.31.nupkg 38ms info : OK https://api.nuget.org/v3-flatcontainer/system.composition.hosting/1.0.31/system.composition.hosting.1.0.31.nupkg 39ms info : Installing System.Composition.Hosting 1.0.31. info : Installing System.Composition.Runtime 1.0.31. info : Installing System.Composition.TypedParts 1.0.31. info : Installing System.Composition.Convention 1.0.31. info : Installing System.Composition.AttributedModel 1.0.31. info : Installing System.Composition 1.0.31. info : Installing System.Linq.Parallel 4.3.0. info : Installing Microsoft.CodeAnalysis.Workspaces.Common 2.8.0. info : Installing Microsoft.CodeAnalysis.CSharp.Workspaces 2.8.0. info : Installing NuGet.Frameworks 4.7.0. info : Installing Microsoft.VisualStudio.Web.CodeGeneration.Contracts 2.2.3. info : Installing Microsoft.VisualStudio.Web.CodeGeneration.Utils 2.2.3. info : Installing Microsoft.VisualStudio.Web.CodeGeneration.Templating 2.2.3. info : Installing Microsoft.VisualStudio.Web.CodeGeneration.Core 2.2.3. info : Installing Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCore 2.2.3. info : Installing Microsoft.VisualStudio.Web.CodeGeneration 2.2.3. info : Installing Microsoft.VisualStudio.Web.CodeGeneration.Design 2.2.3. info : Installing Microsoft.VisualStudio.Web.CodeGenerators.Mvc 2.2.3. info : Package 'Microsoft.VisualStudio.Web.CodeGeneration.Design' is compatible with all the specified frameworks in project '/home/teddy/Documents/dotnet/RazorPagesMovie/RazorPagesMovie.csproj'. info : PackageReference for package 'Microsoft.VisualStudio.Web.CodeGeneration.Design' version '2.2.3' added to file '/home/teddy/Documents/dotnet/RazorPagesMovie/RazorPagesMovie.csproj'. info : Committing restore... info : Generating MSBuild file /home/teddy/Documents/dotnet/RazorPagesMovie/obj/RazorPagesMovie.csproj.nuget.g.props. info : Writing assets file to disk. Path: /home/teddy/Documents/dotnet/RazorPagesMovie/obj/project.assets.json log : Restore completed in 14.77 sec for /home/teddy/Documents/dotnet/RazorPagesMovie/RazorPagesMovie.csproj. teddy@teddy:~/Documents/dotnet/RazorPagesMovie$ dotnet add package Microsoft.EntityFrameworkCore.Design Writing /tmp/tmprEZck2.tmp info : Adding PackageReference for package 'Microsoft.EntityFrameworkCore.Design' into project '/home/teddy/Documents/dotnet/RazorPagesMovie/RazorPagesMovie.csproj'. info : Restoring packages for /home/teddy/Documents/dotnet/RazorPagesMovie/RazorPagesMovie.csproj... info : GET https://api.nuget.org/v3-flatcontainer/microsoft.entityframeworkcore.design/index.json info : OK https://api.nuget.org/v3-flatcontainer/microsoft.entityframeworkcore.design/index.json 1252ms info : GET https://api.nuget.org/v3-flatcontainer/microsoft.entityframeworkcore.design/2.2.4/microsoft.entityframeworkcore.design.2.2.4.nupkg info : OK https://api.nuget.org/v3-flatcontainer/microsoft.entityframeworkcore.design/2.2.4/microsoft.entityframeworkcore.design.2.2.4.nupkg 34ms info : Installing Microsoft.EntityFrameworkCore.Design 2.2.4. info : Package 'Microsoft.EntityFrameworkCore.Design' is compatible with all the specified frameworks in project '/home/teddy/Documents/dotnet/RazorPagesMovie/RazorPagesMovie.csproj'. info : PackageReference for package 'Microsoft.EntityFrameworkCore.Design' version '2.2.4' added to file '/home/teddy/Documents/dotnet/RazorPagesMovie/RazorPagesMovie.csproj'. info : Committing restore... info : Generating MSBuild file /home/teddy/Documents/dotnet/RazorPagesMovie/obj/RazorPagesMovie.csproj.nuget.g.props. info : Writing assets file to disk. Path: /home/teddy/Documents/dotnet/RazorPagesMovie/obj/project.assets.json log : Restore completed in 2.01 sec for /home/teddy/Documents/dotnet/RazorPagesMovie/RazorPagesMovie.csproj. |
The Microsoft.VisualStudio.Web.CodeGeneration.Design package is required for scaffolding.
Register the database context
Add the following using statements at the top of Startup.cs:
|
1 2 3 4 |
... using RazorPagesMovie.Models; using Microsoft.EntityFrameworkCore; ... |
Register the database context with the dependency injection container in Startup.cs in ConfigureServices function:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
... namespace RazorPagesMovie { public class Startup { ... // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.Configure<CookiePolicyOptions>(options => { ... }); services.AddDbContext<RazorPagesMovieContext>(options => options.UseSqlite(Configuration.GetConnectionString("MovieContext"))); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); } ... } } |
Build the project (‘dotnet build’) to verify there are no compilation errors.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
teddy@teddy:~/Documents/dotnet/RazorPagesMovie$ dotnet build Microsoft (R) Build Engine version 16.1.76+g14b0a930a7 for .NET Core Copyright (C) Microsoft Corporation. All rights reserved. Restore completed in 563.27 ms for /home/teddy/Documents/dotnet/RazorPagesMovie/RazorPagesMovie.csproj. RazorPagesMovie -> /home/teddy/Documents/dotnet/RazorPagesMovie/bin/Debug/netcoreapp2.2/RazorPagesMovie.dll RazorPagesMovie -> /home/teddy/Documents/dotnet/RazorPagesMovie/bin/Debug/netcoreapp2.2/RazorPagesMovie.Views.dll Build succeeded. 0 Warning(s) 0 Error(s) Time Elapsed 00:00:04.39 |
Scaffold the movie model
In this section, the movie model is scaffolded. That is, the scaffolding tool produces pages for Create, Read, Update, and Delete (CRUD) operations for the movie model.
Install the scaffolding tool:
|
1 2 3 |
teddy@teddy:~/Documents/dotnet/RazorPagesMovie$ dotnet tool install --global dotnet-aspnet-codegenerator You can invoke the tool using the following command: dotnet-aspnet-codegenerator Tool 'dotnet-aspnet-codegenerator' (version '2.2.3') was successfully installed. |
Run the scaffolding tool to create CRUD for model ‘Movie’:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
teddy@teddy:~/Documents/dotnet/RazorPagesMovie$ dotnet aspnet-codegenerator razorpage -m Movie -dc RazorPagesMovieContext -udl -outDir Pages/Movies --referenceScriptLibraries Building project ... Finding the generator 'razorpage'... Running the generator 'razorpage'... Attempting to compile the application in memory. Attempting to figure out the EntityFramework metadata for the model and DbContext: 'Movie' info: Microsoft.EntityFrameworkCore.Infrastructure[10403] Entity Framework Core 2.2.4-servicing-10062 initialized 'RazorPagesMovieContext' using provider 'Microsoft.EntityFrameworkCore.Sqlite' with options: None Added Razor Page : /Pages/Movies/Create.cshtml Added PageModel : /Pages/Movies/Create.cshtml.cs Added Razor Page : /Pages/Movies/Edit.cshtml Added PageModel : /Pages/Movies/Edit.cshtml.cs Added Razor Page : /Pages/Movies/Details.cshtml Added PageModel : /Pages/Movies/Details.cshtml.cs Added Razor Page : /Pages/Movies/Delete.cshtml Added PageModel : /Pages/Movies/Delete.cshtml.cs Added Razor Page : /Pages/Movies/Index.cshtml Added PageModel : /Pages/Movies/Index.cshtml.cs RunTime 00:00:06.54 |
The scaffold process creates and updates the following files:
Files created
Pages/Movies: Create, Delete, Details, Edit, and Index.
Data/RazorPagesMovieContext.cs
File updated
Startup.cs
The created and updated files are explained in the next section.