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.sqlscript fromScriptsfolder onSQL Serverto setupDatabasewith sample data. - Get connection string from SQL Database and update in
appsettings.json.Integrated Security = Truefor Windows Authentication.User Id={Username}; Password={Password}for Standard Security.
"ConnectionStrings": { "DefaultConnection": "Server={ServerName};Database={DatabaseName};Integrated Security=True" } - Run
DotNet.EFCore.DatabaseFirst.RazorAppproject. CheckCRUD (Create-Read-Update-Delete)operations against database usingEntity Framework Core.
- Skeleton
Code Structureas per requirements. - Add dependent
NuGet Packages.Microsoft.EntityFrameworkCore.ToolsMicrosoft.EntityFrameworkCore.SqlServer
- Have existing database ready and get connection string.
- Update SQL Connection String in
appsettings.json.Integrated Security = Truefor 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) asDefaultandStartupProject.Visual Studio IDE --> Tools --> NuGet Package Manager --> Package Manager Console - Scaffold Database Context using
Scaffold-DbContextcommand. This will createModels,DbContextin specified output directory as per existingdatabase schema.Scaffold-DbContext "{SQL Connection String}" Microsoft.EntityFrameworkCore.SqlServer -OutputDir {Folder Name} [OPTIONAL]MoveContextfile to separate directory if required. If moved, update accordingnamespacereferences in dependent files.- Update
OnConfiguringmethod 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.csorStartup.cs.builder.Services.AddDbContext<{Db Context Name}>(); - Now
DbContextcan be injected inPage .cs file (page.cshml.cs)and can be used to access database. - Create
Razor PageunderPageswith 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
PagesusingModels. - Run
DotNet.EFCore.DatabaseFirst.RazorAppproject. 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