简体   繁体   English

Entity Framework存储过程返回null对象

[英]Entity Framework stored procedure returns null object

I have an ASP.NET MVC site and I want to display some details from a stored procedure. 我有一个ASP.NET MVC站点,我想从存储过程中显示一些细节。 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: EF调用存储过程:

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. 当我在SQL Server Management Studio中调用存储过程时,它返回一个填充了正确值的单行表,我似乎无法让我的ASP.NET MVC站点看到它。

The MVC site uses code-first, and the MostRecentOrderDetail object is correctly mapped to the columns of the stored procedure. MVC站点使用代码优先,并且MostRecentOrderDetail对象正确映射到存储过程的列。

*EDIT *编辑

I have updated my code as @DanielDrews suggested below, and am now receiving an exception, which I think is a step forward: 我已经更新了我的代码,如下面的@DanielDrews所示,现在我正在收到一个异常,我认为这是向前迈出的一步:

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 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 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 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: 这可能是参数viUserId的问题(值是否正确?),尝试初始化如下:

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. procedure返回'order_type_code'但模型需要OrderTypeCode。

There is a typo in your parameter code. 参数代码中有拼写错误。 Your missing @ in "viUserId" 你在"viUserId"遗失了@

UserID = new SqlParameter("@viUserId", userId);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM