Detect missing migrations in Entity Framework Core

 
 
  • Gérald Barré
Entity Framework Core allows to update the database schema using migrations. The migrations are created manually by running a CLI command. It's easy to forget to create a new migration when you change the model. To ensure the migrations are up-to-date, you can write a test that compares the current model with the snapshot model: [Fact] public async Task EnsureMigrationsAreUpToDate() { await using var… [read more]

Using Snapshot Testing to validate EF Core schema

 
 
  • Gérald Barré
When using Entity Framework Core, I prefer to view the generated schema as SQL. This allows me to easily validate what EF is doing behind the scenes and ensures that no attributes are missed, such as accidentally using nvarchar(max). My main objectives are: Be notified when the schema changes Review the new schema while editing it and during code reviews The solution I use is use is to have a test that… [read more]

Migrate from MSTest to xUnit using a Roslyn analyzer

 
 
  • Gérald Barré
Both MSTest and xUnit are great test framework. If you are curious about, I've written many blog posts about them MSTest series If you want to migrate to xUnit from MSTest, I've written a Roslyn Analyzer. This analyzer reports all MSTest attributes and assertions in your code. It can also replace most of them with xUnit equivalent. Note that the goal of this analyzer is not to handle 100% of the cases,… [read more]

Using Roslyn to analyze and rewrite code in a solution

 
 
  • Gérald Barré
I've written a lot about Roslyn in the context of Roslyn Analyzers and Source Generators. You can also use Roslyn as a library to analyze and generate code. For instance, you can create a console application that loads a solution, find patterns, and rewrite code. While Roslyn Analyzers are tied to a single project, you can use Roslyn as a library to analyze a whole solution. Let's create the console… [read more]

Exploring CollectionsMarshal for Dictionary

 
 
  • Gérald Barré
Unlike ConcurrentDictionary, Dictionary does not have a GetOrAdd method. This method is useful when you want to add a key-value pair to the dictionary if the key does not exist, or return the value if the key already exists. The naive implementation of this method looks like this: public static TValue GetOrAdd<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue value) where TKey : notnull {… [read more]

Signing commits in Git using SSH keys on Windows

 
 
  • Gérald Barré
Signing commits in Git is a way to verify the authenticity and integrity of your commits. This ensures that the commits are indeed made by you and have not been tampered with. On Windows, it's easy to set up SSH-Agent for easy signature. Here's how you can do it. First, you need to install SSH on Windows. Open a PowerShell window as an administrator and run the following command to install the OpenSSH… [read more]