The SqlParameterCollection only accepts non-null SqlParameter type objects, not SqlParameter objects in Microsoft.Data.SqlClient

喜欢而已 提交于 2020-01-16 19:32:29

问题


I'm trying to use one of these namespaces

//using Microsoft.Data.SqlClient;
using System.Data.SqlClient;

My model

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; }
}

My controller

        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 );
        }

When I'm using System.Data.SqlClient, everything works well. But when I change different namespace to Microsoft.Data.SqlClient, it produce error message like this:

System.InvalidCastException HResult=0x80004002 Message=The SqlParameterCollection only accepts non-null SqlParameter type objects, not SqlParameter objects. Source=System.Data.SqlClient
StackTrace: at System.Data.SqlClient.SqlParameterCollection.ValidateType(Object value) at System.Data.SqlClient.SqlParameterCollection.Add(Object value) at Microsoft.EntityFrameworkCore.Storage.Internal.DynamicRelationalParameter.AddDbParameter(DbCommand command, Object value) at Microsoft.EntityFrameworkCore.Storage.Internal.CompositeRelationalParameter.AddDbParameter(DbCommand command, Object value) at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalParameterBase.AddDbParameter(DbCommand command, IReadOnlyDictionary2 parameterValues) at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.CreateCommand(IRelationalConnection connection, IReadOnlyDictionary2 parameterValues) at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary2 parameterValues) at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection connection, IReadOnlyDictionary2 parameterValues) at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable1.Enumerator.BufferlessMoveNext(Boolean buffer) at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func3 operation, Func3 verifySucceeded) at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable1.Enumerator.MoveNext() at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.<_TrackEntities>d__172.MoveNext() at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor1.EnumeratorExceptionInterceptor.MoveNext() at System.Collections.Generic.List1.AddEnumerable(IEnumerable1 enumerable) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

When I'm using System.Data.SqlClient, return query from SQL server with null value can be bypass without any error message. But when it changes to Microsoft.Data.SqlClient, return query from SQL Server must be treated with special treatment. So, How do I do/overcome this error message ? I knew this question kind of old style but how to treat non-null SqlParameterbecause of some return null column ?

The reason I'm trying to change Microsoft.Data.SqlClient namespace because near in the future some sensitive column will be encrypted using AKV (AlwaysEncrypted).


回答1:


If I am understanding your question correctly you're saying that when the value of your sql param is the literal null, this blows up, and you do not want this to happen. You need to detect null and instead use System.DBNull.Value.




回答2:


You can try like change to FromSqlRaw and also change System.Data.SqlClient.SqlParameter to Microsoft.Data.SqlClient.SqlParameter

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


来源:https://stackoverflow.com/questions/59499253/the-sqlparametercollection-only-accepts-non-null-sqlparameter-type-objects-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!