The .NET 8 release of Entity Framework Core offers a large list of new features. The goal of this post is not to walk you through all these features, therefore you can have a look at the What's new page on Microsoft Learn, instead I want to talk about a specific feature as a follow-up on the previous posts about EF Core I did this week.
In those posts I talked about the FromSql method to use your handwritten SQL statements to fetch EF Core entities. What I didn’t mention is that this only worked for entities that were registered as an entity to the DbContext.
Starting with the EF Core 8 release, this condition has been removed, allowing us to create any SQL statements you want and map them to C# objects.
This means that EF Core can now become an alternative to micro-ORM’s like Dapper. Of course there is maybe still a performance difference(I’ll do a benchmark and share the results) but feature wise this is a great addition.
To use this feature, we need to call the SqlQuery<T>
method:
The cool thing is that you can combine this with Linq statements. EF Core will generate a SQL query that combines your handwritten statement with generated SQL: