I have an ASP.NET MVC site and I want to display some details from a stored procedure. I have done the exact same thing before with other stored procedures on different pages in the same app and it has worked, but this stored procedure call returns a null object instead of the expected value.
EF call to stored procedure:
public MostRecentOrderDetail GetMostRecentOrder(int userId)
{
var context = new myDatabaseContext();
var paramUserId = new SqlParameter { ParameterName = "viUserId", Value = userId };
var result = context.Database.SqlQuery<MostRecentOrderDetail>("usp#GetMostRecentOrder @viUserId", paramUserId).FirstOrDefault();
return result;
}
In my controller I call it using:
MostRecentOrderDetail latestOrder = myDB.GetMostRecentOrder(CurrentUser.UserId).FirstOrDefault();
When I call the stored procedure in SQL Server Management Studio it returns a single row table populated with the correct values, I just cannot seem to get my ASP.NET MVC site to see it.
The MVC site uses code-first, and the MostRecentOrderDetail
object is correctly mapped to the columns of the stored procedure.
*EDIT
I have updated my code as @DanielDrews suggested below, and am now receiving an exception, which I think is a step forward:
The data reader is incompatible with the specified 'MyApp.Models.MostRecentOrderDetail'. A member of the type, 'OrderTypeCode', does not have a corresponding column in the data reader with the same name.
I believe my model is being mapped correctly.
myDatabaseContext.cs
public partial class myDatabaseContext : DbContext
{
// Database Initializer etc
public IDbSet<MostRecentOrderDetail> MostRecentOrderDetails { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// ... Other Maps
modelBuilder.Configurations.Add(new MostRecentOrderMap());
}
// GetMostRecentOrder() function from above
}
MostRecentOrderMap.cs
public class MostRecentOrderMap : EntityTypeConfiguration<MostRecentOrderDetail>
{
public MostRecentOrderMap()
{
// Table and Column Mappings
this.ToTable("usp#GetMostRecentOrder");
this.Property(t => t.OrderTypeCode).HasColumnName("order_type_code");
this.Property(t => t.OrderStatusCode).HasColumnName("order_status_code");
this.Property(t => t.OrderReceivedDate).HasColumnName("order_received_date");
this.Property(t => t.OrderShippedDate).HasColumnName("order_shipped_date");
}
}
MostRecentOrderDetail.cs
public class MostRecentOrderDetail
{
[Key]
public string OrderTypeCode { get; set; }
public string OrderStatusCode { get; set; }
public DateTime OrderReceivedDate { get; set; }
public DateTime OrderShippedDate { get; set; }
}
The stored procedure itself:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[usp#GetMostRecentOrder]
@viUserId int
AS
begin
SELECT TOP 1 o.order_type_code,
o.order_status_code,
o.order_received_date,
o.order_shipped_date
FROM ORDER_TABLE o
WHERE o.user_id = @viUserId
end
This could be a problem with the parameter viUserId (value is correct?), try to initialize like this:
var paramUserId = new SqlParameter { ParameterName = "viUserId", Value = userId };
in my applications i've call procedures a little bit diferent. Translating to your problems should be like that:
var paramUserId = new SqlParameter { ParameterName = "viUserId", Value = userId };
var result = this.DbContext.Database.SqlQuery<MostRecentOrderDetail>("usp#GetMostRecentOrder @viUserId", paramUserId).FirstOrDefault();
And one last thing. be sure they have the same types (mapping and database). this could lead to an exception
*EDIT
The procedure need to return all properties from your model (the same name). procedure returns 'order_type_code' but model expects OrderTypeCode.
There is a typo in your parameter code. Your missing @
in "viUserId"
UserID = new SqlParameter("@viUserId", userId);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.