How to use stored procedures with SqlParameters in EF Core 3.0

后端 未结 2 777
小鲜肉
小鲜肉 2021-01-19 02:01

I have tried the below

var p = new SqlParameter(\"Option\", \"AUTHENTICATE\");
var user = _context.Set().FromSqlRaw(\"EXECUTE dbo.spGeneral_Authe         


        
相关标签:
2条回答
  • 2021-01-19 02:35

    In .Net core 3.0 or above version,Please use below code: Instead of System.Data.SqlClient.SqlParameter Use Microsoft.Data.SqlClient.SqlParameter

     int AuthourId = 3;
     Microsoft.Data.SqlClient.SqlParameter authorId = new Microsoft.Data.SqlClient.SqlParameter("@AuthorId", AuthourId);
     IEnumerable<Book> booksByAuthor = shopDbContext.Books
                                                    .FromSqlRaw("Select * from Book where AuthorId=@AuthorId", authorId)
                                                    .ToList();
    
    0 讨论(0)
  • 2021-01-19 02:38

    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

    0 讨论(0)
提交回复
热议问题