Moonglade

Modern Markdown blogging with AI-powered localization and search.

Tag: Entity Framework Core


When and Why to Use the `IsUnicode` Method in EF Core

This article provides an in-depth analysis of the practical impact of the `IsUnicode()` configuration in Entity Framework Core on MariaDB and MS SQL Server. Experiments show that this configuration has no effect on MariaDB because the default character set `utf8mb4` already supports Unicode; however, in MS SQL Server, it determines whether string fields are mapped to `nvarchar` or `varchar`. Using `nvarchar` ensures correct storage of Chinese characters and emojis, whereas `varchar` results in garbled non-ASCII characters. The article recommends using `IsUnicode(false)` only when it is confirmed that field content is ASCII-safe (such as pure numbers or English text) to save storage space and improve memory efficiency, particularly when handling massive amounts of data. For URLs or user-generated content, it is advisable to maintain the default Unicode support, even if the content appears safe, to prevent data anomalies caused by special characters or internationalized content entered by users. Additionally, EF Core 6 introduced the `Unicode` attribute as an alternative configuration method. In conclusion, when the data content is uncertain, default Unicode support should be prioritized to ensure system robustness and data integrity, thereby avoiding potential business risks associated with excessive optimization of storage space.

Views 146
C# Database Design EF Core Entity Framework Core IsUnicode Method Model Configuration

Simplifying DDD Value Object Comparison with EF Core Dynamic Expression Trees

This article explores technical solutions for implementing semantic equality queries with value objects in EF Core. It first points out that traditional field-by-field query code is verbose and lacks semantic clarity, while directly comparing value objects for equality results in a compilation exception. To address this, the article proposes and implements a generic `ValueObjectEqualHelper` static class, whose core is the `CheckEqual` method. This method dynamically builds expression trees, iterates through the value object's properties, automatically skips null values to avoid generating redundant `IS NULL` conditions, and correctly constructs binary expression trees for equality comparisons based on property types (primitive or reference types), finally connecting all conditions with `AndAlso` logic. This approach preserves the intuitive semantics of `p => p.Address == new Address(...)` while being correctly translated into SQL by EF Core, solving the pain point of being unable to directly compare value objects for equality in database queries and providing a generic and elegant implementation path.

Views 168
.NET C# Dynamic Condition Generation EF Core Entity Framework Core expression trees

Dynamically Building Expression Trees in EF Core to Set Soft Delete Query Filters for All Entities

This article introduces two QueryFilter configuration approaches for implementing soft delete in Entity Framework Core. First, it demonstrates the conventional method of manually adding `builder.HasQueryFilter(x => !x.SoftDelete)` in each entity configuration class. While simple and direct, this approach incurs high maintenance costs when dealing with a large number of entities. Subsequently, it proposes a superior dynamic expression tree construction scheme. By iterating through all entity types in the model, this approach automatically detects the presence of a `bool` type `SoftDelete` property and uses the `Expression` class to dynamically generate the `x => !x.SoftDelete` Lambda expression to uniformly set the query filter. This method eliminates code duplication and achieves global automation of soft delete filtering. The article also demonstrates how to use the `IgnoreQueryFilters()` method to ignore the filter when necessary to query deleted data, and provides various code implementations ranging from detailed construction to simplified syntax. Finally, it emphasizes the flexibility and vitality of C# syntax in constructing expression trees.

Views 136
.NET Dynamic Expression Trees EF Core Entity Framework Core Soft Delete lambda expressions