ASP.NET Core – Existing Database

Prerequisites

The following prerequisites are needed to complete this walkthrough:

1

Blogging database

This tutorial uses a Blogging database on your LocalDb instance as the existing database.

Note

If you have already created the Blogging database as part of another tutorial, you can skip these steps.

  • Open Visual Studio
  • Tools -> Connect to Database…
  • Select Microsoft SQL Server and click Continue
  • Enter (localdb)\mssqllocaldb as the Server Name
  • Enter master as the Database Name and click OK
  • The master database is now displayed under Data Connections in Server Explorer
  • Right-click on the database in Server Explorer and select New Query
  • Copy the script, listed below, into the query editor
  • Right-click on the query editor and select Execute
Copy
SQL

Create a new project

  • Open Visual Studio 2017
  • File -> New -> Project…
  • From the left menu select Installed -> Templates -> Visual C# -> Web
  • Select the ASP.NET Core Web Application (.NET Core) project template
  • Enter EFGetStarted.AspNetCore.ExistingDb as the name and click OK
  • Wait for the New ASP.NET Core Web Application dialog to appear
  • Under ASP.NET Core Templates 1.1 select the Web Application
  • Ensure that Authentication is set to No Authentication
  • Click OK

Install Entity Framework

To use EF Core, install the package for the database provider(s) you want to target. This walkthrough uses SQL Server. For a list of available providers see Database Providers.1

    • Tools -> NuGet Package Manager -> Package Manager Console
    • Run Install-Package Microsoft.EntityFrameworkCore.SqlServer

1

We will be using some Entity Framework Tools to create a model from the database. So we will install the tools package as well.

  • Run Install-Package Microsoft.EntityFrameworkCore.Tools
  • Run Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design

Reverse engineer your model

Now it’s time to create the EF model based on your existing database.

    • Tools –> NuGet Package Manager –> Package Manager Console
    • Run the following command to create a model from the existing database. If you receive an error stating The term 'Scaffold-DbContext' is not recognized as the name of a cmdlet, then close and reopen Visual Studio.

14

Copy
Code

The reverse engineer process created entity classes (Blog.cs & Post.cs) and a derived context (BloggingContext.cs) based on the schema of the existing database.

The entity classes are simple C# objects that represent the data you will be querying and saving.

Copy
C#

The context represents a session with the database and allows you to query and save instances of the entity classes.

Copy
c#

Register your context with dependency injection

The concept of dependency injection is central to ASP.NET Core. Services (such as BloggingContext) are registered with dependency injection during application startup. Components that require these services (such as your MVC controllers) are then provided these services via constructor parameters or properties. For more information on dependency injection see the Dependency Injection article on the ASP.NET site.

Remove inline context configuration

In ASP.NET Core, configuration is generally performed in Startup.cs. To conform to this pattern, we will move configuration of the database provider to Startup.cs.

  • Open Models\BloggingContext.cs
  • Delete the OnConfiguring(...) method
Copy
c#

  • Add the following constructor, which will allow configuration to be passed into the context by dependency injection
Copy
C#

Register and configure your context in Startup.cs

In order for our MVC controllers to make use of BloggingContext we are going to register it as a service.

  • Open Startup.cs
  • Add the following using statements at the start of the file
Copy
C#

Now we can use the AddDbContext(...) method to register it as a service.

  • Locate the ConfigureServices(...) method
  • Add the following code to register the context as a service
Copy
C#

Note

In a real application you would typically put the connection string in a configuration file. For the sake of simplicity, we are defining it in code. For more information, see Connection Strings.

Create a controller

Next, we’ll enable scaffolding in our project.

  • Right-click on the Controllers folder in Solution Explorer and select Add -> Controller…
  • Select Full Dependencies and click Add
  • You can ignore the instructions in the ScaffoldingReadMe.txt file that opens

Now that scaffolding is enabled, we can scaffold a controller for the Blog entity.

    • Right-click on the Controllers folder in Solution Explorer and select Add -> Controller…
    • Select MVC Controller with views, using Entity Framework and click Ok
    • Set Model class to Blog and Data context class to BloggingContext
    • Click Add

1

Run the application

You can now run the application to see it in action.

  • Debug -> Start Without Debugging
  • The application will build and open in a web browser
  • Navigate to /Blogs
  • Click Create New
  • Enter a Url for the new blog and click Create

image

image