Entity Framework 7 FromSql 存储过程返回值 [英] Entity Framework 7 FromSql stored procedure return value

查看:19
本文介绍了Entity Framework 7 FromSql 存储过程返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用实体框架 7 中的新 FromSql 命令返回一个值.如果一切顺利,我的存储过程返回 0 值,如果发生错误则返回 1 值.

I am trying to return a value using the new FromSql command in Entity Framework 7. My stored procedure returns a value of 0 if all goes well and 1 if an error occurs.

使用 FromSqlDbSet 我们可以例如做

Using FromSql with DbSet we can for example do

_dbContext.ExampleEntity.FromSql('someSproc', 'param')

你将如何从中得到 0 或 1 的标量返回值?

How will you get the scalar return value of 0 or 1 from this?

推荐答案

看起来存储过程支持是 仍在积压中.

Looks like stored procedure support is still on the backlog.

这是您可以使用 _dbContext.ExecuteStoredProcedure("someSproc", "param"); 调用的示例扩展方法.

Here's an example extension method you can call using _dbContext.ExecuteStoredProcedure("someSproc", "param");.

public static class DbContextExtension 
{
    public static int ExecuteStoredProcedure(this DbContext context, string name, string parameter)
    {
        var command = context.Database.GetDbConnection().CreateCommand();
        command.CommandType = CommandType.StoredProcedure;
        command.CommandText = name;
        var param = command.CreateParameter();
        param.ParameterName = "@p0";
        param.Value = parameter;
        command.Parameters.Add(param);
        return (int)command.ExecuteScalar();
    }
}

这篇关于Entity Framework 7 FromSql 存储过程返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆