Entity Framework 8: Raw SQL queries on unmapped types

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

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