Database First Approach - Complete Workflow
Step 1
Create SQL Server Database
Step 2
Create Tables
Step 3
Insert Sample Data
Step 4
Create [Link] Core MVC Project
Step 5
Install EF Core Packages
Step 6
Run Scaffold-DbContext
[Link] Generated
[Link] Generated
Step 7
Write Controller
Step 8
Write View
Run Application
Step 1 : Create Database
CREATE DATABASE CompanyDB;
GO
USE CompanyDB;
GO
Step 2 : Create Table
CREATE TABLE Employees
EmployeeId INT PRIMARY KEY IDENTITY(1,1),
Name NVARCHAR(100) NOT NULL,
Department NVARCHAR(50),
Salary DECIMAL(10,2)
);
Step 3 : Insert Data
INSERT INTO Employees(Name,Department,Salary)
VALUES
('Amit','IT',50000),
('Rahul','HR',45000),
('Neha','Finance',65000),
('Priya','Sales',47000);
Step 4 : Create [Link] Core MVC Project
MvcDemo
Step 5 : Install Packages
Install these NuGet packages:
[Link]
[Link]
Step 6 : Run Scaffold-DbContext
Open Package Manager Console.
Run:
Scaffold-DbContext
"Server=.;Database=CompanyDB;Trusted_Connection=True;TrustServerCe
rtificate=True;" [Link] -OutputDir
Models
If you're using SQL Server Express:
Scaffold-DbContext "Server=.\
SQLEXPRESS;Database=CompanyDB;Trusted_Connection=True;TrustServe
rCertificate=True;" [Link] -OutputDir
Models
What EF Core Generates
Inside the Models folder, you'll see:
Models
├── [Link]
└── [Link]
Notice that you did not write these files.
Generated [Link]
EF Core generates code similar to:
public partial class Employee
public int EmployeeId { get; set; }
public string Name { get; set; } = null!;
public string? Department { get; set; }
public decimal? Salary { get; set; }
Generated [Link]
EF Core generates something like:
public partial class CompanyDbContext : DbContext
public CompanyDbContext()
public CompanyDbContext(DbContextOptions<CompanyDbContext>
options)
: base(options)
public virtual DbSet<Employee> Employees { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder
optionsBuilder)
// Generated by EF Core if not using configuration
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
[Link]<Employee>(entity =>
[Link](e => [Link]);
[Link](e => [Link])
.HasMaxLength(100);
[Link](e => [Link])
.HasMaxLength(50);
[Link](e => [Link])
.HasColumnType("decimal(10,2)");
});
Notice that EF Core even generates the Fluent API configuration.
Step 7 : Configure Connection String
[Link]
"ConnectionStrings": {
"DefaultConnection":
"Server=.;Database=CompanyDB;Trusted_Connection=True;TrustServerCe
rtificate=True;"
Step 8 : Modify [Link]
using [Link];
using [Link];
var builder = [Link](args);
[Link]();
[Link]<CompanyDbContext>(options =>
[Link](
[Link]("DefaultConnection")));
var app = [Link]();
[Link]();
[Link]();
[Link](
name: "default",
pattern: "{controller=Employee}/{action=Index}/{id?}");
[Link]();
Step 9 : Create EmployeeController
using [Link];
using [Link];
namespace [Link]
{
public class EmployeeController : Controller
private readonly CompanyDbContext _context;
public EmployeeController(CompanyDbContext context)
_context = context;
public IActionResult Index()
var employees = _context.[Link]();
return View(employees);
Step 10 : Create [Link]
@model IEnumerable<[Link]>
<h2>Employee List</h2>
<table class="table table-bordered">
<thead>
<tr>
<th>Employee Id</th>
<th>Name</th>
<th>Department</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
@foreach (var emp in Model)
<tr>
<td>@[Link]</td>
<td>@[Link]</td>
<td>@[Link]</td>
<td>@[Link]</td>
</tr>
</tbody>
</table>
Output
Employee List
-----------------------------------------
Id Name Department Salary
-----------------------------------------
1 Amit IT 50000
2 Rahul HR 45000
3 Neha Finance 65000
4 Priya Sales 47000