Posts

Showing posts from September, 2026

Entity Framework Core Pitfalls - Getting a Single Record by Key

Introduction One more for the EF Core pitfalls series . EF Core offers essentially two ways to get a record with a known primary key: The native  Find / FindAsync methods from DbContext  or DbSet<T> A regular Where clause followed by one of First / FirstOrDefault  or Single / SingleOrDefault , both from standard  LINQ extensions , or FirstAsync / FirstOrDefaultAsync , SingleAsync / SingleOrDefaultAsync , the asynchronous alternatives from the EF Core LINQ extensions There are a few differences between these approaches, and also a couple limitations, which I'll cover here. I will use as example a blogging domain model, where a Blog has many Posts , each Post references its Blog , and a Post has many Comments . Like in other tips, this may or may not be a pitfall, but there is a possibility that we are not counting on this. Using Find/FindAsync Methods Find / FindAsync  methods are great, because they allow us to retrieve a single entity wi...

What's in a Task?

Introduction Tasks in .NET have become ubiquitous: by now we all know that in order to make our apps and services more scalable (not necessarily performant, they are different concepts), we need to use them. A task is an operation that is designed to run in the background and which may or may not return a value. Tasks allow complex operations (I/O or CPU-bound) to be executed without blocking the main thread and are implemented by the Task  and  Task<T>  classes. These are actually monads  in mathematical and functional terminology, which means they have certain properties which I won't cover here. There are two ways by which we can call a method that is meant to be processed by another thread (one that returns  Task  or Task<T> , which inherits from  Task  but wraps a result): We use  await  to ask for the callee to be called by another thread and wait for its completion We omit  await  and have the callee processi...

SimpleStateMachine Index

These are the posts in the series: A Simple State Machine in .NET A Simple State Machine in .NET - Adding Code-based Implementation Simple State Machine Updates

ASP.NET Core Index

These are the posts in the series: ASP.NET Core API Versioning Getting Location and Weather from an IP Address Checking the Health of an ASP.NET Core Application Rate Limiting in ASP.NET Core Injecting Action Method Values from Configuration in ASP.NET Core OpenTelemetry with ASP.NET Core ASP.NET Core Middleware The Disposable Pattern in ASP.NET Core ASP.NET Core Distributed Tracing

C#/.NET Index

These are the posts in the series: Less Known LINQ Methods C# Records Implementing the Strategy Pattern with .NET Dependency Injection .NET 10 Validation Nullable and Required Types Working with Strings in .NET C# Magical Syntax .NET Collections .NET Metrics .NET Cancellation Tokens Service Discovery in .NET Retrieving Services from Dependency Injection in .NET The Evolution of .NET Dependency Resolution Named HttpClient Registrations What's in a Task?

EF Core Index

These are the posts in the series: Audit Trails in EF Core Text Querying with EF Core and SQL Server Using GUIDs with EF Core Value Generators in EF Core Optimising EF Core How to Seed Data to EF Core EF Core State Validation Modern Mapping with EF Core Table Inheritance with EF Core

Domain Events Index

These are the posts in the series: Domain Events with .NET Domain Events with .NET - New Features

Isolator Index

These are the posts in the series: Introducing Isolator - a framework for running isolated code for .NET Distributed Isolator Isolator with References Scanning Docker Support for Isolator

GitHub Index

These are the posts in the series: A GitHub Actions Pipeline to Generate OpenAPI Documentation

ASP.NET Core Multitenancy Index

These are the posts in the series: Multitenancy Techniques for EF Core Multitenancy Techniques for ASP.NET Core Multitenancy Techniques for the UI in ASP.NET Core Multitenancy Techniques for the Business Logic in ASP.NET Core (TBD)

Java Versus C# Index

These are the posts in the series: Java vs C# - Part 1 Java vs C# - Part 2 Java vs C# - Part 3 Java vs C# - Part 4 (TBD)

EF Core Pitfalls Index

These are the posts in the series: Entity Framework Core Pitfalls: Calling DB Functions in LINQ Queries as Extension Methods Entity Framework Core Pitfalls: Asynchronous vs Synchronous Calls and Interceptors Entity Framework Core Pitfalls: Getting a Single Record by Key

ASP.NET Core Pitfalls Index

These are the posts in the series: ASP.NET Core Pitfalls – Posting a String ASP.NET Core Pitfalls - Action Constraint Order ASP.NET Core Pitfalls - Content Type Mismatch

ASP.NET Core Extension Points Index

These are the posts in the series: ASP.NET Core Extension Points - Core ASP.NET Core Extension Points - MVC ASP.NET Core Extension Points - Minimal API (TBD) ASP.NET Core Extension Points - Razor Pages (TBD)

.NET Synchronisation APIs Index

These are the posts in the series: .NET Synchronisation APIs - Part 1 - In-Process Synchronisation .NET Synchronisation APIs - Part 2 - Out-of-Process Synchronisation .NET Synchronisation APIs - Part 3 - Distributed Synchronisation (TBD)

AI and LLMs in .NET Index

These are the posts in the series: Generating Structured Code Using Azure, OpenAI and .NET Using LLMs and MCP in .NET An Orchestration Framework for .NET Agents (TBD)

Series of Posts

This is a permanent post for listing the series of posts that are still ongoing, in no particular order: Topic Status .NET AI & LLMs Work in progress .NET Synchronisation APIs Work in progress (2 missing) ASP.NET Core Work in progress ASP.NET Core Extension Points Work in progress ASP.NET Core Multitenancy Work in progress (1 missing) ASP.NET Core Pitfalls Work in progress C#/ .NET Work in progress EF Core Work in progress EF Core Pitfalls Work in progress GitHub Work in progress Java ...

Less Known LINQ Methods

Introduction LINQ extension methods are pretty cool and they make our lives much easier. They are implemented as .NET extension methods over the Enumerable class in the System.Linq namespace and as usual they all apply to IEnumerable<T> sequences. This time I'll be talking about some LINQ methods that are not so commonly used (as per my understanding). Not sure about you guys, but sometimes I don't even remember that some of these exist. Aggregate/AggregateBy Aggregate applies an accumulator function over a sequence, reducing it to a single value. AggregateBy (.NET 9+) does the same but groups by a key first, producing one accumulated result per key. // Aggregate var product = new[] { 1, 2, 3, 4 }.Aggregate((acc, x) => acc * x); // 24 // AggregateBy var words = new[] { "apple", "ant", "bear", "bee" }; var lengthSums = words.AggregateBy( keySelector: w => w[0], seed: 0, func: (acc, w) => acc + w.Length...