Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

With Entity Framework Core removing dbData.Database.SqlQuery<SomeModel> I can't find a solution to build a raw SQL Query for my full-text search query that will return the tables data and also the rank.

The only method I've seen to build a raw SQL query in Entity Framework Core is via dbData.Product.FromSql("SQL SCRIPT"); which isn't useful as I have no DbSet that will map the rank I return in the query.

Any Ideas???

share|improve this question
5  
I will greatly miss the SqlQuery<T> and don't want to have to map custom classes to my DbContext when I really just need a simple DTO for a specific use case. I have created a user voice to request adding this feature back in to EF Core that anyone can vote up if they would like this feature back: data.uservoice.com/forums/… – Matt Sanders Mar 28 at 16:35
up vote 6 down vote accepted

In EF Core you no longer can execute "free" raw sql. You are required to define a POCO class and a DbSet for that class. In your case you will need to define Rank:

var ranks = DbContext.Ranks
   .FromSql("SQL_SCRIPT OR STORED_PROCEDURE @p0,@p1,...etc", parameters)
   .AsNoTracking().ToList();

As it will be surely readonly it will be useful to include the .AsNoTracking() call.

share|improve this answer
2  
So I guess I will also have to extend the DbContext to include a new property DbSet<Rank> Rank { get; set; }. What implications will this have now in reference to linq? I.e. Wont we now be able to use a statement like DBContext.Rank.Where(i => i.key == 1), and won't this statement have no implementation in SQL and therefore fail? – David Harlow Feb 25 at 17:25
    
Linq emitted against this set have to be resolved in memory. If you need to emit different WHERE sql clause you have to include them as parameters or build a different script. – E-Bat Feb 25 at 20:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.