asp.net mvc中的SP问题

时间:2014-04-01 12:57:00

标签: c# asp.net sql asp.net-mvc tsql

我有asp.net mvc 4项目,尝试调用T-SQL,我需要选择一些不是所有列的行

CREATE PROCEDURE [dbo].[SP_name]
@iSkip int, @iTake int
    AS
    BEGIN 
    SELECT [Column1], [Column2], [Column3] FROM [dbo].[Table] WITH(NOLOCK)
    ORDER BY 
     [Column1] DESC
OFFSET  @iSkip ROWS 
FETCH NEXT @iTake ROWS ONLY 
    END 

但是当我尝试从代码中调用此SP时出现以下错误

The data reader is incompatible with the specified 'Model.Table'. A member of the type, 'Column4', does not have a corresponding column in the data reader with the same name.

当我尝试在MS SQL Studio上调用它时一切正常,有人知道我的错误在哪里吗?我该怎么办?

编辑:

这是在存储库中:

public IEnumerable<T> GetAllEntitiesWithParam(string procedureName, int param1, int param2) {
            return _context.DbContext.Database.SqlQuery<T>("EXEC " + procedureName + " @param1, @param2", new SqlParameter("param1", param1), new SqlParameter("param2", param2));
        }

这里面是代码:

IEnumerable<Table> list = _myRepo.GetAllEntitiesWithParam("SP_name", par1, par2);

1 个答案:

答案 0 :(得分:1)

错误似乎很清楚 - 模型中有一个Column4不在sproc的结果集中。您可以为sproc结果添加“默认”值:

CREATE PROCEDURE [dbo].[SP_name]
@iSkip int, @iTake int
    AS
    BEGIN 
    SELECT [Column1], 
           [Column2], 
           [Column3],
           NULL AS [Column4]     // <----------------
    FROM [dbo].[Table] WITH(NOLOCK)
    ORDER BY 
     [Column1] DESC
OFFSET  @iSkip ROWS 
FETCH NEXT @iTake ROWS ONLY 
    END