本文转自:https://stackoverflow.com/questions/43935345/how-to-get-return-values-and-output-values-from-a-stored-procedure-with-ef-core

问题未解决

ALTER PROCEDURE [dbo].[SearchMovies]
--@Year int = null,
@CategoryIds varchar(50) = null,
@Keywords nvarchar(4000) = null,
@PageIndex int = 1,
@PageSize int = 2147483644,
@TotalRecords int = null OUTPUT
As ...

EF Repository:

 public class EFRepository<T> : IRepository<T> where T : BaseEntity
{
private readonly ApplicationDbContext _ctx;
private DbSet<T> entities;
string errorMessage = string.Empty; public EFRepository(ApplicationDbContext context)
{
this._ctx = context;
entities = context.Set<T>();
}
... public IQueryable<T> ExecuteStoredProcedureList(string commandText, params object[] parameters)
{
_ctx.Database.ExecuteSqlCommand(commandText, parameters);
return entities.FromSql(commandText, parameters);
}
}

I call this like:

var pCategoryIds = new SqlParameter()
{
ParameterName = "@CategoryIds",
Value = commaSeparatedCategoryIds,
DbType = DbType.String
};
var pKeywords = new SqlParameter()
{
ParameterName = "@Keywords",
DbType = DbType.String,
Value = name
};
var pPageIndex = new SqlParameter()
{
ParameterName = "@PageIndex",
DbType = DbType.Int32,
Value = pageIndex
};
var pPageSize = new SqlParameter()
{
ParameterName = "@PageSize",
DbType = DbType.Int32,
Value = pageSize
}; var pTotalRecords = new SqlParameter();
pTotalRecords.ParameterName = "@TotalRecords";
pTotalRecords.Direction = ParameterDirection.Output;
pTotalRecords.DbType = DbType.Int32; var query1 = _ctx.Database.ExecuteSqlCommand("dbo.[SearchMovies] " +
"@CategoryIds, @Keywords, @PageIndex, @PageSize, @TotalRecords OUTPUT",
pCategoryIds, pKeywords, pPageIndex, pPageSize, pTotalRecords); var query2 = _ctx.Set<MovieItem>.FromSql("dbo.[SearchMovies] " +
"@CategoryIds, @Keywords, @PageIndex, @PageSize, @TotalRecords OUTPUT",
pCategoryIds, pKeywords, pPageIndex, pPageSize, pTotalRecords);

query1 does get the output pTotalRecords fine, but no return values, and the second query2 gets the return values but no output parameter.

In EF 6, we used to have SqlQuery to do both actions in one command, how can I do the same in EF core ?

UPDATED:

Temporarily, I run 2 query, one to get the output param and one for result set.

 public IQueryable<T> ExecuteStoredProcedureList(string commandText, params object[] parameters)
{
_ctx.Database.ExecuteSqlCommand(commandText, parameters);
return entities.FromSql(commandText, parameters);
}
asked May 12 at 10:21
nam vo

96062036
 
    
What's your MovieItem model? Does it have the exact same properties as returned by the stored procedure?– Tseng May 12 at 10:34
    
so you meant with Fromsql it can return output param ? ExecuteSqlCommand will return a integer, and in stored proc, SELECT m.* ... from dbo.MovieItem m which is exactly the same as MovieItem entity. – nam vo May 12 at 10:54
    
As of EF Core 1.x, EF Core only supports result values which map to an existing entity and no fields can be missing from that entity. See the EF Core roadmapRaw SQL queries for non-Model types allows a raw SQL query to be used to populate types that are not part of the model (typically for denormalized view-model data). – Tseng May 12 at 11:37
    
I don't have problem to get the results as I said above, using FromSql I get all results back ok except TotalRecords which is a rowcount. And It seems EF core still lacks this feature, as I've googled for hours and no one has an example for this. See dotnetthoughts.net/how-to-execute-storedprocedure-in-ef-core – nam vo May 12 at 13:25
    
Have you tried to use pure ADO.NET (System.Data.SqlClient) ? – H. Herzl May 12 at 21:39
 
 

[转]How to get return values and output values from a stored procedure with EF Core?的更多相关文章

  1. Dapper: How to get return value ( output value) by call stored procedure

    使用Dapper 执行存储过程插入一条数据,同时返回主键 Dapper 的参数类型有以下四种 System.Data.ParameterDirection public enum ParameterD ...

  2. MyBatis调用存储过程,含有返回结果集、return参数和output参数

    Ibatis是我们经常使用的O/R映射框架,mybats是ibatis被Google收购后重新命名的一个工程,当然也做了大量的升级.而调用存储过程也是一次额C/S架构模式下经常使用的手段,我们知道,i ...

  3. EF 接收OUTPUT参数的方法 How to Retrieve Stored Procedure Output Parameters in Entity Framework

    原文地址:http://blogs.microsoft.co.il/gilf/2010/05/09/how-to-retrieve-stored-procedure-output-parameters ...

  4. [转]Easy Stored Procedure Output Oracle Select

    本文转自:http://www.oraclealchemist.com/oracle/easy-stored-procedure-output/ I answered a question on a ...

  5. [D3] Convert Input Data to Output Values with Linear Scales in D3

    Mapping abstract values to visual representations is what data visualization is all about, and that’ ...

  6. forward reference前向引用,gloal values and local values全局变量和局部变量,recursive function递归函数

    1.全局变量与局部变量练习 1 # -*- coding: UTF-8 -*- 2 def bar(): 3 print('from bar') 4 def foo(): 5 print('from ...

  7. 微软版的SqlHelper.cs类

    一,微软SQLHelper.cs类 中文版: using System; using System.Data; using System.Xml; using System.Data.SqlClien ...

  8. OracleHelper类

    using System; using System.Collections; using System.Collections.Generic; using System.Data; using S ...

  9. SqlHelper类

    using System; using System.Collections; using System.Collections.Generic; using System.Data; using S ...

随机推荐

  1. Solr(一) 安装部署环境

    Solr运行环境 第一步:安装java jdk 第二步:安装tomcat服务器 安装过程:参考http://jingyan.baidu.com/article/870c6fc33e62bcb03fe4 ...

  2. 如何修改Entity Framework Db Frist模式下的Entity继承关系?

    1.准备工作 Db Frist创建实体数据模型(创建edmx并不是重点,各位随意即可),此处取名ZeroCodeDB,所得文件如图所示:其中红框中的文件(ZeroCodeDB.tt)是各实体的生成的关 ...

  3. Python3.5 学习五

    心灵鸡汤电影推荐: 阿甘正传.辛德勒名单.肖申克的救赎.勇敢的心.角斗士.美国丽人.教父.钢琴师.指环王.西雅图不眠夜.廊桥遗梦.可可西里的美丽传说.放牛班的春天.血钻.战争之王.上帝之城.中央车站. ...

  4. linux下安装nginx,centos安装nginx

    初学nginx,进行简单的安装和配置. 一.依赖 openssl-fips-2.0.9.tar.gz zlib-1.2.11.tar.gz pcre-8.01.tar.gz nginx-1.8.0.t ...

  5. jquery中的$()详解

    一.jQuery的三种$() $号是jQuery“类”的一个别称,$()构造了一个jQuery对象.所以,“$()”可以叫做jQuery的构造函数. 1.$()可以是$(expresion),即css ...

  6. 前后端分离——token超时刷新策略

    前言 记录一下前后端分离下————token超时刷新策略! 需求场景 昨天发了一篇记录 前后端分离应用——用户信息传递 中介绍了token认证机制,跟几位群友讨论了下,有些同学有这么一个疑惑:toke ...

  7. 关于在JS中AJAX导致跨域问题的解决

    在部署一个原声的前端项目的时候,请求该服务器后端接口时发现出现了CORS跨域的问题,但是服务端已经做了同源策略的兼容,常见问题,遂记录. 报错信息: XMLHttpRequest cannot loa ...

  8. 基础概念——令人迷惑的EOF

    EOF概念常常使人迷惑. 首先我们要理解并没有像EOF字符这样的东西. 进一步讲EOF是由内核检测到的一种条件. 应用程序在它接收到由read函数返回的零返回码时,它就会发现EOF条件. 对于磁盘文件 ...

  9. 最新版chrome浏览器如何离线安装crx插件?(转载)

    原文链接:https://newsn.net/say/chrome-crx-offline.html mac新版chrome开启离线插件安装 对于mac新版chrome,注意,大家一定要按照顺序来.m ...

  10. CentOS7 安装 Adobe Flash 看网络视频

    登录 Adobe 网站,找到 Adobe Flash Player 下载页,进入后网页自动识别 Linux 环境,手动选择版本,选择 YUM 方式,自动下载一个 rpm 文件 定位到下载目录,通常默认 ...