In this article I will explain a simple tutorial with an example, how to use implement
Database-First Approach using
Entity Framework in ASP.Net Core (.Net Core 10) in Visual Studio 2026.
Database First Approach
In this workflow, the database pre-exists. The ORM "reverse engineers" the schema of the Database to generate classes and Data Context.
Advantage
The DBA has full control over the Application and all changes go from Database to the Code.
Disadvantage
One should be highly skilled in Database in order to move ahead with this approach as the application development highly requires Database skills in order to make changes directly in the Database and then in code.
When it should be used
If you are skilled in Database and/or Database already exists, then Database-First approach is significantly helpful.
Requirements
In order to move ahead you will need the following software.
Visual Studio 2026
SQL Server 2022
Expertise
Knowledge of SQL Server and mid-level understanding of Tables and SQL Queries.
So, let’s start with the Database-First approach.
Installing Microsoft.EntityFrameworkCore.SqlServer package using Nuget
In order to install
Microsoft.EntityFrameworkCore.SqlServer library using
Nuget, please refer my article:
Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
Model
1. Once the package is successfully installed, create a Folder named Models in your project and then a new class by right clicking the Models folder and then click on Add and then New Item option of the Context Menu.
2. Create the following properties inside the Model class as shown below.
Note: In this article, only four Columns will be displayed and hence four properties are added to the class.
public class Customer
{
public string CustomerID { get;set; }
public string ContactName { get;set; }
public string City { get;set; }
public string Country { get;set; }
}
Database Context
1. Now you will need to add a new class to your project by right clicking the Solution Explorer and then click on Add and then New Item option of the Context Menu.
2. Inside the class, first inherit the EntityFrameworkCore namespace and then inherit the DbContext class.
Then using Dependency Injection, a Constructor is created DbContextOptions are passed as parameter and also the Constructor of base class i.e. DbContext class is inherited.
Finally, a
DbSet Collection property of Customer Model class is created, which will be later used for holding the Data fetched from
SQL Server database.
using EF_Core_8_MVC.Models;
using Microsoft.EntityFrameworkCore;
namespace EF_Core_8_MVC
{
public class DBCtx: DbContext
{
public DBCtx (DbContextOptions<DBCtx> options) : base (options)
{
}
public DbSet<Customer> Customers { get;set; }
}
}
Adding the Connection String inside AppSettings.json
The following Connection String setting has been added in the AppSettings.json file.
{
"ConnectionStrings": {
"MyConn":"Data Source=.\\SQL2022;Initial Catalog=Northwind;Integrated Security=True"
}
}
Configuring Database Context in Program.cs
Inside the Program.cs file, the Connection String is read from the AppSettings.json file and is used to add the DbContext service to the WebApplicationBuilder class.
using EF_Core_8_MVC;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
//Enabling MVC
builder.Services.AddControllersWithViews();
//Read the Connection String
string? conStr = builder.Configuration.GetConnectionString("MyConn");
//Add the DbConext
builder.Services.AddDbContext<DBCtx>(options => options.UseSqlServer(conStr));
var app = builder.Build();
//Configuring Routes
app.MapControllerRoute(
name:"default",
pattern:"{controller=Home}/{action=Index}/{id?}");
app.Run();
Controller
The Controller consists of following Action method.
Action method for handling GET operation
Inside this Action method, the Top 10 records are fetched from the
Customers Table of the
Northwind Database using
Entity Framework and returned to the View.
public class HomeController: Controller
{
private DBCtxContext { get; }
public HomeController(DBCtx_context)
{
this.Context = _context;
}
public IActionResultIndex()
{
List<Customer> customers = (from customer in this.Context.Customers.Take(10)
select customer).ToList();
return View(customers);
}
}
View
HTML Markup
Inside the View, in the very first line the Customer Entity is declared as IEnumerable which specifies that it will be available as a Collection.
For displaying the records, an HTML Table is used. A FOR EACH loop will be executed over the Model which will generate the HTML Table rows with the Customer records.
@using EF_Core_8_MVC.Models;
@model IEnumerable<Customer>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<h4>Customers</h4>
<hr />
<table cellpadding="0" cellspacing="0">
<tr>
<th>Customer Id</th>
<th>ContactName</th>
<th>City</th>
<th>Country</th>
</tr>
@foreach (Customer customer in Model)
{
<tr>
<td>@customer.CustomerID</td>
<td>@customer.ContactName</td>
<td>@customer.City</td>
<td>@customer.Country</td>
</tr>
}
</table>
</body>
</html>
Screenshot
Downloads