我尝试从数据库第一个 MVC Entity Framework 项目调用 SQL Server 中的存储过程,但返回值始终返回 -1。
我尝试了很多不同的方法来解决这个问题,但到目前为止都没有成功。我在下面列出了我认为是我的相关代码。让我知道是否需要更多代码。感谢任何可以提供帮助的人!
这只是我的测试代码,看看它是否正常工作。
public ActionResult Index()
{
Cust_Entities cse = new Cust_Entities();
if (cse.DoesCustomerNumberExist("1") == 1)
{
return RedirectToAction("Index", "Home");
}
else
{
return RedirectToAction("About", "Home");
}
}
public virtual int DoesCustomerNumberExist(string customer_Number)
{
var customer_NumberParameter = customer_Number != null ?
new ObjectParameter("Customer_Number", customer_Number) :
new ObjectParameter("Customer_Number", typeof(string));
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("DoesCustomerNumberExist", customer_NumberParameter);
}
SQL Server 存储过程代码:
ALTER PROCEDURE [dbo].[DoesCustomerNumberExist]
@Customer_Number VARCHAR(20)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Exists INT
IF EXISTS(SELECT Customer_Number
FROM business_Final.dbo.AR_Customer
WHERE Customer_Number = @Customer_Number)
BEGIN
SET @Exists = '1'
END
ELSE
BEGIN
SET @Exists = '0'
END
RETURN @Exists
END
最佳答案
您正在使用 ExecuteFunction
的非类型化版本。根据文档:
Executes a stored procedure or function that is defined in the data source and expressed in the conceptual model; discards any results returned from the function; and returns the number of rows affected by the execution.
如果未进行任何修改,则通常为 -1。
这是关于 ExecuteFunction
的不同形式的文档: https://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.executefunction(v=vs.110).aspx
关于sql - 存储过程始终返回 -1(asp.net MVC、Entity、SQL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45086610/