gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:24:15 25 4
gpt4 key购买 nike

我将我的项目从 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com