简体   繁体   中英

Cannot get data from stored procedure with SqlQuery

This is the stored procedure:

public partial class AddedSP: DbMigration
{
    public override void Up()
    {
        CreateStoredProcedure("dbo.GetTopRecommendingUsers"
          // These are stored procedure parameters
          ,null,
          // Here is the stored procedure body
          @" SET NOCOUNT ON;
             BEGIN
             SELECT promoter.Id, promoter.Email, promoter.PhoneNumber, promoter.UserName, promoter.FirstName, promoter.LocationId, promoter.RegionId, promoter.ProfilePhotoId, promoter.LastName, COUNT(u.RecommendedById) AS Recommendations
             FROM AspNetUsers AS promoter 
             INNER JOIN AspNetUsers AS u ON u.RecommendedById = promoter.Id
             GROUP BY promoter.Id, promoter.Email, promoter.PhoneNumber,promoter.RegionId, promoter.UserName, promoter.LocationId, promoter.ProfilePhotoId, promoter.FirstName, promoter.LastName
             ORDER BY COUNT(u.RecommendedById) DESC
             END");
    }
    public override void Down()
    {
        DropStoredProcedure("dbo.GetTopRecommendingUsers");
    }
} 

This is my class:

public class RecommenderViewModel
{
    public string Id { get; set; }
    public string Email { get; set; }
    public string PhoneNumber { get; set; }
    public string UserName { get; set; }
    public string FirstName { get; set; }
    public string LocationId { get; set; }
    public string RegionId { get; set; }
    public string ProfilePhotoId { get; set; }
    public string LastName { get; set; }
    public int Recommendations { get; set; }
}

And this is the method that makes the call:

    public List<RecommenderViewModel> GetTopRecommendingUsers()
    {
        List<RecommenderViewModel> model = new List<RecommenderViewModel>();
        using (var context = new ApplicationDbContext())
        {
            model = context.Database.SqlQuery<RecommenderViewModel>("GetTopRecommendingUsers").ToList();
        }
        return model;
    }

But I get an empty list (Count=0) in the model variable of my GetTopRecommendingUsers() method.

What am I doing wrong?

Can you show the ConnectString? might be I think you access another database in local with same name of your database.

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.

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