在使用Entity Framework执行存储过程时,过程需要'ntext / nchar / nvarchar类型的参数'@statement'

时间:2017-02-25 11:19:24

标签: c# sql-server entity-framework

我在执行带有Entity Framework的存储过程时遇到错误,该过程需要'ntext / nchar / nvarchar类型的参数'@statement'。

这是我存储的行为:

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#代码

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

1 个答案:

答案 0 :(得分:0)

使用此默认脚本创建AspNetRoles表..如果你没有改变它..

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