Efcore: query stored procedures using raw data

Created on 21 May 2020  路  2Comments  路  Source: dotnet/efcore


Right now my team is using efCore 2.2 and we are able to query stored procedures using raw data thanks to the code below. We are trying to upgrade to 3.1.4 but it is bricking our project.

public static RelationalDataReader ExecuteSqlQuery(this DatabaseFacade databaseFacade, string sql, params object[] parameters)
{
var concurrencyDetector = databaseFacade.GetService();

        using (concurrencyDetector.EnterCriticalSection())
        {
            var rawSqlCommand = databaseFacade
                .GetService<IRawSqlCommandBuilder>()
                .Build(sql, parameters);
            return rawSqlCommand
                .RelationalCommand
                .ExecuteReader(
                    databaseFacade.GetService<IRelationalConnection>(),
                    parameterValues: rawSqlCommand.ParameterValues);
        }
    }


I'm not the most well versed in efCore and was wondering if there was something we could do to fix this or will we have to rewrite our sproc queries in our api.

closed-question customer-reported

Most helpful comment

ExecuteReader takes RelationalCommandParameterObject in 3.x release.

All 2 comments

ExecuteReader takes RelationalCommandParameterObject in 3.x release.

Note: your code above seems to have been using an internal service (ConcurrencyDetector) - it's a good idea to avoid that unless absolutely necessary (and to tell us why it's absolutely necessary). Note that since 3.0, EF Core comes with an analyzer that flags usage of internal APIs.

For doing the above, I'd also just consider dropping down to ADO.NET, i.e. calling databaseFacade.GetDbConnection. At that point you have a regular connection, can create a command and just execute it, rather than using EF Core's services to do it (there's not much extra value here).

Was this page helpful?
0 / 5 - 0 ratings