简体   繁体   English

无法使用 SqlQuery 从存储过程中获取数据

[英]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:这是我的 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.但是我在GetTopRecommendingUsers()方法的 model 变量中得到一个空列表(Count=0)。

What am I doing wrong?我究竟做错了什么?

Can you show the ConnectString?你能显示 ConnectString 吗? might be I think you access another database in local with same name of your database.可能是我认为您在本地访问另一个与您的数据库名称相同的数据库。

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

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