Database.ExecuteSqlRaw - 只接受非空的 SqlParameter 类型对象

Database.ExecuteSqlRaw - only accepts non-null SqlParameter type objects

我尝试调用一个包含如下代码的方法

代码将由过程执行

不幸的是,我运行出错了,我在下面描述了错误

有人知道执行该过程的任何其他解决方案吗?

控制器:


string fromMail = "test";
string toMail= "test";
string title = "test";
string body= "test";


   string sql = @"
                        
                        USE [eFaktura]
                        GO
                        
                        DECLARE @ExtSystem varchar(20)
                        DECLARE @AdresOd varchar(255)
                        DECLARE @AdresDo varchar(255)
                        DECLARE @Temat varchar(255)
                        DECLARE @Tresc varchar(max)
                        DECLARE @Logo bit
                        DECLARE @IDMaila int
                        
                        
                        EXECUTE [dbo].[WstawMail_X] 
                           @ExtSystem = @ParExtSystem
                          ,@AdresOd = @ParAdresOd
                          ,@AdresDo = @ParAdresDo
                          ,@Temat = @ParTemat
                          ,@Tresc = @ParTresc
                          ,@Logo = 0
                          ,@IDMaila = @IDMaila output
                        GO
                        
                        ";
            _context.Database.ExecuteSqlRaw(sql,

                 new SqlParameter("@ParExtSystem", "ECP"),
                 new SqlParameter("@ParAdresOd", fromMail),
                 new SqlParameter("@ParAdresDo", toMail),
                 new SqlParameter("@ParTemat", title),
                 new SqlParameter("@ParTresc", body),
                 new SqlParameter("@ParLogo", 0)

            );

System.InvalidCastException HResult=0x80004002 Message=The SqlParameterCollection only accepts non-null SqlParameter type objects, not > SqlParameter objects. Source=Microsoft.Data.SqlClient StackTrace: at Microsoft.Data.SqlClient.SqlParameterCollection.ValidateType(Object value) at Microsoft.Data.SqlClient.SqlParameterCollection.Add(Object value) at > Microsoft.EntityFrameworkCore.Storage.Internal.RawRelationalParameter.AddDbParameter(DbComma > nd command, Object value)

直接调用存储过程可能更容易:

SqlParameter idm;

_context.Database.ExecuteSqlRaw(
     "[dbo].[WstawMail_X] @pEx, @pAOd, @pADo, @pTe, @pTr, @pL, @pIDM OUTPUT",
     new SqlParameter("@pEx", "ECP"),
     new SqlParameter("@pAOd", fromMail),
     new SqlParameter("@pADo", toMail),
     new SqlParameter("@pTe", title),
     new SqlParameter("@pTR", body),
     new SqlParameter("@pL", 0),
     idm = new SqlParameter("@pIDM", 0)
);
//you can retrieve the value of pIDM here if you want

或者如果你想让生活更整洁,使用插值:

SqlParameter idm = new SqlParameter("@whateverName", 0)
_context.Database.ExecuteSqlInterpolated(
     $"[dbo].[WstawMail_X] 'ECP', {fromMail}, {toMail}, {title}, {body}, '0', {idm} OUTPUT"
);
//retrieve idm.Value here

如果你使用 named 是因为上面的顺序不是这样,或者你只是喜欢使用 named,那么使用 interpolated 会更清楚一些:

_context.Database.ExecuteSqlInterpolated(
     $"[dbo].[WstawMail_X] @ExtSystem='ECP', @ParAdresOd={fromMail}, ..., @IDMaila={idm} OUTPUT"
);