Back to Aspnetcore

Visual Studio

aspnetcore/tutorials/first-mvc-app/working-with-sql/includes/working-with-sql3-5.md

latest5.4 KB
Original Source

:::moniker range=">= aspnetcore-3.1 < aspnetcore-6.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 ConfigureServices method in the Startup.cs file:

Visual Studio

[!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]

Visual Studio Code / Visual Studio for Mac

[!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]

Visual Studio

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 > View 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

Visual Studio Code / Visual Studio for Mac

[!INCLUDE] [!INCLUDE]


<!-- End of VS tabs -->

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.

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

<a name="si"></a>

Add the seed initializer

Replace the contents of Program.cs with the following code:

[!code-csharp]

Test the app.

Visual Studio

Delete all the records in the database. You can do this with the delete links in the browser or from SSOX.

Force the app to initialize, calling the methods in the Startup class, so the seed method runs. To force initialization, IIS Express must be stopped and restarted. You can do this with any of the following approaches:

  • Right-click the IIS Express system tray icon in the notification area and tap Exit or Stop Site:

    • If you were running VS in non-debug mode, press F5 to run in debug mode
    • If you were running VS in debug mode, stop the debugger and press F5

Visual Studio Code / Visual Studio for Mac

Delete all the records in the database. Stop and start the app so the SeedData.Initialize method runs and seeds the database.


The app shows the seeded data.

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

:::moniker-end