首页 > 解决方案 > SqlParameterCollection 只接受非空参数类型对象

问题描述

我正在尝试在 Entity Framework Core 中使用存储过程。执行存储过程时,我传递了两个输入参数和一个输出参数。我不断收到此错误:

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

这是我的代码:

public string InsertCardsData(DateTime RecordingStartDate, DateTime RecordingEndDate)
{
        try
        {
            // DateTime DRecEndDate = Convert.ToDateTime(RecordingEndDate);
            DateTime DRecEndDate = RecordingEndDate.AddDays(1);

            var RecordStartDate = new SqlParameter
            {
                ParameterName = "RecordingStartDate",
                Value = RecordingStartDate,
                Direction = ParameterDirection.Input
            };

            var RecordEndDate = new SqlParameter
            {
                ParameterName = "RecordingEndDate",
                Value = DRecEndDate,
                Direction = ParameterDirection.Input
            };

            var RecordCount = new SqlParameter
            {
                ParameterName = "RecLoadCount",
                Direction = ParameterDirection.Output
            };

            var SQL = "Exec Recload_InsertPrimeExtract @RecordingStartDate, @RecordingEndDate, @RecLoadCount OUT";
            var result = _context.Database.ExecuteSqlRaw(SQL, RecordStartDate, RecordEndDate, RecordCount);
            var RecordCountValue = RecordCount.Value.ToString();

            return RecordCountValue;
        }
        catch (Exception ex)
        {
            return "";
        }
}

我将放置一个有意义的 catch 语句,但是现在,我在 catch 语句中放置了一个断点,并且发生了上述错误。

任何帮助将不胜感激。

标签: c#entity-frameworkentity-framework-core

解决方案


您的问题很可能是由于EF Core 3 中的重大更改引起的,通过声明using System.Data.SqlClient应该何时using Microsoft.Data.SqlClient

为什么

Microsoft.Data.SqlClient是 SQL Server 未来的旗舰数据访问驱动程序,System.Data.SqlClient不再是开发重点。某些重要功能(例如始终加密)仅在Microsoft.Data.SqlClient.

此外

如果您的代码直接依赖于System.Data.SqlClient,则必须将其更改为引用Microsoft.Data.SqlClient;由于这两个包保持了非常高的 API 兼容性,这应该只是一个简单的包和命名空间更改。

github上的相关问题


推荐阅读