Search for existing suggestions

Add DbContext.Database.SqlQuery to Entity Framework Core

I can't always get the best performance for extremely specific queries queries in Entity Framework. Sometimes it is best to just write very specific SQL (Currently working on a CTE recursive query that I wouldn't even know how to begin getting into EF without hitting the DB multiple times). Having SqlQuery<T> available give me complete freedom and allows me to use EF in the same way as Micro ORMS like Dapper to map a specific DTO when it makes sense. I don't wan't to have to map a custom object to my context and use the FromSql implementation off of a DbSet. I will probably end up just building my own extensions to solve this for now, but this always felt like a fundamental part of EF that allowed freedom on an as needed/when it makes sense basis.

37 votes
Sign in
(thinking…)
Password icon
Signed in as (Sign out)

We’ll send you updates on this idea

Matt shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

The feature is part of EF Core 2.1, which is currently in preview, but with a small variation. The feature is called query types and the idea is that these are types that are added to the model (e.g. they are not ad-hoc) and however they don’t need to have entity keys and they don’t need to be mapped to a table. On of these query types is defined in the model it can be used in ad-hoc queries, e.g. context.Query().FromSql(“SELECT …”);

Please create issues in our issue tracker if you find any problems.

6 comments

Sign in
(thinking…)
Password icon
Signed in as (Sign out)
Submitting...
  • Rui Luo commented  ·   ·  Flag as inappropriate

    Another scenario is where you have a shared "Data" library within which the db context is defined. Then all the other projects in the solution refer to this library and do their own business. Each project will have its application specific query results that don't make sense in other projects. Forcing these app-specific POCOs to be organized as domain entity really does not make sense.

  • Nicholas Petersen commented  ·   ·  Flag as inappropriate

    How is this anything other than *totally baffling* that they would remove this??? It's like putting a big tag on EFCore (which I really want to use) that says: "HEY, don't use me, I'm not a full-fledged ORM." Can't even believe this.

Feedback and Knowledge Base