Executing Stored Procedure in Entity Framework Core 2.0

Have a look over the documentation from MS: https://learn.microsoft.com/en-us/ef/core/querying/raw-sql

var blog = context.Blogs
    .FromSql("EXECUTE dbo.GetMostPopularBlogs")
    .SingleOrDefault();

They have a git repo full of examples: https://github.com/aspnet/EntityFramework.Docs/blob/master/samples/core/Querying/Querying/RawSQL/Sample.cs

To utilise stored procedures in EF Core, check out this video: https://www.youtube.com/watch?v=bX3BPSbvofE


DbCommand cmd = ctx.Database.GetDbConnection().CreateCommand();
cmd.CommandText = "SPName";
cmd.CommandType = CommandType.StoredProcedure;

    if (cmd.Connection.State != ConnectionState.Open)
    {
        cmd.Connection.Open();
    }

return await cmd.ExecuteNonQueryAsync();

Here is a post about that: https://nodogmablog.bryanhogan.net/2016/07/entity-framework-core-and-calling-a-stored-proceduce/#comment-60582


Able to solve my problem with below code. This is based on suggestions given in below replies.

using (var command = context.Database.GetDbConnection().CreateCommand())
    {
        command.CommandText = "StoredProcedureName";
        command.CommandType = CommandType.StoredProcedure;

        context.Database.OpenConnection();

        var dataReader = command.ExecuteReader();

        if (dataReader.Read())
        {
            string _test = dataReader.GetString(dataReader.GetOrdinal("ColumnName"));
        }
    }