简体   繁体   English

System.InvalidCastException:“SqlParameterCollection 只接受非空 SqlParameter 类型对象,不接受 SqlParameter 对象。”

[英]System.InvalidCastException: 'The SqlParameterCollection only accepts non-null SqlParameter type objects, not SqlParameter objects.'

I migrated my project from ASP.NET Core 2.2 to ASP.NET Core 3.0.我将我的项目从 ASP.NET Core 2.2 迁移到 ASP.NET Core 3.0。 Now I get this exception.现在我得到这个例外。 In ASP.NET Core 2.2 it was using FromSql() ;在 ASP.NET Core 2.2 中,它使用的是FromSql() now it is using FromSqlRaw() .现在它正在使用FromSqlRaw() I am calling my procedure using Entity Framework Core.我正在使用 Entity Framework Core 调用我的过程。

SqlParameter Username = new SqlParameter
            {
                ParameterName = "USERNAME",
                SqlDbType = SqlDbType.NVarChar,
                Value = user.Username,
                Direction = ParameterDirection.Input,
                Size = 50
            };

SqlParameter Password = new SqlParameter
            {
                ParameterName = "PASSWORD",
                SqlDbType = SqlDbType.NVarChar,
                Value = user.Password,
                Direction = ParameterDirection.Input,
                Size = 50
            };

SqlParameter msgOut = new SqlParameter
            {
                ParameterName = "MSG",
                SqlDbType = SqlDbType.NVarChar,
                Direction = ParameterDirection.Output,
                Size = 1000
            };

SqlParameter statusOut = new SqlParameter
            {
                ParameterName = "STATUS",
                SqlDbType = SqlDbType.Int,
                Direction = ParameterDirection.Output
            };

var sql = @"EXEC PRC_USERS_LOGIN
                        @USERNAME, 
                        @PASSWORD, 
                        @MSG OUT, 
                        @STATUS OUT";

Users resultUser = new Users();
resultUser = ctx.Users.FromSqlRaw(sql, Username, Password, msgOut, statusOut)
                      .FirstOrDefault();

Fixed by changing using System.Data.SqlClient to using Microsoft.Data.SqlClient通过将使用 System.Data.SqlClient 更改为使用 Microsoft.Data.SqlClient 来修复

https://github.com/aspnet/EntityFrameworkCore/issues/16812#issuecomment-516013245 https://github.com/aspnet/EntityFrameworkCore/issues/16812#issuecomment-516013245

This code worked after replacing此代码在替换后有效

System.Data.SqlClient.SqlParameter

to

Microsoft.Data.SqlClient.SqlParameter

and

FirstOrDefault();

to

ToList();

The reverse is true also: you must use System.Data.SqlClient.SqlParameters with EF 6, not Microsoft.Data.SqlClient.SqlParameters.反之亦然:您必须将 System.Data.SqlClient.SqlParameters 与 EF 6 一起使用,而不是Microsoft.Data.SqlClient.SqlParameters。 Also be sure to use Microsoft.SqlServer.Server.SqlDataRecords, not Microsoft.Data.SqlClient.Server.SqlDataRecords.还要确保使用 Microsoft.SqlServer.Server.SqlDataRecords,而不是 Microsoft.Data.SqlClient.Server.SqlDataRecords。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SqlParameterCollection仅接受非空的SqlParameter类型对象。 参数名称:值 - The SqlParameterCollection only accepts non-null SqlParameter type objects. Parameter name: value SqlParameterCollection 只接受非空的 SqlParameter 类型对象,而不接受 Microsoft.Data.SqlClient 中的 SqlParameter 对象 - The SqlParameterCollection only accepts non-null SqlParameter type objects, not SqlParameter objects in Microsoft.Data.SqlClient SqlParameterCollection只接受非null的SqlParameter类型对象,而不接受DBNull对象 - SqlParameterCollection only accepts non-null SqlParameter type objects, not DBNull objects sqlparametercollection 只接受非空的 sqlparameter 类型对象,而不是 byte[] 对象 - the sqlparametercollection only accepts non-null sqlparameter type objects not byte[] objects 错误:SqlParameterCollection仅接受非null的SqlParameter类型对象,而不接受String对象 - Error : The SqlParameterCollection only accepts non-null SqlParameter type objects, not String objects SqlParameterCollection仅接受非null SqlParameter类型的对象,不接受DBNull对象 - The SqlParameterCollection only accepts non-null SqlParameter type objects, not DBNull objects “SqlParameterCollection 只接受非空的 SqlParameter 类型对象,不接受 String 对象” - “SqlParameterCollection only accepts non-null SqlParameter type objects, not String objects” Database.ExecuteSqlRaw - 只接受非空的 SqlParameter 类型对象 - Database.ExecuteSqlRaw - only accepts non-null SqlParameter type objects SqlParameterCollection 只接受非空的 Parameter 类型对象 - The SqlParameterCollection only accepts non-null Parameter type objects SqlParameterCollection仅接受非空的SqlParamter类型对象,而不接受String对象 - The SqlParameterCollection only accepts non-null SqlParamter type objects, not String objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM