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 · 2 comments

Comments

@clockwiseq
Copy link

commented Nov 21, 2017

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

This comment has been minimized.

Copy link
Member

commented Nov 21, 2017

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

@devel0

This comment has been minimized.

Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.