Back to Aspnetcore

Visual Studio

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

latest5.2 KB
Original Source

:::moniker range="= aspnetcore-7.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:

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 (dbo.Movie) > 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=snippet_"si"></a>

Add the seed initializer

Visual Studio

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

[!code-csharp]

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

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.

Visual Studio Code / Visual Studio for Mac

Update Program.cs with the following highlighted code:

[!code-csharp]

Delete all the records in the database.

Test the app. Stop it and restart it 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