ASP.NET Core Tutorial for Beginners
ASP.NET Core Fundamentals
Learn how to create app in ASP.NET Core using Visual Studio and the C# language.
[P16] : Eager Loading, Lazy Loading And Explicit Loading In Entity Framework
We will discussing about Eager Loading, Lazy Loading and Explicit Loading in an Entity Framework. All three terms -- Eager Loading, Lazy Loading and Explicit Loading -- refer to the process of loading the related entities. They define when to load the related entities or child entities.
Eager Loading helps you to load all your needed entities at once
Lazy Loading
It is the default behavior of an Entity Framework, where a child entity is loaded only when it is accessed for the first time.
Explicit Loading
There are options to disable Lazy Loading in an Entity Framework. After turning Lazy Loading off, you can still load the entities by explicitly calling the Load method for the related entities.
Package : EntityFrameworkCore.Proxies
Install-Package Microsoft.EntityFrameworkCore.Proxies
Script to create tables
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Mission](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Title] [nchar](300) NULL,
[DateCreated] [smalldatetime] NULL,
[Priority] [int] NULL,
CONSTRAINT [PK_Task] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[MissionDetail](
[ID] [int] IDENTITY(1,1) NOT NULL,
[MissionID] [int] NOT NULL,
[Description] [nvarchar](max) NULL,
[Dateadded] [datetime] NULL,
[Priority] [int] NULL,
CONSTRAINT [PK_MissionDetail] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[MissionDetail] WITH CHECK ADD CONSTRAINT [FK_MissionDetail_Mission] FOREIGN KEY([MissionID])
REFERENCES [dbo].[Mission] ([ID])
GO
ALTER TABLE [dbo].[MissionDetail] CHECK CONSTRAINT [FK_MissionDetail_Mission]
GO