Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar while executing a stored procedure with Entity Framework

独自空忆成欢 提交于 2021-01-28 07:33:24

问题


I am getting error

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

while executing a stored procedure with Entity Framework.

This is my stored procedure:

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

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>();

回答1:


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]

so try this...

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>();


来源:https://stackoverflow.com/questions/42455360/procedure-expects-parameter-statement-of-type-ntext-nchar-nvarchar-while-exe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!