c# - System.InvalidCastException : 'The SqlParameterCollection only accepts non-null SqlParameter type objects, not SqlParameter objects.'

标签 c# asp.net asp.net-core asp.net-core-3.0 sqlclient

我将我的项目从 ASP.NET Core 2.2 迁移到 ASP.NET Core 3.0。现在我得到了这个异常(exception)。在 ASP.NET Core 2.2 中,它使用 FromSql() ;现在它正在使用 FromSqlRaw() .我正在使用 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();

最佳答案

通过将使用 System.Data.SqlClient 更改为使用 Microsoft.Data.SqlClient 来修复

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

关于c# - System.InvalidCastException : 'The SqlParameterCollection only accepts non-null SqlParameter type objects, not SqlParameter objects.' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58153228/

相关文章:

c# - 将 int 从 aspx.cs 传递到 aspx 页面

c# - 使用 Moq 测试 View 模型构造函数参数

c# - 对 Flickr API 的签名请求签名无效(在控制台中模拟)

c# - 您可以为 .NET 应用程序禁用系统声音吗?

c# - Linq 帮助使用 "Contains"

当 .NET 4.5 Framework 仅安装在负载平衡器后面的一台服务器中时,ASP.NET EventValidation 失败

c# - 如何通过 docker 使用启动配置文件运行 .net 核心应用程序

c# - Directory.GetCurrentDirectory() 不返回正确的目录

c# - 如何将 Font Awesome 图标与带有服务器控件属性的 ASP.Net 按钮一起使用

c# - 使用 MEF 加载多个插件实例