必须使用dbcontext.Database.SqlQuery声明标量变量'@custid'?
技术问答
133 人阅读
|
0 人回复
|
2023-09-13
|
我正在尝试使用EF5从bcontext.Database.SqlQuery执行存储过程。1 W! P4 t+ \2 N+ J
它抛出一个错误 must declare the scalar variable '@custid'
0 ` ]# d7 z# l9 f* T9 [. T- ? var results = _MiscContext.Database.SqlQuery<i>(, K0 q$ R" ]! p+ o! B, h) I0 G
"exec sp_GetStaff @custid",
3 z0 T( T$ }: r8 H$ v0 y+ p) i8 ~8 P customerNumber).ToList<i>();
0 @( m, b5 x+ |& Y1如果customerNumber是staff,则SP返回,否则返回empty行。) X) b t. i- r y8 B: c* `! S
ALTER PROCEDURE [dbo].[sp_GetStaff]6 J6 q, B4 B C2 R5 Y9 Z
@custid varchar(12)
+ n" N' I% Z$ C$ yAS8 C7 [1 ]* M7 o7 X% g2 p
BEGIN
: n; b x v" e7 j, i# Z# d5 e SET NOCOUNT ON;
7 J: n9 g5 Z9 w5 ~; m+ ^ [: {, {SELECT $ Y0 J0 {" H, f* Z: y+ \% F
1 AS [C1]& U3 @9 v; b i/ L+ S5 `+ Q( s
FROM [dbo].[Staff] with (nolock)& L- q5 o4 X% E1 o: R' z
WHERE [CUSTOMER_ID] = @custid( J2 K' H; A' l( G k: f
: A: J, C; t) _END1 @/ U; p( @) N# A/ b" |2 P0 _, K/ @
如何处理?; d) u8 T% [# e4 l4 Z8 m7 i4 ~
) O b0 P. p* n8 @/ n
解决方案:4 E6 o/ ], ~, p/ P% e
' ^) E# M5 U4 d5 ?( j
8 u9 C' l" f, t0 x# m4 Q0 X) k# W: `6 c
由于使用的是命名参数,因此必须为要传递的参数指定匹配名称。; @& c; X# y( d: A
var results = _MiscContext.Database.SqlQuery<i>(
$ ^- K/ M$ r- e f& b% ^ "exec sp_GetStaff @custid",) z& U: V* V! ]+ L/ G
new SqlParameter("custid", customerNumber)).ToList<i>(); |
|
|
|
|
|