SlideShare a Scribd company logo
ASP.NET Core MVC
with
EF Core Code-First
About Me
Md. Aftab Uddin Kajal
Software Engineer
SELISE rockin' software
linkedin: @iamkajal || github: @iamkajal || kajal.alpha@gmail.com
Agenda
● Get started
● Create, Read, Update, and Delete operations
● Sorting, filtering
● Migrations
● Create a complex data model
● Reading related data
● Updating related data
● Implementing Repository Pattern
● Implementing Clean Architecture
EMS web app
Get started with EF Core in an ASP.NET MVC web app
This lesson teaches ASP.NET Core MVC and Entity Framework Core with controllers and views.
The EMS sample web application demonstrates how to create ASP.NET Core 2.2 MVC web
applications using Entity Framework (EF) Core 2.0 and Visual Studio 2017.
The sample application is a web site for a EMS. It includes functionality such as employee add,
salary creation, and task assignments.
In this lesson, you:
● Create ASP.NET Core MVC web app
● Set up the site style
● Learn about EF Core NuGet packages
● Create the data model
● Create the database context
● Register the SchoolContext
● Initialize DB with test data
● Create controller and views
● View the database
Create ASP.NET Core MVC web app
Open Visual Studio and create a new ASP.NET Core C# web project named
"EmployeeManagementSystem".
1. From the File menu, select New > Project.
2. From the left pane, select Installed > Visual C# > Web.
3. Select the ASP.NET Core Web Application project template.
4. Enter EmployeeManagementSystem as the name and click OK.
● Wait for the New ASP.NET Core Web
Application (.NET Core) dialog to
appear
● Select ASP.NET Core 2.2 and the
Web Application
(Model-View-Controller) template.
● Note: This tutorial requires ASP.NET
Core 2.2 and EF Core 2.0 or later.
● Make sure Authentication is set to No
Authentication and Click OK
Set up the site style
A few simple changes will set up the site menu, layout, and home page.
Open Views/Shared/_Layout.cshtml and make the following changes:
● Change each occurrence of "EmployeeManagementSystem" to "EMS". There are three
occurrences.
● Add menu entries for About, Employees, Salary Sheet, Task, and Departments, and delete the
Privacy menu entry.
Coding
Create the
BaseEntity
In the Models folder, create
a folder named
“SharedKernel” and create
file named BaseEntity.cs
and replace the template
code with the following
code.
public abstract class BaseEntity
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
}
Create the data
model
In the Models folder, create
a class file named
Emplyee.cs and replace the
template code with the
following code.
public class Employee : BaseEntity
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Designation { get; set; }
public string Gender { get; set; }
public int DepartmentId { get; set; }
public Department Department { get; set; }
public double Salary { get; set; }
[DataType(DataType.Date)]
public DateTime? DateOfBirth { get; set; }
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
public string PhoneNumber { get; set; }
public string ShortCode { get; set; }
public string ImageUrl { get; set; }
}
Create the data
model
In the Models folder, create
a class file named
Department.cs and replace
the template code with the
following code.
public class Department : BaseEntity
{
public string Name { get; set; }
public List<Employee> Employees { get; set; }
}
Create the
database context
Create this class by deriving from the
Microsoft.EntityFrameworkCore.DbContext
public class EmployeeDbContext : DbContext
{
Public EmployeeDbContext(
DbContextOptions<EmployeeDbContext> options) : base(options)
{ }
public DbSet<Department> Departments { get; set; }
public DbSet<Employee> Employees { get; set; }
}
Register the EmployeeDbContext
Coding
Add Employee Controller
Coding
Implement Index action method in Employee
Controller
Coding
Implement Index View for Index method
Coding
Implement Create, Edit, details and delete
action method and View
Coding
Implement sorting, searching
Repository Pattern
The repository pattern is mediator
between database and application
business logic. repository pattern have
two purposes; first it is an abstraction
of the data layer and second it is a way
of centralising the handling of the
domain objects.
Repository
Pattern
Implementation
Coding
ASP.NET Core MVC with EF Core code first
Resources
● ASP.NET Core
● ASP.NET Core in MVA
● Patterns In Model-View-Controller Architectures
● Principles Take pride in your code.
●
ASP.NET Core MVC with EF Core code first

More Related Content

PPTX
ASP.NET Core MVC + Web API with Overview
PPTX
ASP.NET Web API
PPT
Asp.net.
PPT
ADO.NET
PDF
Asp.Net Core MVC , Razor page , Entity Framework Core
PPTX
Asp.Net Core MVC with Entity Framework
PDF
Lecture 3: Servlets - Session Management
PPTX
ASP.NET Core MVC + Web API with Overview
ASP.NET Core MVC + Web API with Overview
ASP.NET Web API
Asp.net.
ADO.NET
Asp.Net Core MVC , Razor page , Entity Framework Core
Asp.Net Core MVC with Entity Framework
Lecture 3: Servlets - Session Management
ASP.NET Core MVC + Web API with Overview

What's hot (20)

PPTX
PPT
Jsp ppt
PDF
MVC in PHP
PPTX
Web forms in ASP.net
PPTX
Build web apps with react js
PDF
Introdução ao MySQL
PPTX
Introduction to Django
PPTX
ASP.NET MVC 3.0 Validation
PDF
Servlet and servlet life cycle
PPTX
Model view controller (mvc)
PPTX
Spring Framework
PPTX
Spring ioc
PPTX
Introduction to .NET Core
PPTX
Asp.NET Validation controls
PPTX
PPTX
Android Services
PDF
Angular - Chapter 3 - Components
PPTX
Ef code first
PPT
Introduction to visual basic programming
Jsp ppt
MVC in PHP
Web forms in ASP.net
Build web apps with react js
Introdução ao MySQL
Introduction to Django
ASP.NET MVC 3.0 Validation
Servlet and servlet life cycle
Model view controller (mvc)
Spring Framework
Spring ioc
Introduction to .NET Core
Asp.NET Validation controls
Android Services
Angular - Chapter 3 - Components
Ef code first
Introduction to visual basic programming
Ad

Similar to ASP.NET Core MVC with EF Core code first (20)

DOCX
Learning MVC Part 3 Creating MVC Application with EntityFramework
PDF
Getting started with the entity framework 4.1 using asp.net mvc
PPTX
Real World MVC
DOCX
MVC Application using EntityFramework Code-First approach Part4
PDF
[FREE PDF sample] Programming Entity Framework DbContext 1st Edition Julia Le...
PDF
Getting started with entity framework 6 code first using mvc 5
DOCX
ASP.NET MVC3 RAD
PDF
Entity Framework Core Cookbook 2nd Edition Ricardo Peres
PPTX
MVC and Entity Framework 4
PDF
767458168-Introduction-to-Web-API767458168-Introduction-to-Web-API.pdf.pdf
PPTX
Entity Framework: Code First and Magic Unicorns
PPTX
MVC and Entity Framework
PDF
Enterprise Level Application Architecture with Web APIs using Entity Framewor...
PPTX
dotNet Miami - June 21, 2012: Richie Rump: Entity Framework: Code First and M...
PPTX
MVC and Entity Framework 4
PPTX
PPTX
Entity Framework Database and Code First
PDF
Entity frame work by Salman Mushtaq -1-
PDF
Angular 12 CRUD Example with Web API
PPT
Overview of CSharp MVC3 and EF4
Learning MVC Part 3 Creating MVC Application with EntityFramework
Getting started with the entity framework 4.1 using asp.net mvc
Real World MVC
MVC Application using EntityFramework Code-First approach Part4
[FREE PDF sample] Programming Entity Framework DbContext 1st Edition Julia Le...
Getting started with entity framework 6 code first using mvc 5
ASP.NET MVC3 RAD
Entity Framework Core Cookbook 2nd Edition Ricardo Peres
MVC and Entity Framework 4
767458168-Introduction-to-Web-API767458168-Introduction-to-Web-API.pdf.pdf
Entity Framework: Code First and Magic Unicorns
MVC and Entity Framework
Enterprise Level Application Architecture with Web APIs using Entity Framewor...
dotNet Miami - June 21, 2012: Richie Rump: Entity Framework: Code First and M...
MVC and Entity Framework 4
Entity Framework Database and Code First
Entity frame work by Salman Mushtaq -1-
Angular 12 CRUD Example with Web API
Overview of CSharp MVC3 and EF4
Ad

Recently uploaded (20)

PPTX
Big Data Technologies - Introduction.pptx
PPTX
Spectroscopy.pptx food analysis technology
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
KodekX | Application Modernization Development
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Cloud computing and distributed systems.
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
cuic standard and advanced reporting.pdf
Big Data Technologies - Introduction.pptx
Spectroscopy.pptx food analysis technology
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
MYSQL Presentation for SQL database connectivity
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
KodekX | Application Modernization Development
“AI and Expert System Decision Support & Business Intelligence Systems”
Network Security Unit 5.pdf for BCA BBA.
Diabetes mellitus diagnosis method based random forest with bat algorithm
Review of recent advances in non-invasive hemoglobin estimation
Reach Out and Touch Someone: Haptics and Empathic Computing
Cloud computing and distributed systems.
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Chapter 3 Spatial Domain Image Processing.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Understanding_Digital_Forensics_Presentation.pptx
cuic standard and advanced reporting.pdf

ASP.NET Core MVC with EF Core code first

  • 1. ASP.NET Core MVC with EF Core Code-First
  • 2. About Me Md. Aftab Uddin Kajal Software Engineer SELISE rockin' software linkedin: @iamkajal || github: @iamkajal || kajal.alpha@gmail.com
  • 3. Agenda ● Get started ● Create, Read, Update, and Delete operations ● Sorting, filtering ● Migrations ● Create a complex data model ● Reading related data ● Updating related data ● Implementing Repository Pattern ● Implementing Clean Architecture
  • 5. Get started with EF Core in an ASP.NET MVC web app This lesson teaches ASP.NET Core MVC and Entity Framework Core with controllers and views. The EMS sample web application demonstrates how to create ASP.NET Core 2.2 MVC web applications using Entity Framework (EF) Core 2.0 and Visual Studio 2017. The sample application is a web site for a EMS. It includes functionality such as employee add, salary creation, and task assignments. In this lesson, you: ● Create ASP.NET Core MVC web app ● Set up the site style ● Learn about EF Core NuGet packages ● Create the data model ● Create the database context ● Register the SchoolContext ● Initialize DB with test data ● Create controller and views ● View the database
  • 6. Create ASP.NET Core MVC web app Open Visual Studio and create a new ASP.NET Core C# web project named "EmployeeManagementSystem". 1. From the File menu, select New > Project. 2. From the left pane, select Installed > Visual C# > Web. 3. Select the ASP.NET Core Web Application project template. 4. Enter EmployeeManagementSystem as the name and click OK.
  • 7. ● Wait for the New ASP.NET Core Web Application (.NET Core) dialog to appear ● Select ASP.NET Core 2.2 and the Web Application (Model-View-Controller) template. ● Note: This tutorial requires ASP.NET Core 2.2 and EF Core 2.0 or later. ● Make sure Authentication is set to No Authentication and Click OK
  • 8. Set up the site style A few simple changes will set up the site menu, layout, and home page. Open Views/Shared/_Layout.cshtml and make the following changes: ● Change each occurrence of "EmployeeManagementSystem" to "EMS". There are three occurrences. ● Add menu entries for About, Employees, Salary Sheet, Task, and Departments, and delete the Privacy menu entry.
  • 10. Create the BaseEntity In the Models folder, create a folder named “SharedKernel” and create file named BaseEntity.cs and replace the template code with the following code. public abstract class BaseEntity { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } }
  • 11. Create the data model In the Models folder, create a class file named Emplyee.cs and replace the template code with the following code. public class Employee : BaseEntity { public string FirstName { get; set; } public string LastName { get; set; } public string Designation { get; set; } public string Gender { get; set; } public int DepartmentId { get; set; } public Department Department { get; set; } public double Salary { get; set; } [DataType(DataType.Date)] public DateTime? DateOfBirth { get; set; } [DataType(DataType.EmailAddress)] public string Email { get; set; } public string PhoneNumber { get; set; } public string ShortCode { get; set; } public string ImageUrl { get; set; } }
  • 12. Create the data model In the Models folder, create a class file named Department.cs and replace the template code with the following code. public class Department : BaseEntity { public string Name { get; set; } public List<Employee> Employees { get; set; } }
  • 13. Create the database context Create this class by deriving from the Microsoft.EntityFrameworkCore.DbContext public class EmployeeDbContext : DbContext { Public EmployeeDbContext( DbContextOptions<EmployeeDbContext> options) : base(options) { } public DbSet<Department> Departments { get; set; } public DbSet<Employee> Employees { get; set; } }
  • 16. Coding Implement Index action method in Employee Controller
  • 17. Coding Implement Index View for Index method
  • 18. Coding Implement Create, Edit, details and delete action method and View
  • 20. Repository Pattern The repository pattern is mediator between database and application business logic. repository pattern have two purposes; first it is an abstraction of the data layer and second it is a way of centralising the handling of the domain objects.
  • 24. Resources ● ASP.NET Core ● ASP.NET Core in MVA ● Patterns In Model-View-Controller Architectures ● Principles Take pride in your code. ●