68 KiB
title | author | description | ms.author | ms.custom | ms.date | no-loc | uid | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Razor Pages with Entity Framework Core in ASP.NET Core - Tutorial 1 of 8 | rick-anderson | Shows how to create a Razor Pages app using Entity Framework Core | riande | mvc, seodec18 | 3/3/2021 |
|
data/ef-rp/intro |
Razor Pages with Entity Framework Core in ASP.NET Core - Tutorial 1 of 8
By Tom Dykstra, Jeremy Likness, and Jon P Smith
::: moniker range=">= aspnetcore-5.0"
This is the first in a series of tutorials that show how to use Entity Framework (EF) Core in an ASP.NET Core Razor Pages app. The tutorials build a web site for a fictional Contoso University. The site includes functionality such as student admission, course creation, and instructor assignments. The tutorial uses the code first approach. For information on following this tutorial using the database first approach, see this Github issue.
Download or view the completed app. Download instructions.
Prerequisites
- If you're new to Razor Pages, go through the Get started with Razor Pages tutorial series before starting this one.
Visual Studio
[!INCLUDEVS prereqs]
Database engines
The Visual Studio instructions use SQL Server LocalDB, a version of SQL Server Express that runs only on Windows.
Visual Studio Code
[!INCLUDEVS Code prereqs]
Consider downloading and installing a third-party tool for managing and viewing a SQLite database, such as DB Browser for SQLite.
Database engines
The Visual Studio Code tab use SQLite, a cross-platform database engine.
Troubleshooting
If you run into a problem you can't resolve, compare your code to the completed project. A good way to get help is by posting a question to StackOverflow.com, using the ASP.NET Core tag or the EF Core tag.
The sample app
The app built in these tutorials is a basic university web site. Users can view and update student, course, and instructor information. Here are a few of the screens created in the tutorial.
The UI style of this site is based on the built-in project templates. The tutorial's focus is on how to use EF Core with ASP.NET Core, not how to customize the UI.
Optional: Build the sample download
This step is optional. Building the completed app is recommended when you have problems you can't solve. If you run into a problem you can't resolve, compare your code to the completed project. Download instructions.
Visual Studio
Select ContosoUniversity.csproj to open the project.
- Build the project.
- In Package Manager Console (PMC) run the following command:
Update-Database
Visual Studio Code
-
Remove the comments from the appsettings.Development.json file so the SQLite connections string is used: [!code-jsonMain]
-
Remove the comments from the ContosoUniversity.csproj file so
SQLiteVersion
is defined:<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> <DefineConstants>TRACE;SQLiteVersion</DefineConstants> </PropertyGroup>
-
In a command window, update the database:
dotnet tool install --global dotnet-ef dotnet ef database update
Run the project to seed the database.
Create the web app project
Visual Studio
- Start Visual Studio and select Create a new project.
- In the Create a new project dialog, select ASP.NET Core Web Application > Next.
- In the Configure your new project dialog, enter
ContosoUniversity
for Project name. It's important to use this exact name including capitalization, so eachnamespace
matches when code is copied. - Select Create.
- In the Create a new ASP.NET Core web application dialog, select:
Visual Studio Code
-
In a terminal, navigate to the folder in which the project folder should be created.
-
Run the following commands to create a Razor Pages project and
cd
into the new project folder:dotnet new webapp -o ContosoUniversity cd ContosoUniversity
Set up the site style
Copy and paste the following code into the Pages/Shared/_Layout.cshtml file:
[!code-cshtmlMain]
The layout file sets the site header, footer, and menu. The preceding code makes the following changes:
- Each occurrence of "ContosoUniversity" to "Contoso University". There are three occurrences.
- The Home and Privacy menu entries are deleted.
- Entries are added for About, Students, Courses, Instructors, and Departments.
In Pages/Index.cshtml, replace the contents of the file with the following code:
[!code-cshtmlMain]
The preceding code replaces the text about ASP.NET Core with text about this app.
Run the app to verify that the home page appears.
The data model
The following sections create a data model:
A student can enroll in any number of courses, and a course can have any number of students enrolled in it.
The Student entity
-
Create a Models folder in the project folder.
-
Create Models/Student.cs with the following code:
[!code-csharpMain]
The ID
property becomes the primary key column of the database table that corresponds to this class. By default, EF Core interprets a property that's named ID
or classnameID
as the primary key. So the alternative automatically recognized name for the Student
class primary key is StudentID
. For more information, see EF Core - Keys.
The Enrollments
property is a navigation property. Navigation properties hold other entities that are related to this entity. In this case, the Enrollments
property of a Student
entity holds all of the Enrollment
entities that are related to that Student. For example, if a Student row in the database has two related Enrollment rows, the Enrollments
navigation property contains those two Enrollment entities.
In the database, an Enrollment row is related to a Student row if its StudentID
column contains the student's ID value. For example, suppose a Student row has ID=1. Related Enrollment rows will have StudentID
= 1. StudentID
is a foreign key in the Enrollment table.
The Enrollments
property is defined as ICollection<Enrollment>
because there may be multiple related Enrollment entities. Other collection types can be used, such as List<Enrollment>
or HashSet<Enrollment>
. When ICollection<Enrollment>
is used, EF Core creates a HashSet<Enrollment>
collection by default.
The Enrollment entity
Create Models/Enrollment.cs with the following code:
The EnrollmentID
property is the primary key; this entity uses the classnameID
pattern instead of ID
by itself. For a production data model, many developers choose one pattern and use it consistently. This tutorial uses both just to illustrate that both work. Using ID
without classname
makes it easier to implement some kinds of data model changes.
The Grade
property is an enum
. The question mark after the Grade
type declaration indicates that the Grade
property is nullable. A grade that's null is different from a zero grade—null means a grade isn't known or hasn't been assigned yet.
The StudentID
property is a foreign key, and the corresponding navigation property is Student
. An Enrollment
entity is associated with one Student
entity, so the property contains a single Student
entity.
The CourseID
property is a foreign key, and the corresponding navigation property is Course
. An Enrollment
entity is associated with one Course
entity.
EF Core interprets a property as a foreign key if it's named <navigation property name><primary key property name>
. For example,StudentID
is the foreign key for the Student
navigation property, since the Student
entity's primary key is ID
. Foreign key properties can also be named <primary key property name>
. For example, CourseID
since the Course
entity's primary key is CourseID
.
The Course entity
Create Models/Course.cs with the following code:
[!code-csharpMain]
The Enrollments
property is a navigation property. A Course
entity can be related to any number of Enrollment
entities.
The DatabaseGenerated
attribute allows the app to specify the primary key rather than having the database generate it.
Build the project to validate that there are no compiler errors.
Scaffold Student pages
In this section, the ASP.NET Core scaffolding tool is used to generate:
- An EF Core
DbContext
class. The context is the main class that coordinates Entity Framework functionality for a given data model. It derives from the xref:Microsoft.EntityFrameworkCore.DbContext?displayProperty=fullName class. - Razor pages that handle Create, Read, Update, and Delete (CRUD) operations for the
Student
entity.
Visual Studio
- Create a Pages/Students folder.
- In Solution Explorer, right-click the Pages/Students folder and select Add > New Scaffolded Item.
- In the Add New Scaffold Item dialog:
- In the left tab, select Installed > Common > Razor Pages
- Select Razor Pages using Entity Framework (CRUD) > ADD.
- In the Add Razor Pages using Entity Framework (CRUD) dialog:
- In the Model class drop-down, select Student (ContosoUniversity.Models).
- In the Data context class row, select the + (plus) sign.
- Change the data context name to end in
SchoolContext
rather thanContosoUniversityContext
. The updated context name:ContosoUniversity.Data.SchoolContext
- Select Add to finish adding the data context class.
- Change the data context name to end in
- Select Add to finish the Add Razor Pages dialog.
If scaffolding fails with the error 'Install the package Microsoft.VisualStudio.Web.CodeGeneration.Design and try again.'
, run the scaffold tool again or see this GitHub issue.
The following packages are automatically installed:
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
Microsoft.VisualStudio.Web.CodeGeneration.Design
Visual Studio Code
-
Run the following .NET Core CLI commands to install required NuGet packages:
dotnet add package Microsoft.EntityFrameworkCore.SQLite -v 5.0.0-* dotnet add package Microsoft.EntityFrameworkCore.SqlServer -v 5.0.0-* dotnet add package Microsoft.EntityFrameworkCore.Design -v 5.0.0-* dotnet add package Microsoft.EntityFrameworkCore.Tools -v 5.0.0-* dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design -v 5.0.0-* dotnet add package Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore -v 5.0.0-*
The
Microsoft.VisualStudio.Web.CodeGeneration.Design
package is required for scaffolding. Although the app won't use SQL Server, the scaffolding tool needs the SQL Server package. -
Create a Pages/Students folder.
-
Run the following command to install the aspnet-codegenerator scaffolding tool.
dotnet tool install --global dotnet-aspnet-codegenerator
-
Run the following command to scaffold Student pages.
On Windows
dotnet aspnet-codegenerator razorpage -m Student -dc ContosoUniversity.Data.SchoolContext -udl -outDir Pages\Students --referenceScriptLibraries -sqlite
On macOS or Linux
dotnet aspnet-codegenerator razorpage -m Student -dc ContosoUniversity.Data.SchoolContext -udl -outDir Pages/Students --referenceScriptLibraries -sqlite
If the preceding step fails, build the project and retry the scaffold step.
The scaffolding process:
- Creates Razor pages in the Pages/Students folder:
- Create.cshtml and Create.cshtml.cs
- Delete.cshtml and Delete.cshtml.cs
- Details.cshtml and Details.cshtml.cs
- Edit.cshtml and Edit.cshtml.cs
- Index.cshtml and Index.cshtml.cs
- Creates Data/SchoolContext.cs.
- Adds the context to dependency injection in Startup.cs.
- Adds a database connection string to appsettings.json.
Database connection string
The scaffolding tool generates a connection string in the appsettings.json file.
Visual Studio
The connection string specifies SQL Server LocalDB:
[!code-jsonMain]
LocalDB is a lightweight version of the SQL Server Express Database Engine and is intended for app development, not production use. By default, LocalDB creates .mdf files in the C:/Users/<user>
directory.
Visual Studio Code
Shorten the SQLite connection string to CU.db:
[!code-jsonMain]
Don't change the directory without making sure it's valid.
Update the database context class
The main class that coordinates EF Core functionality for a given data model is the database context class. The context is derived from Microsoft.EntityFrameworkCore.DbContext. The context specifies which entities are included in the data model. In this project, the class is named SchoolContext
.
Update Data/SchoolContext.cs with the following code:
[!code-csharpMain]
The preceding code changes from the singular DbSet<Student> Student
to the plural DbSet<Student> Students
. To make the Razor Pages code match the new DBSet
name, make a global change from:
_context.Student.
to:
_context.Students.
There are 8 occurrences.
Because an entity set contains multiple entities, many developers prefer the DBSet
property names should be plural.
The highlighted code:
- Creates a DbSet<TEntity> property for each entity set. In EF Core terminology:
- An entity set typically corresponds to a database table.
- An entity corresponds to a row in the table.
- Calls xref:Microsoft.EntityFrameworkCore.DbContext.OnModelCreating%2A.
OnModelCreating
:- Is called when
SchoolContext
has been initialized, but before the model has been locked down and used to initialize the context. - Is required because later in the tutorial the
Student
entity will have references to the other entities.
- Is called when
Build the project to verify there are no compiler errors.
Startup.cs
ASP.NET Core is built with dependency injection. Services such as the SchoolContext
are registered with dependency injection during app startup. Components that require these services, such as Razor Pages, are provided these services via constructor parameters. The constructor code that gets a database context instance is shown later in the tutorial.
The scaffolding tool automatically registered the context class with the dependency injection container.
Visual Studio
The following highlighted lines were added by the scaffolder:
[!code-csharpMain]
Visual Studio Code
Verify the code added by the scaffolder calls UseSqlite
.
[!code-csharpMain]
See Use SQLite for development, SQL Server for production for information on using a production database.
The name of the connection string is passed in to the context by calling a method on a DbContextOptions object. For local development, the ASP.NET Core configuration system reads the connection string from the appsettings.json file.
Add the database exception filter
Add xref:Microsoft.Extensions.DependencyInjection.DatabaseDeveloperPageExceptionFilterServiceExtensions.AddDatabaseDeveloperPageExceptionFilter%2A to ConfigureServices
as shown in the following code:
Visual Studio
[!code-csharpMain]
Add the Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore NuGet package.
In the Package Manager Console, enter the following to add the NuGet package:
Install-Package Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore
Visual Studio Code
[!code-csharpMain]
The Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore
NuGet package provides ASP.NET Core middleware for Entity Framework Core error pages. This middleware helps to detect and diagnose errors with Entity Framework Core migrations.
The AddDatabaseDeveloperPageExceptionFilter
provides helpful error information in the development environment.
Create the database
Update Program.cs to create the database if it doesn't exist:
[!code-csharpMain]
The EnsureCreated method takes no action if a database for the context exists. If no database exists, it creates the database and schema. EnsureCreated
enables the following workflow for handling data model changes:
- Delete the database. Any existing data is lost.
- Change the data model. For example, add an
EmailAddress
field. - Run the app.
EnsureCreated
creates a database with the new schema.
This workflow works early in development when the schema is rapidly evolving, as long as data doesn't need to be preserved. The situation is different when data that has been entered into the database needs to be preserved. When that is the case, use migrations.
Later in the tutorial series, the database is deleted that was created by EnsureCreated
and migrations is used. A database that is created by EnsureCreated
can't be updated by using migrations.
Test the app
- Run the app.
- Select the Students link and then Create New.
- Test the Edit, Details, and Delete links.
Seed the database
The EnsureCreated
method creates an empty database. This section adds code that populates the database with test data.
Create Data/DbInitializer.cs with the following code:
[!code-csharpMain]
The code checks if there are any students in the database. If there are no students, it adds test data to the database. It creates the test data in arrays rather than List<T>
collections to optimize performance.
-
In Program.cs, remove
//
from theDbInitializer.Initialize
line:context.Database.EnsureCreated(); DbInitializer.Initialize(context);
Visual Studio
-
Stop the app if it's running, and run the following command in the Package Manager Console (PMC):
Drop-Database -Confirm
-
Respond with
Y
to delete the database.
Visual Studio Code
- Stop the app if it's running, and delete the CU.db file.
- Restart the app.
- Select the Students page to see the seeded data.
View the database
Visual Studio
- Open SQL Server Object Explorer (SSOX) from the View menu in Visual Studio.
- In SSOX, select (localdb)\MSSQLLocalDB > Databases > SchoolContext-{GUID}. The database name is generated from the context name provided earlier plus a dash and a GUID.
- Expand the Tables node.
- Right-click the Student table and click View Data to see the columns created and the rows inserted into the table.
- Right-click the Student table and click View Code to see how the
Student
model maps to theStudent
table schema.
Visual Studio Code
Use a SQLite tool to view the database schema and seeded data. The database file is named CU.db and is located in the project folder.
Asynchronous code
Asynchronous programming is the default mode for ASP.NET Core and EF Core.
A web server has a limited number of threads available, and in high load situations all of the available threads might be in use. When that happens, the server can't process new requests until the threads are freed up. With synchronous code, many threads may be tied up while they aren't doing work because they're waiting for I/O to complete. With asynchronous code, when a process is waiting for I/O to complete, its thread is freed up for the server to use for processing other requests. As a result, asynchronous code enables server resources to be used more efficiently, and the server can handle more traffic without delays.
Asynchronous code does introduce a small amount of overhead at run time. For low traffic situations, the performance hit is negligible, while for high traffic situations, the potential performance improvement is substantial.
In the following code, the async keyword, Task
return value, await
keyword, and ToListAsync
method make the code execute asynchronously.
public async Task OnGetAsync()
{
Students = await _context.Students.ToListAsync();
}
- The
async
keyword tells the compiler to:- Generate callbacks for parts of the method body.
- Create the Task object that's returned.
- The
Task
return type represents ongoing work. - The
await
keyword causes the compiler to split the method into two parts. The first part ends with the operation that's started asynchronously. The second part is put into a callback method that's called when the operation completes. ToListAsync
is the asynchronous version of theToList
extension method.
Some things to be aware of when writing asynchronous code that uses EF Core:
- Only statements that cause queries or commands to be sent to the database are executed asynchronously. That includes
ToListAsync
,SingleOrDefaultAsync
,FirstOrDefaultAsync
, andSaveChangesAsync
. It doesn't include statements that just change anIQueryable
, such asvar students = context.Students.Where(s => s.LastName == "Davolio")
. - An EF Core context isn't thread safe: don't try to do multiple operations in parallel.
- To take advantage of the performance benefits of async code, verify that library packages (such as for paging) use async if they call EF Core methods that send queries to the database.
For more information about asynchronous programming in .NET, see Async Overview and Asynchronous programming with async and await.
Performance considerations
In general, a web page shouldn't be loading an arbitrary number of rows. A query should use paging or a limiting approach. For example, the preceding query could use Take
to limit the rows returned:
[!code-csharpMain]
Enumerating a large table in a view could return a partially constructed HTTP 200 response if a database exception occurs part way through the enumeration.
xref:Microsoft.AspNetCore.Mvc.MvcOptions.MaxModelBindingCollectionSize defaults to 1024. The following code sets MaxModelBindingCollectionSize
:
[!code-csharpMain]
See Configuration for information on configuration settings like MyMaxModelBindingCollectionSize
.
Paging is covered later in the tutorial.
For more information, see Performance considerations (EF).
[!INCLUDEs]
Next steps
Use SQLite for development, SQL Server for production
[!div class="step-by-step"] Next tutorial
::: moniker-end
::: moniker range=">= aspnetcore-3.0 < aspnetcore-5.0"
This is the first in a series of tutorials that show how to use Entity Framework (EF) Core in an ASP.NET Core Razor Pages app. The tutorials build a web site for a fictional Contoso University. The site includes functionality such as student admission, course creation, and instructor assignments. The tutorial uses the code first approach. For information on following this tutorial using the database first approach, see this Github issue.
Download or view the completed app. Download instructions.
Prerequisites
- If you're new to Razor Pages, go through the Get started with Razor Pages tutorial series before starting this one.
Visual Studio
[!INCLUDEVS prereqs]
Visual Studio Code
[!INCLUDEVS Code prereqs]
Database engines
The Visual Studio instructions use SQL Server LocalDB, a version of SQL Server Express that runs only on Windows.
The Visual Studio Code instructions use SQLite, a cross-platform database engine.
If you choose to use SQLite, download and install a third-party tool for managing and viewing a SQLite database, such as DB Browser for SQLite.
Troubleshooting
If you run into a problem you can't resolve, compare your code to the completed project. A good way to get help is by posting a question to StackOverflow.com, using the ASP.NET Core tag or the EF Core tag.
The sample app
The app built in these tutorials is a basic university web site. Users can view and update student, course, and instructor information. Here are a few of the screens created in the tutorial.
The UI style of this site is based on the built-in project templates. The tutorial's focus is on how to use EF Core, not how to customize the UI.
Follow the link at the top of the page to get the source code for the completed project. The cu30 folder has the code for the ASP.NET Core 3.0 version of the tutorial. Files that reflect the state of the code for tutorials 1-7 can be found in the cu30snapshots folder.
Visual Studio
To run the app after downloading the completed project:
-
Build the project.
-
In Package Manager Console (PMC) run the following command:
Update-Database
-
Run the project to seed the database.
Visual Studio Code
To run the app after downloading the completed project:
-
Delete ContosoUniversity.csproj, and rename ContosoUniversitySQLite.csproj to ContosoUniversity.csproj.
-
In Program.cs, comment out
#define Startup
soStartupSQLite
is used. -
Delete appSettings.json, and rename appSettingsSQLite.json to appSettings.json.
-
Delete the Migrations folder, and rename MigrationsSQL to Migrations.
-
Do a global search for
#if SQLiteVersion
and remove#if SQLiteVersion
and the associated#endif
statement. -
Build the project.
-
At a command prompt in the project folder, run the following commands:
dotnet tool uninstall --global dotnet-ef dotnet tool install --global dotnet-ef --version 5.0.0-* dotnet ef database update
-
In your SQLite tool, run the following SQL statement:
UPDATE Department SET RowVersion = randomblob(8)
-
Run the project to seed the database.
Create the web app project
Visual Studio
- From the Visual Studio File menu, select New > Project.
- Select ASP.NET Core Web Application.
- Name the project ContosoUniversity. It's important to use this exact name including capitalization, so the namespaces match when code is copied and pasted.
- Select .NET Core and ASP.NET Core 3.0 in the dropdowns, and then select Web Application.
Visual Studio Code
-
In a terminal, navigate to the folder in which the project folder should be created.
-
Run the following commands to create a Razor Pages project and
cd
into the new project folder:dotnet new webapp -o ContosoUniversity cd ContosoUniversity
Set up the site style
Set up the site header, footer, and menu by updating Pages/Shared/_Layout.cshtml:
-
Change each occurrence of "ContosoUniversity" to "Contoso University". There are three occurrences.
-
Delete the Home and Privacy menu entries, and add entries for About, Students, Courses, Instructors, and Departments.
The changes are highlighted.
[!code-cshtmlMain]
In Pages/Index.cshtml, replace the contents of the file with the following code to replace the text about ASP.NET Core with text about this app:
[!code-cshtmlMain]
Run the app to verify that the home page appears.
The data model
The following sections create a data model:
A student can enroll in any number of courses, and a course can have any number of students enrolled in it.
The Student entity
-
Create a Models folder in the project folder.
-
Create Models/Student.cs with the following code:
[!code-csharpMain]
The ID
property becomes the primary key column of the database table that corresponds to this class. By default, EF Core interprets a property that's named ID
or classnameID
as the primary key. So the alternative automatically recognized name for the Student
class primary key is StudentID
. For more information, see EF Core - Keys.
The Enrollments
property is a navigation property. Navigation properties hold other entities that are related to this entity. In this case, the Enrollments
property of a Student
entity holds all of the Enrollment
entities that are related to that Student. For example, if a Student row in the database has two related Enrollment rows, the Enrollments
navigation property contains those two Enrollment entities.
In the database, an Enrollment row is related to a Student row if its StudentID column contains the student's ID value. For example, suppose a Student row has ID=1. Related Enrollment rows will have StudentID = 1. StudentID is a foreign key in the Enrollment table.
The Enrollments
property is defined as ICollection<Enrollment>
because there may be multiple related Enrollment entities. You can use other collection types, such as List<Enrollment>
or HashSet<Enrollment>
. When ICollection<Enrollment>
is used, EF Core creates a HashSet<Enrollment>
collection by default.
The Enrollment entity
Create Models/Enrollment.cs with the following code:
[!code-csharpMain]
The EnrollmentID
property is the primary key; this entity uses the classnameID
pattern instead of ID
by itself. For a production data model, choose one pattern and use it consistently. This tutorial uses both just to illustrate that both work. Using ID
without classname
makes it easier to implement some kinds of data model changes.
The Grade
property is an enum
. The question mark after the Grade
type declaration indicates that the Grade
property is nullable. A grade that's null is different from a zero grade—null means a grade isn't known or hasn't been assigned yet.
The StudentID
property is a foreign key, and the corresponding navigation property is Student
. An Enrollment
entity is associated with one Student
entity, so the property contains a single Student
entity.
The CourseID
property is a foreign key, and the corresponding navigation property is Course
. An Enrollment
entity is associated with one Course
entity.
EF Core interprets a property as a foreign key if it's named <navigation property name><primary key property name>
. For example,StudentID
is the foreign key for the Student
navigation property, since the Student
entity's primary key is ID
. Foreign key properties can also be named <primary key property name>
. For example, CourseID
since the Course
entity's primary key is CourseID
.
The Course entity
Create Models/Course.cs with the following code:
[!code-csharpMain]
The Enrollments
property is a navigation property. A Course
entity can be related to any number of Enrollment
entities.
The DatabaseGenerated
attribute allows the app to specify the primary key rather than having the database generate it.
Build the project to validate that there are no compiler errors.
Scaffold Student pages
In this section, you use the ASP.NET Core scaffolding tool to generate:
- An EF Core context class. The context is the main class that coordinates Entity Framework functionality for a given data model. It derives from the
Microsoft.EntityFrameworkCore.DbContext
class. - Razor pages that handle Create, Read, Update, and Delete (CRUD) operations for the
Student
entity.
Visual Studio
- Create a Students folder in the Pages folder.
- In Solution Explorer, right-click the Pages/Students folder and select Add > New Scaffolded Item.
- In the Add Scaffold dialog, select Razor Pages using Entity Framework (CRUD) > ADD.
- In the Add Razor Pages using Entity Framework (CRUD) dialog:
- In the Model class drop-down, select Student (ContosoUniversity.Models).
- In the Data context class row, select the + (plus) sign.
- Change the data context name from ContosoUniversity.Models.ContosoUniversityContext to ContosoUniversity.Data.SchoolContext.
- Select Add.
The following packages are automatically installed:
Microsoft.VisualStudio.Web.CodeGeneration.Design
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.Extensions.Logging.Debug
Microsoft.EntityFrameworkCore.Tools
Visual Studio Code
- Run the following .NET Core CLI commands to install required NuGet packages:
dotnet add package Microsoft.EntityFrameworkCore.SQLite
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.EntityFrameworkCore.Tools
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
dotnet add package Microsoft.Extensions.Logging.Debug
The Microsoft.VisualStudio.Web.CodeGeneration.Design package is required for scaffolding. Although the app won't use SQL Server, the scaffolding tool needs the SQL Server package.
-
Create a Pages/Students folder.
-
Run the following command to install the aspnet-codegenerator scaffolding tool.
dotnet tool install --global dotnet-aspnet-codegenerator
-
Run the following command to scaffold Student pages.
On Windows
dotnet aspnet-codegenerator razorpage -m Student -dc ContosoUniversity.Data.SchoolContext -udl -outDir Pages\Students --referenceScriptLibraries
On macOS or Linux
dotnet aspnet-codegenerator razorpage -m Student -dc ContosoUniversity.Data.SchoolContext -udl -outDir Pages/Students --referenceScriptLibraries
If you have a problem with the preceding step, build the project and retry the scaffold step.
The scaffolding process:
- Creates Razor pages in the Pages/Students folder:
- Create.cshtml and Create.cshtml.cs
- Delete.cshtml and Delete.cshtml.cs
- Details.cshtml and Details.cshtml.cs
- Edit.cshtml and Edit.cshtml.cs
- Index.cshtml and Index.cshtml.cs
- Creates Data/SchoolContext.cs.
- Adds the context to dependency injection in Startup.cs.
- Adds a database connection string to appsettings.json.
Database connection string
Visual Studio
The appsettings.json file specifies the connection string SQL Server LocalDB.
[!code-jsonMain]
LocalDB is a lightweight version of the SQL Server Express Database Engine and is intended for app development, not production use. By default, LocalDB creates .mdf files in the C:/Users/<user>
directory.
Visual Studio Code
Change the connection string to point to a SQLite database file named CU.db:
[!code-jsonMain]
Update the database context class
The main class that coordinates EF Core functionality for a given data model is the database context class. The context is derived from Microsoft.EntityFrameworkCore.DbContext. The context specifies which entities are included in the data model. In this project, the class is named SchoolContext
.
Update Data/SchoolContext.cs with the following code:
[!code-csharpMain]
The highlighted code creates a DbSet<TEntity> property for each entity set. In EF Core terminology:
- An entity set typically corresponds to a database table.
- An entity corresponds to a row in the table.
Since an entity set contains multiple entities, the DBSet properties should be plural names. Since the scaffolding tool created aStudent
DBSet, this step changes it to plural Students
.
To make the Razor Pages code match the new DBSet name, make a global change across the whole project of _context.Student
to _context.Students
. There are 8 occurrences.
Build the project to verify there are no compiler errors.
Startup.cs
ASP.NET Core is built with dependency injection. Services (such as the EF Core database context) are registered with dependency injection 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 database context instance is shown later in the tutorial.
The scaffolding tool automatically registered the context class with the dependency injection container.
Visual Studio
-
In
ConfigureServices
, the highlighted lines were added by the scaffolder:[!code-csharpMain]
Visual Studio Code
-
In
ConfigureServices
, make sure the code added by the scaffolder callsUseSqlite
.[!code-csharpMain]
The name of the connection string is passed in to the context by calling a method on a DbContextOptions object. For local development, the ASP.NET Core configuration system reads the connection string from the appsettings.json file.
Create the database
Update Program.cs to create the database if it doesn't exist:
[!code-csharpMain]
The EnsureCreated method takes no action if a database for the context exists. If no database exists, it creates the database and schema. EnsureCreated
enables the following workflow for handling data model changes:
- Delete the database. Any existing data is lost.
- Change the data model. For example, add an
EmailAddress
field. - Run the app.
EnsureCreated
creates a database with the new schema.
This workflow works well early in development when the schema is rapidly evolving, as long as you don't need to preserve data. The situation is different when data that has been entered into the database needs to be preserved. When that is the case, use migrations.
Later in the tutorial series, you delete the database that was created by EnsureCreated
and use migrations instead. A database that is created by EnsureCreated
can't be updated by using migrations.
Test the app
- Run the app.
- Select the Students link and then Create New.
- Test the Edit, Details, and Delete links.
Seed the database
The EnsureCreated
method creates an empty database. This section adds code that populates the database with test data.
Create Data/DbInitializer.cs with the following code:
[!code-csharpMain]
The code checks if there are any students in the database. If there are no students, it adds test data to the database. It creates the test data in arrays rather than List<T>
collections to optimize performance.
-
In Program.cs, replace the
EnsureCreated
call with aDbInitializer.Initialize
call:// context.Database.EnsureCreated(); DbInitializer.Initialize(context);
Visual Studio
Stop the app if it's running, and run the following command in the Package Manager Console (PMC):
Drop-Database
Visual Studio Code
- Stop the app if it's running, and delete the CU.db file.
-
Restart the app.
-
Select the Students page to see the seeded data.
View the database
Visual Studio
- Open SQL Server Object Explorer (SSOX) from the View menu in Visual Studio.
- In SSOX, select (localdb)\MSSQLLocalDB > Databases > SchoolContext-{GUID}. The database name is generated from the context name you provided earlier plus a dash and a GUID.
- Expand the Tables node.
- Right-click the Student table and click View Data to see the columns created and the rows inserted into the table.
- Right-click the Student table and click View Code to see how the
Student
model maps to theStudent
table schema.
Visual Studio Code
Use your SQLite tool to view the database schema and seeded data. The database file is named CU.db and is located in the project folder.
Asynchronous code
Asynchronous programming is the default mode for ASP.NET Core and EF Core.
A web server has a limited number of threads available, and in high load situations all of the available threads might be in use. When that happens, the server can't process new requests until the threads are freed up. With synchronous code, many threads may be tied up while they aren't actually doing any work because they're waiting for I/O to complete. With asynchronous code, when a process is waiting for I/O to complete, its thread is freed up for the server to use for processing other requests. As a result, asynchronous code enables server resources to be used more efficiently, and the server can handle more traffic without delays.
Asynchronous code does introduce a small amount of overhead at run time. For low traffic situations, the performance hit is negligible, while for high traffic situations, the potential performance improvement is substantial.
In the following code, the async keyword, Task<T>
return value, await
keyword, and ToListAsync
method make the code execute asynchronously.
public async Task OnGetAsync()
{
Students = await _context.Students.ToListAsync();
}
- The
async
keyword tells the compiler to:- Generate callbacks for parts of the method body.
- Create the Task object that's returned.
- The
Task<T>
return type represents ongoing work. - The
await
keyword causes the compiler to split the method into two parts. The first part ends with the operation that's started asynchronously. The second part is put into a callback method that's called when the operation completes. ToListAsync
is the asynchronous version of theToList
extension method.
Some things to be aware of when writing asynchronous code that uses EF Core:
- Only statements that cause queries or commands to be sent to the database are executed asynchronously. That includes
ToListAsync
,SingleOrDefaultAsync
,FirstOrDefaultAsync
, andSaveChangesAsync
. It doesn't include statements that just change anIQueryable
, such asvar students = context.Students.Where(s => s.LastName == "Davolio")
. - An EF Core context isn't thread safe: don't try to do multiple operations in parallel.
- To take advantage of the performance benefits of async code, verify that library packages (such as for paging) use async if they call EF Core methods that send queries to the database.
For more information about asynchronous programming in .NET, see Async Overview and Asynchronous programming with async and await.
Next steps
[!div class="step-by-step"] Next tutorial
::: moniker-end
::: moniker range="< aspnetcore-3.0"
The Contoso University sample web app demonstrates how to create an ASP.NET Core Razor Pages app using Entity Framework (EF) Core.
The sample app is a web site for a fictional Contoso University. It includes functionality such as student admission, course creation, and instructor assignments. This page is the first in a series of tutorials that explain how to build the Contoso University sample app.
Download or view the completed app. Download instructions.
Prerequisites
Visual Studio
Visual Studio Code
Familiarity with Razor Pages. New programmers should complete Get started with Razor Pages before starting this series.
Troubleshooting
If you run into a problem you can't resolve, you can generally find the solution by comparing your code to the completed project. A good way to get help is by posting a question to StackOverflow.com for ASP.NET Core or EF Core.
The Contoso University web app
The app built in these tutorials is a basic university web site.
Users can view and update student, course, and instructor information. Here are a few of the screens created in the tutorial.
The UI style of this site is close to what's generated by the built-in templates. The tutorial focus is on EF Core with Razor Pages, not the UI.
Create the ContosoUniversity Razor Pages web app
Visual Studio
- From the Visual Studio File menu, select New > Project.
- Create a new ASP.NET Core Web Application. Name the project ContosoUniversity. It's important to name the project ContosoUniversity so the namespaces match when code is copy/pasted.
- Select ASP.NET Core 2.1 in the dropdown, and then select Web Application.
For images of the preceding steps, see Create a Razor web app. Run the app.
Visual Studio Code
dotnet new webapp -o ContosoUniversity
cd ContosoUniversity
dotnet run
Set up the site style
A few changes set up the site menu, layout, and home page. Update Pages/Shared/_Layout.cshtml with the following changes:
-
Change each occurrence of "ContosoUniversity" to "Contoso University". There are three occurrences.
-
Add menu entries for Students, Courses, Instructors, and Departments, and delete the Contact menu entry.
The changes are highlighted. (All the markup is not displayed.)
In Pages/Index.cshtml, replace the contents of the file with the following code to replace the text about ASP.NET and MVC with text about this app:
Create the data model
Create entity classes for the Contoso University app. Start with the following three entities:
There's a one-to-many relationship between Student
and Enrollment
entities. There's a one-to-many relationship between Course
and Enrollment
entities. A student can enroll in any number of courses. A course can have any number of students enrolled in it.
In the following sections, a class for each one of these entities is created.
The Student entity
Create a Models folder. In the Models folder, create a class file named Student.cs with the following code:
The ID
property becomes the primary key column of the database (DB) table that corresponds to this class. By default, EF Core interprets a property that's named ID
or classnameID
as the primary key. In classnameID
, classname
is the name of the class. The alternative automatically recognized primary key is StudentID
in the preceding example.
The Enrollments
property is a navigation property. Navigation properties link to other entities that are related to this entity. In this case, the Enrollments
property of a Student entity
holds all of the Enrollment
entities that are related to that Student
. For example, if a Student row in the DB has two related Enrollment rows, the Enrollments
navigation property contains those two Enrollment
entities. A related Enrollment
row is a row that contains that student's primary key value in the StudentID
column. For example, suppose the student with ID=1 has two rows in the Enrollment
table. The Enrollment
table has two rows with StudentID
= 1. StudentID
is a foreign key in the Enrollment
table that specifies the student in the Student
table.
If a navigation property can hold multiple entities, the navigation property must be a list type, such as ICollection<T>
. ICollection<T>
can be specified, or a type such as List<T>
or HashSet<T>
. When ICollection<T>
is used, EF Core creates a HashSet<T>
collection by default. Navigation properties that hold multiple entities come from many-to-many and one-to-many relationships.
The Enrollment entity
In the Models folder, create Enrollment.cs with the following code:
The EnrollmentID
property is the primary key. This entity uses the classnameID
pattern instead of ID
like the Student
entity. Typically developers choose one pattern and use it throughout the data model. In a later tutorial, using ID without classname is shown to make it easier to implement inheritance in the data model.
The Grade
property is an enum
. The question mark after the Grade
type declaration indicates that the Grade
property is nullable. A grade that's null is different from a zero grade -- null means a grade isn't known or hasn't been assigned yet.
The StudentID
property is a foreign key, and the corresponding navigation property is Student
. An Enrollment
entity is associated with one Student
entity, so the property contains a single Student
entity. The Student
entity differs from the Student.Enrollments
navigation property, which contains multiple Enrollment
entities.
The CourseID
property is a foreign key, and the corresponding navigation property is Course
. An Enrollment
entity is associated with one Course
entity.
EF Core interprets a property as a foreign key if it's named <navigation property name><primary key property name>
. For example,StudentID
for the Student
navigation property, since the Student
entity's primary key is ID
. Foreign key properties can also be named <primary key property name>
. For example, CourseID
since the Course
entity's primary key is CourseID
.
The Course entity
In the Models folder, create Course.cs with the following code:
The Enrollments
property is a navigation property. A Course
entity can be related to any number of Enrollment
entities.
The DatabaseGenerated
attribute allows the app to specify the primary key rather than having the DB generate it.
Scaffold the student model
In this section, the student model is scaffolded. That is, the scaffolding tool produces pages for Create, Read, Update, and Delete (CRUD) operations for the student model.
- Build the project.
- Create the Pages/Students folder.
Visual Studio
- In Solution Explorer, right click on the Pages/Students folder > Add > New Scaffolded Item.
- In the Add Scaffold dialog, select Razor Pages using Entity Framework (CRUD) > ADD.
Complete the Add Razor Pages using Entity Framework (CRUD) dialog:
- In the Model class drop-down, select Student (ContosoUniversity.Models).
- In the Data context class row, select the + (plus) sign and change the generated name to ContosoUniversity.Models.SchoolContext.
- In the Data context class drop-down, select ContosoUniversity.Models.SchoolContext
- Select Add.
See Scaffold the movie model if you have a problem with the preceding step.
Visual Studio Code
Run the following commands to scaffold the student model.
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design --version 2.1.0
dotnet tool install --global dotnet-aspnet-codegenerator
dotnet aspnet-codegenerator razorpage -m Student -dc ContosoUniversity.Models.SchoolContext -udl -outDir Pages/Students --referenceScriptLibraries
The scaffold process created and changed the following files:
Files created
- Pages/Students Create, Delete, Details, Edit, Index.
- Data/SchoolContext.cs
File updates
- Startup.cs : Changes to this file are detailed in the next section.
- appsettings.json : The connection string used to connect to a local database is added.
Examine the context registered with dependency injection
ASP.NET Core is built with dependency injection. Services (such as the EF Core DB context) are registered with dependency injection 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.
The scaffolding tool automatically created a DB Context and registered it with the dependency injection container.
Examine the ConfigureServices
method in Startup.cs. The highlighted line was added by the scaffolder:
The name of the connection string is passed in to the context by calling a method on a DbContextOptions object. For local development, the ASP.NET Core configuration system reads the connection string from the appsettings.json file.
Update main
In Program.cs, modify the Main
method to do the following:
- Get a DB context instance from the dependency injection container.
- Call the EnsureCreated.
- Dispose the context when the
EnsureCreated
method completes.
The following code shows the updated Program.cs file.
EnsureCreated
ensures that the database for the context exists. If it exists, no action is taken. If it does not exist, then the database and all its schema are created. EnsureCreated
does not use migrations to create the database. A database that is created with EnsureCreated
cannot be later updated using migrations.
EnsureCreated
is called on app start, which allows the following work flow:
- Delete the DB.
- Change the DB schema (for example, add an
EmailAddress
field). - Run the app.
EnsureCreated
creates a DB with theEmailAddress
column.
EnsureCreated
is convenient early in development when the schema is rapidly evolving. Later in the tutorial the DB is deleted and migrations are used.
Test the app
Run the app and accept the cookie policy. This app doesn't keep personal information. You can read about the cookie policy at EU General Data Protection Regulation (GDPR) support.
- Select the Students link and then Create New.
- Test the Edit, Details, and Delete links.
Examine the SchoolContext DB context
The main class that coordinates EF Core functionality for a given data model is the DB context class. The data context is derived from Microsoft.EntityFrameworkCore.DbContext. The data context specifies which entities are included in the data model. In this project, the class is named SchoolContext
.
Update SchoolContext.cs with the following code:
The highlighted code creates a DbSet<TEntity> property for each entity set. In EF Core terminology:
- An entity set typically corresponds to a DB table.
- An entity corresponds to a row in the table.
DbSet<Enrollment>
and DbSet<Course>
could be omitted. EF Core includes them implicitly because the Student
entity references the Enrollment
entity, and the Enrollment
entity references the Course
entity. For this tutorial, keep DbSet<Enrollment>
and DbSet<Course>
in the SchoolContext
.
SQL Server Express LocalDB
The connection string specifies SQL Server LocalDB. LocalDB is a lightweight version of the SQL Server Express Database Engine and is intended for app development, not production use. LocalDB starts on demand and runs in user mode, so there's no complex configuration. By default, LocalDB creates .mdf DB files in the C:/Users/<user>
directory.
Add code to initialize the DB with test data
EF Core creates an empty DB. In this section, an Initialize
method is written to populate it with test data.
In the Data folder, create a new class file named DbInitializer.cs and add the following code:
Note: The preceding code uses Models
for the namespace (namespace ContosoUniversity.Models
) rather than Data
. Models
is consistent with the scaffolder-generated code. For more information, see this GitHub scaffolding issue.
The code checks if there are any students in the DB. If there are no students in the DB, the DB is initialized with test data. It loads test data into arrays rather than List<T>
collections to optimize performance.
The EnsureCreated
method automatically creates the DB for the DB context. If the DB exists, EnsureCreated
returns without modifying the DB.
In Program.cs, modify the Main
method to call Initialize
:
Visual Studio
Stop the app if it's running, and run the following command in the Package Manager Console (PMC):
Drop-Database
Visual Studio Code
- Stop the app if it's running, and delete the CU.db file.
View the DB
The database name is generated from the context name you provided earlier plus a dash and a GUID. Thus, the database name will be "SchoolContext-{GUID}". The GUID will be different for each user. Open SQL Server Object Explorer (SSOX) from the View menu in Visual Studio. In SSOX, click (localdb)\MSSQLLocalDB > Databases > SchoolContext-{GUID}.
Expand the Tables node.
Right-click the Student table and click View Data to see the columns created and the rows inserted into the table.
Asynchronous code
Asynchronous programming is the default mode for ASP.NET Core and EF Core.
A web server has a limited number of threads available, and in high load situations all of the available threads might be in use. When that happens, the server can't process new requests until the threads are freed up. With synchronous code, many threads may be tied up while they aren't actually doing any work because they're waiting for I/O to complete. With asynchronous code, when a process is waiting for I/O to complete, its thread is freed up for the server to use for processing other requests. As a result, asynchronous code enables server resources to be used more efficiently, and the server is enabled to handle more traffic without delays.
Asynchronous code does introduce a small amount of overhead at run time. For low traffic situations, the performance hit is negligible, while for high traffic situations, the potential performance improvement is substantial.
In the following code, the async keyword, Task<T>
return value, await
keyword, and ToListAsync
method make the code execute asynchronously.
-
The
async
keyword tells the compiler to:- Generate callbacks for parts of the method body.
- Automatically create the Task object that's returned. For more information, see Task Return Type.
-
The implicit return type
Task
represents ongoing work. -
The
await
keyword causes the compiler to split the method into two parts. The first part ends with the operation that's started asynchronously. The second part is put into a callback method that's called when the operation completes. -
ToListAsync
is the asynchronous version of theToList
extension method.
Some things to be aware of when writing asynchronous code that uses EF Core:
- Only statements that cause queries or commands to be sent to the DB are executed asynchronously. That includes,
ToListAsync
,SingleOrDefaultAsync
,FirstOrDefaultAsync
, andSaveChangesAsync
. It doesn't include statements that just change anIQueryable
, such asvar students = context.Students.Where(s => s.LastName == "Davolio")
. - An EF Core context isn't thread safe: don't try to do multiple operations in parallel.
- To take advantage of the performance benefits of async code, verify that library packages (such as for paging) use async if they call EF Core methods that send queries to the DB.
For more information about asynchronous programming in .NET, see Async Overview and Asynchronous programming with async and await.
In the next tutorial, basic CRUD (create, read, update, delete) operations are examined.
Additional resources
[!div class="step-by-step"] Next
::: moniker-end