Skip to content

Latest commit

 

History

History
157 lines (92 loc) · 6.23 KB

File metadata and controls

157 lines (92 loc) · 6.23 KB
title Part 5, work with a database in an ASP.NET Core MVC app
ai-usage ai-assisted
author wadepickett
description Part 5 of tutorial series on ASP.NET Core MVC.
monikerRange >= aspnetcore-3.1
ms.author wpickett
ms.custom sfi-ropc-nochange
ms.date 04/03/2026
uid tutorials/first-mvc-app/working-with-sql

Part 5, work with a database in an ASP.NET Core MVC app

[!INCLUDE]

:::moniker range=">= aspnetcore-10.0"

Introduction

This part of the tutorial series focuses on working with a SQL database in your ASP.NET Core MVC application.

You’ll learn how to:

  • Register and configure the Entity Framework Core database context for your ASP.NET Core MVC app.
  • Work with database connection strings for local development.
  • Use SQL Server Express LocalDB for development and examine your database and data using SQL Server Object Explorer.
  • Seed your database with initial sample data.

Prerequisite

This tutorial uses a database you set up in the previous step: xref:tutorials/first-mvc-app/adding-model.

Working with the database context

The MvcMovieContext object handles the task of connecting to the database and mapping Movie objects to database records. The database context is registered with the Dependency Injection container in the Program.cs file:

[!code-csharp]

The ASP.NET Core Configuration system reads the ConnectionString key. For local development, it gets the connection string from the appsettings.json file:

[!code-json]

[!code-csharp]

The ASP.NET Core Configuration system reads the ConnectionString. For local development, it gets the connection string from the appsettings.json file:

[!code-json]


[!INCLUDE managed-identities]

SQL Server Express LocalDB

LocalDB:

  • Is a lightweight version of the SQL Server Express Database Engine, installed by default with Visual Studio.
  • Starts on demand by using a connection string.
  • Is targeted for program development. It runs in user mode, so there's no complex configuration.
  • By default creates .mdf files in the C:/Users/{user} directory.

Examine the database

From the View menu, open SQL Server Object Explorer (SSOX).

Right-click on the Movie table (dbo.Movie) > View Designer

:::image type="content" source="~/tutorials/first-mvc-app/working-with-sql/media/view-designer.png" alt-text="Right-click on the Movie table > View Designer.":::

:::image type="content" source="~/tutorials/first-mvc-app/working-with-sql/media/designer-view.png" alt-text="Movie table open in Designer.":::

Note the key icon next to ID. By default, EF makes a property named ID the primary key.

Right-click on the Movie table > View Data

:::image type="content" source="~/tutorials/first-mvc-app/working-with-sql/media/view-data.png" alt-text="Right-click on the Movie table > View Data.":::

:::image type="content" source="~/tutorials/first-mvc-app/working-with-sql/media/empty-movie-data.png" alt-text="Movie table open showing no data."::: -->

[!INCLUDE] [!INCLUDE]


Seed the database

Create a new class named SeedData in the Models folder. Replace the generated code with the following:

[!code-csharp]

If there are any movies in the database, the seed initializer returns and no movies are added.

if (context.Movie.Any())
{
    return;  // DB has been seeded.
}

Add the seed initializer

Replace the contents of Program.cs with the following code. The new code is highlighted.

[!code-csharp]

Test the app. Force the app to initialize, calling the code in the Program.cs file, so the seed method runs. To force initialization, close the command prompt window that Visual Studio opened, and restart by pressing Ctrl+F5.

Update Program.cs with the following highlighted code:

[!code-csharp]

Test the app. Stop it and restart it so the SeedData.Initialize method runs and seeds the database.


The app shows the seeded data.

:::image type="content" source="~/tutorials/first-mvc-app/working-with-sql/media/seeded-movies.png" alt-text="MVC Movie app open in Microsoft Edge showing movie data.":::

And a refresh of the Movie table shows the same data.

:::image type="content" source="~/tutorials/first-mvc-app/working-with-sql/media/seeded-movie-data.png" alt-text="Movie table has seeded data.":::

[!INCLUDE]

[!div class="step-by-step"] Previous: Adding a model Next: Adding controller methods and views

:::moniker-end

[!INCLUDE]

[!INCLUDE]

[!INCLUDE]

[!INCLUDE]

[!INCLUDE]