Entity Framework 8: Raw SQL queries on unmapped types

1/12/2023
1 minute read

The next iteration of Entity Framework, namely Entity Framework 8, will have a new and exciting feature:

Support raw SQL queries without defining an entity type for the result

That means less boilerplate code!

Until now, you had to create a mapping for types that are returned from SqlQueryRaw. With Entity Framework 8 this looks a bit different as a new feature helps us effectively to reduce that boilerplate.

A small example:

// This works from now on
context.Database.SqlQueryRaw<MyUnmappedType>(@"SELECT * FROM SomeTableOrView");

This is especially nice if you work with a lot of SQL views. Until now, you had to define CLR types that had the correct mapping inside the DbContext.

A few notes about the limitations of that feature.

  • The entity type cannot have relationships
  • Properties are mapped by convention and mapping attributes are respected.
  • The entity types are keyless.

Resources

Entity Framework and ordered indexes

In Entity Framework 7, the team has added support for ordered indexes to the fluent API. In this blog post we will look at how to use this feature and what it means for your database.

Entity Framework - Storing complex objects as JSON

From time to time, it is nice to store complex objects or lists as JSON in the database. With Entity Framework 8, this is now easily possible. But this was possible all along with Entity Framework 7.

Native DateOnly and TimeOnly support in Entity Framework 8 in SQL Server

.NET 6 brought us two new datatypes: DateOnly and TimeOnly. For those types we don't have any first class support in Entity Framework - until now.

There is a recent change, that hit us with Entity Framework 8 that might ease the situation and brings native support for those types.

An error has occurred. This application may no longer respond until reloaded. Reload x