在 EF Core 3.0 与 2.2 中执行存储过程

Ste*_*ank 11 entity-framework-core asp.net-core .net-core-3.1

我正在尝试更新我的代码以适应 EF Core 3.0 中的更改,特别是ExecuteSqlCommand.

以下代码在 2.2 中工作,但如上所述,我需要摆脱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);
Run Code Online (Sandbox Code Playgroud)

我尝试将调用更改为ExecuteSqlRaw(保留其他所有内容相同),但是尽管它可以编译,但在运行时会引发以下异常:

SqlParameterCollection 只接受非空的 SqlParameter 类型对象,不接受 SqlParameter 对象

我已经用调试器检查过,没有一个SqlParameter是空的。我怀疑我的调用ExecuteSqlRaw格式不正确,但除了将调用集成到我不需要做的 Linq 查询之外,我找不到任何示例。我只想触发对存储过程的调用,并在完成后查看输出参数。

Ang*_*nov 14

这很可能是由于切换到 Microsoft.Data.SqlClient。

删除对 System.Data.SqlClient 程序集的引用并替换命名空间。

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