zoukankan      html  css  js  c++  java
  • [转]How to get return values and output values from a stored procedure with EF Core?

    本文转自: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);
        }
    
    shareimprove this question
     
        
    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
     
     
  • 相关阅读:
    Gamma correction 伽马校准及 matlab 实现
    特征描述子(feature descriptor) —— HOG(方向梯度直方图)
    特征描述子(feature descriptor) —— HOG(方向梯度直方图)
    opencv cvtColor dtype issue(error: (-215) )
    opencv cvtColor dtype issue(error: (-215) )
    python 命令行:help(),'more'不是内部或外部命令,也不是可运行的程序或批处理文件
    PES包格式
    Makefile编写 四 函数篇
    Makefile编写 三 伪目标的作用
    ELF文件和BIN文件
  • 原文地址:https://www.cnblogs.com/freeliver54/p/7325122.html
Copyright © 2011-2022 走看看