Skip to content

Virtual keyword in EF Core: .NET 10, ILazyLoader and explicit loading samples - #2266

Open
vladimir-pecanac-main wants to merge 2 commits into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/109993-efcore-why-do-we-use-the-virtual-keyword-for-class-properties
Open

vladimir-pecanac-main wants to merge 2 commits into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/109993-efcore-why-do-we-use-the-virtual-keyword-for-class-properties

Conversation

@vladimir-pecanac-main

@vladimir-pecanac-main vladimir-pecanac-main commented Oct 4, 2026 •

Copy link
Copy Markdown
Collaborator

Sample update for the article "Why EF Core Lazy Loading Needs Virtual Navigation Properties" (https://code-maze.com/efcore-why-do-we-use-the-virtual-keyword-for-class-properties/).

Folder: dotnet-efcore/VirtualKeywordInEFCore only.

  • Both projects retargeted from net8.0 to net10.0.
  • Microsoft.EntityFrameworkCore, .Proxies and .Sqlite 8.0.2 to 10.0.12; Microsoft.EntityFrameworkCore.Abstractions 10.0.12 added explicitly for ILazyLoader.
  • Tests: Microsoft.NET.Test.Sdk 18.10.1, xunit 2.9.3, xunit.runner.visualstudio 4.0.0, coverlet.collector 10.1.0 (newest stable on NuGet, queried 2026-10-04).
  • Removed the dead null check after First() in LoadData(); LoadDataWithLazyLoading() keeps its null check and ?. uses unchanged, so the article's debugger screenshot (Program.cs lines 36 to 57) still matches. Book.Author and BookLazy.Author initialised with null! (1 build warning now, the pre-existing CS8602 in LoadDataWithLazyLoading(); 3 before).
  • Added AuthorInjected, BookInjected and DataContextInjected: lazy loading through an injected ILazyLoader with no virtual members and no proxy type.
  • Added an explicit loading demo with Entry(...).Collection(...).Load().
  • Two new tests: the injected entity lazy-loads its books while staying AuthorInjected, and explicit loading fills an empty collection.

Local run: SDK 10.0.302, runtime 10.0.10. Build 1 warning (CS8602, pre-existing), 0 errors. dotnet test: Passed 5, Failed 0. No vulnerable packages (direct or transitive).

… samples

Retarget both projects to net10.0 and EF Core 10.0.12, bump the test packages,
remove the two dead null checks after First(), fix the three nullable warnings,
and add an ILazyLoader entity (no virtual members) plus an explicit loading call,
each covered by a new test.
…ading

Restore the original if (authorLazy is not null) block and the ?. uses so the
article's debugger screenshot matches lines 36 to 57 of Program.cs again.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant