Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use stored procedures with SqlParameters in EF Core 3.0

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.

like image 791
Palanikumar Avatar asked Sep 28 '19 12:09

Palanikumar


Video Answer


1 Answers

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

like image 100
Palanikumar Avatar answered Oct 01 '22 03:10

Palanikumar