Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database.SqlQuery<TElement> Missing #10365

Closed
clockwiseq opened this issue Nov 21, 2017 · 3 comments
Closed

Database.SqlQuery<TElement> Missing #10365

clockwiseq opened this issue Nov 21, 2017 · 3 comments

Comments

@clockwiseq
Copy link

In Entity Framework, there is a method to execute a query against the database and return a specified object type. Entity Framework Core does not have this and the only alternative is 4 times the amount of code. Example below.

Database.SqlQuery (Entity Framework 6.0)

This is the SqlQuery method found in Entity Framework 6.0

var integerList = db.Database.SqlQuery<int>("SELECT [MyIntegerColumn] FROM [MyTable]").ToList();

foreach (int id in integerList)
{
    // do something with each in the list or just return the list
}

SqlCommand (Entity Framework Core 2.0)

This is the DbDataReader method found in Entity Framework Core 2.0

using (var command = context.Database.GetDbConnection().CreateCommand())
{
    command.CommandText = "SELECT [MyIntegerColumn] FROM [MyTable]";
    DbDataReader reader = await command.ExecuteReaderAsync();

    if (reader.HasRows)
    {
        while (await reader.ReadAsync())
        {
            // do something with each in the list or just return the list
        }
    }
}

Honestly, quadrupling the amount of code required to perform the same action and expecting the same result is not "more efficient" in my opinion. I have literally dozens of "quick calls" as I call them in my code that will now require me to convert a single line of code over to 4 (or more).

@ajcvickers
Copy link
Member

Raw SQL queries that return arbitrary types is being tracked by #9290.

@devel0
Copy link

devel0 commented Oct 2, 2018

Meanwhile you can try netcore-ef-util and a code like follow

using SearchAThing.EFUtil;
using System.Linq;

...
var integerList = db.ExecSQL<int>("SELECT [MyIntegerColumn] FROM [MyTable]");
foreach (int id in integerList)
{
    // do something with each in the list or just return the list
}

More complex example here

@enicolasgomez
Copy link

enicolasgomez commented May 27, 2020

This is a perfect example why people is moving to NodeJS and alternative frameworks

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants