3

I'm transitioning from .NET Framework to Core and, while it's been a mostly seamless and beneficial process, there are some difficulties.

The latest is how to perform the fairly simple EF-based task of taking results from a raw query and mapping those results into a list of objects of a given type. In Framework, I could so something like:

using (DbContext Context = new DbContext(Connection))
{
    return Context.Database.SqlQuery<T>(Sql).ToList<T>();
}

Where Connection would be a raw connection string, Sql a raw query string, and T any given type known to fit the output of the Sql.

In Core, I can't seem to grab a new Context with nothing but a connection string, and nor can I apply the SqlQuery method. There's got to be as quick & easy a way to do this in Core, but I can't seem to find it.

I do have System.Data.Common and System.Data.SqlClient, along with EntityFrameworkCore installed into the project.

Michael Doleman
  • 522
  • 2
  • 9
  • 24
  • Possible duplicate of [How to Map the results of Sql query onto Objects in Net Core2?](https://stackoverflow.com/questions/53586872/how-to-map-the-results-of-sql-query-onto-objects-in-net-core2) –  Dec 05 '18 at 04:02

1 Answers1

0

I think that the answer to my own question is that this simply can't be done -- at least in this very automatic way -- in EF Core. It requires some additional work, to manually parse and loop the query result. That's unfortunate, but not a huge deal.

Michael Doleman
  • 522
  • 2
  • 9
  • 24
  • see https://stackoverflow.com/questions/53586872/how-to-map-the-results-of-sql-query-onto-objects-in-net-core2/53588774#53588774 –  Dec 05 '18 at 04:01