在使用Entity Framework执行存储过程时,过程期望类型为'ntext/nchar/nvarchar的参数'@statement' [英] Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar while executing a stored procedure with Entity Framework

查看:42
本文介绍了在使用Entity Framework执行存储过程时,过程期望类型为'ntext/nchar/nvarchar的参数'@statement'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到错误

程序需要类型为'ntext/nchar/nvarchar'的参数'@statement'

Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'

在使用Entity Framework执行存储过程时.

while executing a stored procedure with Entity Framework.

这是我的存储过程:

CREATE PROCEDURE [dbo].[usp_RolesList]   
    (@WhereCond VARCHAR(50))
AS
BEGIN
    SET NoCount ON

    DECLARE @SQLQuery AS VARCHAR(MAX)
    SET @SQLQuery = 'Select * From dbo.AspNetRoles ' + @WhereCond

    EXECUTE sp_Executesql @SQLQuery
END

这是我用来执行结果的C#代码

and this is my c# code that I am using for executing the results

var idParam = new SqlParameter
                    {
                        ParameterName = "WhereCond",
                        Value = "where Id > 0"
                    };
var courseList = ctx.Database.SqlQuery<UserRoles>("exec usp_RolesList @WhereCond ", idParam).ToList<UserRoles>();

推荐答案

使用此默认脚本创建的AspNetRoles表..如果您没有更改,则..

AspNetRoles table creating with this default script.. if u no changed it..

CREATE TABLE [dbo].[AspNetRoles](
[Id] [nvarchar](128) NOT NULL,
[Name] [nvarchar](256) NOT NULL,
CONSTRAINT [PK_dbo.AspNetRoles] PRIMARY KEY CLUSTERED 
(
  [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

所以试试这个...

var idParam = new SqlParameter
                {
                    ParameterName = "WhereCond",
                    Value = "where not Id is null" //it will be work but it makes no sense because it is primarkey field
                };
var courseList = ctx.Database.SqlQuery<UserRoles>("exec usp_RolesList @WhereCond ", idParam).ToList<UserRoles>();

这篇关于在使用Entity Framework执行存储过程时,过程期望类型为'ntext/nchar/nvarchar的参数'@statement'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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