EF Core Implementing Fuzzy Search

Fuzzy search allows finding strings that match the pattern approximately. It is a feature needed in almost every app, but it can be a little problematic to implement. We will focus on EFCore PostgreSql provider as it is free and seems to be the most popular choice nowadays (for me at least). First, our EFCore project needs this package: PostgreSQL.FuzzyStringMatch Then, we need to register it with our DbContext, so let’s navigate to Startup....

EF Core Automatic Creation and Modification Timestamp

For auditing purposes, we might want our entities to have creation and/or modification timestamps (we may also extend it with some additional data, like user ID). It can be a pain to keep in mind to always set the appropriate columns, but we can make EFCore do it for us. First we need to create some sort of interface for it, like: ICreationAudited.cs or IAuditedEntity.cs. public interface IAuditedEntity { DateTimeOffset CreationDateTime { get; set; } DateTimeOffset?...

EF Core Registering Model Configurations By Convention

When our model configuration is stored all in one file it becomes tedious to edit it as the application grows. Even, if developers agree to stick to some kind of ordering it is bound to blur and fade away as one of the developers inevitably adds one configuration in the wrong place. How can we prevent it? The answer to our problem is: IEntityTypeConfiguration<T>. By implementing the interface you can configure the model for each class by implementing the interface’s only method void Configure(EntityTypeBuilder<T> builder)....

EF Core Implementing Soft Delete

Soft deleting is an easy way of deleting data without actually removing it from the database. Instead of performing DELETE on the database we mark it as deleted and filter it out by default on the application side. We want our soft delete mechanism to function seamlessly with EFCore, after all, it’s just the implementation detail, so we need to intercept all DbContext Remove calls. So, to achieve this we need two parts:...