SHARE:

From .NET 6 to .NET 8, my migration experience: Entity Framework Core 8

Introduction

We finally migrated our web applications to .NET 8 from .NET 6. (And yes .NET 6 will already be deprecated on November 7th). Since we are using Entity Framework Core 6, we had to upgrade to Entity Framework Core 8. In this post, I will explain my migration experience (which does not cover all migration scenarios)by detailing the changes I had to make. Some of these changes have been brought by Entity Framework Core 7, which we skipped since we haven’t migrated to .NET 7 (EOL May 7th 2024).

Changes to make

The migration was actually quite simple in my case. First, as you might expect, you have to upgrade the Nuget packages, then you have to replace some extension methods on the ModelBuilder, and finally, you have to set a breaking change on the tables that contain Triggers.

Nuget packages

A picture is worth a thousand words, I will show you in the blink of an eye the changes to make.

As you can see, you can pretty much replace 6.x.x with 8.x.x. If you are wondering what Z.EntityFramework.Plus.EFCore is, it is an extension on the Entity Framework Core developed and maintained by a third party. If you want to know more, you can check the website related to this package here: https://entityframework-extensions.net/.

Entity Builder changes

On the Entity Builder, the HasName extension has been renamed to HasDatabaseName:

Simple isn’t it?

Managing Triggers

This change is going to require a bit more work. So what’s going on here? There’s a change in the behaviour of Entity Framework Core. Any table that contains one or more Triggers will throw an exception when you do a CRUD operation involving a Trigger on that table for that CRUD operation (could be only on insertion if you have a Trigger on inserts. The error message generated is:

Unhandled exception. Microsoft.EntityFrameworkCore.DbUpdateException: Could not save changes because the target table has database triggers. Please configure your entity type accordingly, see https://aka.ms/efcore-docs-sqlserver-save-changes-and-triggers for more information. —> Microsoft.Data.SqlClient.SqlException (0x80131904): The target table ‘{TriggerName}’ of the DML statement cannot have any enabled triggers if the statement contains an OUTPUT clause without INTO clause.

The solution is to tell globally, with a Convention configuration (applying to all tables in one shot) to Entity Framework Core that some of your tables contain Triggers, from then on none of your tables will generate an exception when you do a CRUD operation on them:

Then you must attach it on your DbContext as follows within a method named ConfigureConvention:

Conclusion

The difficulty level was pretty low. Even though I missed Entity Framework Core 7, the migration from version 6 to 8 was easy, which is not always the case when migrating from two versions at once. I hope this post will help you in your migration 🙂

Written by

anthonygiretti

Anthony is a specialist in Web technologies (14 years of experience), in particular Microsoft .NET and learns the Cloud Azure platform. He has received twice the Microsoft MVP award and he is also certified Microsoft MCSD and Azure Fundamentals.