In this approach, we create database first then model our entities either manually or using scaffolding. This approach is useful when we work with an existing databases.
Razor Pages can make coding page-focused scenarios easier and more productive than using controllers and views.
Language : C#
.Net Version : >=6.0
- Microsoft.AspNetCore.App>=6.0
- Microsoft.NETCore.App>=6.0
- Microsoft.EntityFrameworkCore.Tools>=6.0
- Microsoft.EntityFrameworkCore.SqlServer>=6.0
- Microsoft.VisualStudio.Web.CodeGeneration.Design>=6.0
- Visual Studio IDE
- Microsoft SQL Server
- Azure Data Studio / SQL Server Management Studio (SSMS) / SSDT for Visual Studio
- Execute
efcore-dbfirst-approach.sql
script fromScripts
folder onSQL Server
to setupDatabase
with sample data. - Get connection string from SQL Database and update in
appsettings.json
.Integrated Security = True
for Windows Authentication.User Id={Username}; Password={Password}
for Standard Security.
"ConnectionStrings": { "DefaultConnection": "Server={ServerName};Database={DatabaseName};Integrated Security=True" }
- Run
DotNet.EFCore.DatabaseFirst.RazorApp
project. CheckCRUD (Create-Read-Update-Delete)
operations against database usingEntity Framework Core
.
- Skeleton
Code Structure
as per requirements. - Add dependent
NuGet Packages
.Microsoft.EntityFrameworkCore.Tools
Microsoft.EntityFrameworkCore.SqlServer
- Have existing database ready and get connection string.
- Update SQL Connection String in
appsettings.json
.Integrated Security = True
for Windows Authentication.User Id={Username}; Password={Password}
for Standard Security.
"ConnectionStrings": { "DefaultConnection": "{SQL Connection String}" }
- Launch
Package Manager Console
. Choose project with EF NuGet Packages Installed (E.g.,DotNet.EFCore.DatabaseFirst.RazorApp
) asDefault
andStartup
Project.Visual Studio IDE --> Tools --> NuGet Package Manager --> Package Manager Console
- Scaffold Database Context using
Scaffold-DbContext
command. This will createModels
,DbContext
in specified output directory as per existingdatabase schema
.Scaffold-DbContext "{SQL Connection String}" Microsoft.EntityFrameworkCore.SqlServer -OutputDir {Folder Name}
[OPTIONAL]
MoveContext
file to separate directory if required. If moved, update accordingnamespace
references in dependent files.- Update
OnConfiguring
method in DbContext for getting connection string from JSON config file or some secure vault like Azure Key Vault etc. - Register DbContext in Application Builder Services Collection before performing Application Build either in
Program.cs
orStartup.cs
.builder.Services.AddDbContext<{Db Context Name}>();
- Now
DbContext
can be injected inPage .cs file (page.cshml.cs)
and can be used to access database. - Create
Razor Page
underPages
with folder organization with eitherRazor Page with Entity Framework Core (CRUD)
orRazor Page Empty
. - [OPTIONAL] Implement either
Repository/Unit of Work(UoW)/Factory Pattern etc
., to separte operations based on entity using injectedDbContext
. - Pass transactional data to
Pages
usingModels
. - Run
DotNet.EFCore.DatabaseFirst.RazorApp
project. CheckCRUD (Create-Read-Update-Delete)
operations against database usingEntity Framework Core
.
EntityFrameworkCore
.Net Core Razor Pages
EF Core - Razor Pages
EFCore - Get Started
EFCore - Model from DB