Stored procedure returns int instead of result set

后端 未结 17 2012
说谎
说谎 2020-12-03 13:02

I have a stored procedure that contains dynamic select. Something like this:

ALTER PROCEDURE [dbo].[usp_GetTestRecords] 
    --@p1 int = 0, 
    --@p2 int =          


        
17条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-03 13:44

    If you need to do this, then you might be better off just making a partial of the dbcontext and creating the C# function yourself that will use SqlQuery to return the data you need. Advantages over some of the other options is:

    1. Don't have to change anything when the model updates
    2. Won't get overwritten if you do it directly in the generated class (someone above mention this as if it's an option :) )
    3. Don't have to add anything to the proc itself that could have side effects now or later on

    Example Code:

     public partial class myEntities
        {
           public List usp_GetTestRecords(int _p1, int _p2, string _groupId)
           {
              // fill out params
              SqlParameter p1 = new SqlParameter("@p1", _p1);
              ...
              obj[] parameters = new object[] { p1, p2, groupId };
    
              // call the proc
              return this.Database.SqlQuery(@"EXECUTE usp_GetTestRecords @p1, @p2, @groupId", parameters).ToList();
           }
        }
    

提交回复
热议问题