0

我正在尝试通过以下方式执行长 sql 查询:

IEnumerable<erequest> blah = db.Database.SqlQuery<erequest>().ToList();

为方便起见,查询显示在下方。当我执行查询时,我收到以下错误:

数据读取器与指定的“ enrollDBModel.erequest”不兼容。类型的成员“ er_student_id”在数据读取器中没有同名的对应列。

但是,er_student_idmeber 确实存在于 erequest.cs Context.tt 类中

public int er_student_id { get; set; }

(完整课程另见帖子底部)

询问:

SELECT erequests.er_id, 
       CASE 
         WHEN current_subject_enrollment.count >= subjects.sj_max_enrollment 
       THEN 0 
         WHEN already_passed.count >= 1 THEN 0 
         WHEN is_already_enrolled.count >= 1 THEN 0 
         WHEN failure_times.count >= 3 THEN 0 
         WHEN current_student_enrollment.count >= 4 THEN 0 
         ELSE 1 
       END AS can_enroll, 
       students.st_first_name, 
       students.st_mid_name, 
       students.st_last_name, 
       students.st_student_id, 
       subjects.sj_subject_name, 
       subjects.sj_availability, 
       subjects.sj_max_enrollment, 
       erequests.er_subject_id, 
       erequests.er_reason, 
       erequests.er_status 
FROM   erequests 
       RIGHT JOIN students 
               ON erequests.er_student_id = students.st_student_id 
       RIGHT JOIN subjects 
               ON erequests.er_subject_id = subjects.sj_subject_id 
                  AND subjects.sj_availability = 1 
       LEFT OUTER JOIN (SELECT Count(em_student_id) AS count, 
                               em_subject_id 
                        FROM   enrollment 
                        WHERE  ( em_enrolled = 1 ) 
                        GROUP  BY em_subject_id) AS current_subject_enrollment 
                    ON erequests.er_subject_id = 
                       current_subject_enrollment.em_subject_id 
       LEFT OUTER JOIN (SELECT Count(em_student_id) AS count, 
                               em_student_id, 
                               em_subject_id 
                        FROM   enrollment 
                        WHERE  ( em_enrolled = 1 ) 
                        GROUP  BY em_student_id, 
                                  em_subject_id) AS is_already_enrolled 
                    ON erequests.er_student_id = 
                       is_already_enrolled.em_student_id 
                       AND erequests.er_subject_id = 
                           is_already_enrolled.em_subject_id 
       LEFT OUTER JOIN (SELECT Count(em_student_id) AS count, 
                               em_student_id, 
                               em_subject_id 
                        FROM   enrollment 
                        WHERE  ( em_enrolled = 0 
                                 AND em_result >= 50 ) 
                        GROUP  BY em_student_id, 
                                  em_subject_id) AS already_passed 
                    ON erequests.er_subject_id = already_passed.em_subject_id 
                       AND erequests.er_subject_id = 
                           already_passed.em_subject_id 
       LEFT OUTER JOIN (SELECT Count(em_student_id) AS count, 
                               em_student_id, 
                               em_subject_id 
                        FROM   enrollment 
                        WHERE  ( em_enrolled = 0 
                                 AND em_result < 50 ) 
                        GROUP  BY em_student_id, 
                                  em_subject_id) AS failure_times 
                    ON erequests.er_subject_id = failure_times.em_subject_id 
                       AND erequests.er_subject_id = failure_times.em_subject_id 
       LEFT OUTER JOIN (SELECT Count(em_subject_id) AS count, 
                               em_student_id 
                        FROM   enrollment 
                        WHERE  ( em_enrolled = 1 ) 
                        GROUP  BY em_student_id) AS current_student_enrollment 
                    ON erequests.er_student_id = 
                       current_student_enrollment.em_student_id 
WHERE  ( subjects.sj_availability = 1 ) 
       AND ( erequests.er_status NOT IN ( 'A', 'D' ) ) 

请求.cs:

public partial class erequest
{
    public int er_id { get; set; }

    public int can_enroll { get; set; }

    [Required]
    [Range(1, 100000)]
    [Display(Name = "Subject Id: ")]
    public int er_subject_id { get; set; }

    [Required]
    [Range(1, 100000)]
    [Display(Name = "Student Id: ")]
    public int er_student_id { get; set; }

    [Required]
    [Display(Name = "Reason: ")]
    public string er_reason { get; set; }

    [Required]
    [Display(Name = "Status: ")]
    public string er_status { get; set; }

    public virtual student student { get; set; }
    public virtual subject subject { get; set; }

    public IEnumerable<erequest> processedErequests { get; set; }

    public IEnumerable<erequest> unprocessedErequests { get; set; }
}
4

1 回答 1

0

您以错误的方式解释错误。它说:

 A member of the type, 'er_student_id', does not have a corresponding column in the data reader

它告诉您它在数据读取器中找不到该属性的值。您需要确保您的数据阅读器有一个具有该名称的列(例如,更改您的查询)

于 2013-05-17T17:18:22.200 回答