Execute stored procedure in EF Core 3.0 vs 2.2

本小妞迷上赌 提交于 2020-01-24 03:56:29

问题


I am trying to update my code to accommodate changes in EF Core 3.0, specifically the deprecation of ExecuteSqlCommand.

The following code was working in 2.2 but as stated, I need to get rid of ExecuteSqlCommand:

SqlParameter srcid = new SqlParameter("@srcCharacterId", participantApplication.CharacterId);
SqlParameter newid = new SqlParameter("@newCharacterId", newCharacterId);
SqlParameter pResult = new SqlParameter
{
    ParameterName = "@pResult",
    SqlDbType = System.Data.SqlDbType.Bit,
    Direction = System.Data.ParameterDirection.Output
};

_db.Database.ExecuteSqlCommand("pCharacterCopy @srcCharacterId, @newCharacterId, @pResult OUTPUT", srcid, newid, pResult);

I've tried changing the call to ExecuteSqlRaw (leaving everything else identical) but that, although it compiles, throws the following exception at run time:

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

I've checked with the debugger and none of the SqlParameter are null. I suspect my call to ExecuteSqlRaw is not formatted correctly but I cannot find any examples other than integrating calls into Linq queries which I don't need to do. I just want to fire off the call to the stored procedure and take a look at the output parameter when it is done.


回答1:


This is most likely due to the switch to Microsoft.Data.SqlClient.

Remove the reference to the System.Data.SqlClient assembly and replace the namespaces.

using System.Data.SqlClient; => using Microsoft.Data.SqlClient;



来源:https://stackoverflow.com/questions/59655923/execute-stored-procedure-in-ef-core-3-0-vs-2-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!