C# SqlParameterCollection仅接受非空的SqlParameter类型对象,而不接受Microsoft.Data.SqlClient中的SqlParameter对象

C# SqlParameterCollection仅接受非空的SqlParameter类型对象,而不接受Microsoft.Data.SqlClient中的SqlParameter对象,c#,sql-server,azure,asp.net-core-2.2,C#,Sql Server,Azure,Asp.net Core 2.2,我正在尝试使用其中一个名称空间 //using Microsoft.Data.SqlClient; using System.Data.SqlClient; 我的模型 public class Get_Data_Scholar{ [Key] public Int32 ID_Transcript { get; set; } public string Year_Semester { get; set; } public string Period { get; s

我正在尝试使用其中一个名称空间

//using Microsoft.Data.SqlClient;
using System.Data.SqlClient;
我的模型

public class Get_Data_Scholar{
    [Key]
    public Int32 ID_Transcript { get; set; }
    public string Year_Semester { get; set; }
    public string Period { get; set; }
    public string Status { get; set; }
}
我的控制器

        public JsonResult Get_GPA_Tuition (Int32 id)
        {
            var ID_NCS = new SqlParameter("@ID_NCS", id);
            List<Get_data> get_Data = new List<Models.Get_Data_Scholar>();
            get_Data = _applicationDbContext.Get_Data_Scholar.FromSql(
                            "EXEC [dbo].[Get_Data_Scholar]  @ID"

                            , id
                            ).ToList();

            return Json(get_Data );
        }
public JsonResult获得GPA学费(Int32 id)
{
var ID\u NCS=新的SqlParameter(“@ID\u NCS”,ID);
List get_Data=new List();
get_Data=\u applicationDbContext.get_Data\u Scholar.FromSql(
“EXEC[dbo].[Get_Data_Scholar]@ID”
身份证件
).ToList();
返回Json(获取_数据);
}
当我使用
System.Data.SqlClient
时,一切正常。但是,当我将不同的名称空间更改为Microsoft.Data.SqlClient时,会产生如下错误消息:

System.InvalidCastException HResult=0x80004002消息= SqlParameterCollection仅接受非null SqlParameter类型 对象,而不是SqlParameter对象。Source=System.Data.SqlClient
StackTrace:在 System.Data.SqlClient.SqlParameterCollection.ValidateType(对象 值)在System.Data.SqlClient.SqlParameterCollection.Add(对象 价值)在 Microsoft.EntityFrameworkCore.Storage.Internal.DynamicRelationalParameter.AddDbParameter(DbCommand 命令,对象值)在 Microsoft.EntityFrameworkCore.Storage.Internal.CompositeRelationalParameter.AddDbParameter(DbCommand 命令,对象值)在 Microsoft.EntityFrameworkCore.Storage.Internal.RelationalParameterBase.AddDbParameter(DbCommand 命令,IReadOnlyDictionary
2参数值)位于
Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.CreateCommand(IRelationalConnection
连接,IReadOnlyDictionary
2个参数值) Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection 连接,DbCommandMethod executeMethod,IReadOnlyDictionary
2
参数值)在
Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection
连接,IReadOnlyDictionary
2个参数值) Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable
1.Enumerator.BufferlessMovenText(布尔值
缓冲区)在
Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState
状态,Func
3操作,Func
3验证成功)
Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable
1.Enumerator.MoveNext() 在 Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.d_u17
2.MoveNext()
在
Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor
1.EnumeratorExceptionInterceptor.MoveNext() 在System.Collections.Generic.List中
1.AddEnumerable(IEnumerable
1 可枚举)位于System.Linq.enumerable.ToList[TSource](IEnumerable`1 (来源)

当我使用
System.Data.SqlClient
时,可以绕过SQL server返回的带有
null
值的查询,而不显示任何错误消息。 但当它更改为Microsoft.Data.SqlClient时,来自SQL Server的返回查询必须经过特殊处理。那么,如何处理/克服此错误消息? 我知道这类问题,但是如何处理
非空SqlParameter
因为返回
null


我之所以尝试更改
Microsoft.Data.SqlClient
命名空间,是因为不久的将来,某些敏感列将使用AKV()进行加密。

您可以尝试将SQLRAW更改为
,也可以将
System.Data.SqlClient.SqlParameter
更改为
Microsoft.Data.SqlClient.SqlParameter

 get_Data = _applicationDbContext.Get_Data_Scholar.FromSql(
                        "EXEC [dbo].[Get_Data_Scholar] {0}", 
                         id).ToList();

如果我正确理解了您的问题,那么您的意思是,当您的sql参数的值为文本null时,这会爆炸,您不希望发生这种情况。您需要检测空值,而不是使用System.DBNull.Value。

您可以检查参数是否为空,并根据此检查相应地处理您的情况:
if(ID\u NCS!=null){//your logic}else{//handle null condition}
ID\u NCS永远不会为空,因为它是一个提要参数。执行存储过程后SQL Server返回的查询将为空。您使用的是哪个版本的.NET CORE?它是.NET CORE v 2.2。我认为您应该升级到.NET CORE 3.0。阅读本文了解更多信息:我应该使用什么样的程序集?错误消息:dbset不包含“FromSqlRaw”的定义,并且找不到可访问的扩展方法“FromSqlRaw”,该扩展方法接受类型为“dbset”的第一个参数(是否缺少using指令或程序集引用?)Microsoft.Data.SqlClient,它已安装。或者Microsoft.Data.SqlClient仅适用于.Net Core v.3?请尝试使用Microsoft.EntityFrameworkCore添加;在名称空间的顶部,通过添加此名称空间Microsoft.Data.SqlClient.SqlParameter,不需要使用指令。感谢您的好奇,我的问题是,如何通过使用SystemDBNull.value来检测返回null值?
new SqlParameter(@ID\u NCS,ID==null?DBNull.value:ID)
,尽管在您的示例中ID是不可为null的int,所以我现在明白了,我的答案是相当不相关的>