SqlParameterCollection仅接受非null参数类型的对象 [英] The SqlParameterCollection only accepts non-null Parameter type objects

查看:49
本文介绍了SqlParameterCollection仅接受非null参数类型的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use stored procedures in Entity Framework Core. When executing a stored procedure, I am passing two input parameters and one output parameter. I keep getting this error:

SqlParameterCollection仅接受非null的SqlParameter类型对象,而不接受SqlParameter对象.

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

这是我的代码:

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语句中放置了一个断点,并且发生了以上错误.

I will put a a meaningful catch statement, but right now, I am putting a breakpoints in catch statement and the above error occurs.

任何帮助将不胜感激.

推荐答案

您的问题很可能是由于

Most likely your issues is caused due to a breaking change in EF Core 3, by declaring using System.Data.SqlClient when it should beusing Microsoft.Data.SqlClient

为什么

Microsoft.Data.SqlClient 是SQL的旗舰数据访问驱动程序服务器向前发展, System.Data.SqlClient 不再是焦点发展.一些重要功能(例如始终加密)是仅在 Microsoft.Data.SqlClient 上可用.

Microsoft.Data.SqlClient is the flagship data access driver for SQL Server going forward, and System.Data.SqlClient no longer be the focus of development. Some important features, such as Always Encrypted, are only available on Microsoft.Data.SqlClient.

更多

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

If your code takes a direct dependency on System.Data.SqlClient, you must change it to reference Microsoft.Data.SqlClient instead; as the two packages maintain a very high degree of API compatibility, this should only be a simple package and namespace change.

github上的相关问题

这篇关于SqlParameterCollection仅接受非null参数类型的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆