How to use stored procedures with SqlParameters in

2020-04-11 06:46发布

问题:

I have tried the below

var p = new SqlParameter("Option", "AUTHENTICATE");
var user = _context.Set<User>().FromSqlRaw("EXECUTE dbo.spGeneral_Authenticate @Option", p).ToList();
var user = _context.Set<User>().FromSqlRaw("EXECUTE dbo.spGeneral_Authenticate @Option=@Option", p).ToList();

and

SqlParameter[] ps = new SqlParameter[1];
ps[0] = new SqlParameter("Option", "AUTHENTICATE");
var user = _context.Set<User>().FromSqlRaw("EXECUTE dbo.spGeneral_Authenticate @Option", ps).ToList();
var user = _context.Set<User>().FromSqlRaw("EXECUTE dbo.spGeneral_Authenticate @Option=@Option", ps).ToList();

Error:

InvalidCastException: The SqlParameterCollection only accepts non-null SqlParameter type objects, not SqlParameter objects.

回答1:

InvalidCastException: The SqlParameterCollection only accepts non-null SqlParameter type objects, not SqlParameter objects.

For the above error, SqlParameter should be Microsoft.Data.SqlClient.SqlParameter not System.Data.SqlClient.SqlParameter