JinYazhou +

Raw SQL Query without DbSet - .net core

原文链接:https://stackoverflow.com/questions/35631903/raw-sql-query-without-dbset-entity-framework-core

Q:
With Entity Framework Core removing dbData.Database.SqlQuery<SomeModel> I can not 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 have seen to build a raw SQL query in Entity Framework Core is via dbData.Product.FromSql(“SQL SCRIPT”); which is not useful as I have no DbSet that will map the rank I return in the query.
Any Ideas???


A:
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.

最新评论