Home > database >  EF 6 Plus - How To Do Future Raw Query
EF 6 Plus - How To Do Future Raw Query

Time:10-25

I am using this EF 6 PLUS library, and I am trying to do future raw query, something like this

context.Database.SqlQuery<SomeClass>("query here").Future();

but there is not such option in this library, I also tried using DeferredFirst(), DeferredSingle() of this library but no luck, those methods don't let me insert raw queries.

How can I achieve this with this library or another one?

CodePudding user response:

Future and all methods like DeferredFirst are extension methods on IQueryable<T>:

public static QueryFutureEnumerable<T> Future<T>(this IQueryable<T> query)

Database.SqlQuery returns DbRawSqlQuery<T>, which implements IEnumerable<T>, not IQueryable<T>, so the extension doesn't apply. There's no way to execute SqlQuerys in one Future batch with other queries.

You can alleviate some of the "pain" by opening the context's connection before executing a mix of queries and closing it afterwards. That prevents EF from closing and opening the connection for each individual query it executes. It's not the same as executing queries in deferrable batches, but it may add some efficiency.

try
{
    context.Database.Connection.Open();
    ...
}
finally
{
    context.Database.Connection.Close();
}
  • Related