SqlException在传递时期望参数

时间:2015-12-23 00:30:54

标签: c# entity-framework stored-procedures asp.net-mvc-5

我收到SqlException

  

程序或功能' GetDashboardData'期望参数' @ userID',这是未提供的。

我一直试图关注这篇文章,但似乎并不适合传递参数。

https://msdn.microsoft.com/en-us/data/jj691402.aspx然后寻找修复

然后我继续尝试传递一个参数,但抛出了这个异常(如上所示)并用Google搜索。我已经在这篇文章中尝试了第一个答案帖子,因为它似乎是最相关和最快捷的方式

Passing parameter to Stored Procedure using DbContext

然而这没有用,所以我想知道如何传递这个参数?

string userID = User.Identity.GetUserId();

// If using code first we need to make sure the model built before we open a connection
db.Database.Initialize(force: false);
var command = db.Database.Connection.CreateCommand();
command.CommandText = "[dbo].[GetDashboardData]";

var userIDParameter = new SqlParameter { ParameterName = "@userID", Value = userID, DbType = DbType.String, Direction = ParameterDirection.Input };
command.Parameters.Add(userIDParameter);

try 
{
    db.Database.Connection.Open();

    // We now run the stored procedure
    var reader = command.ExecuteReader();

    var getUserDashboards = ((IObjectContextAdapter)db).ObjectContext.Translate<DashboardModel>(reader, "DashboardModel", MergeOption.AppendOnly);
}
finally
{
    db.Database.Connection.Close();
}

0 个答案:

没有答案