问题
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