[英]ASP.NET Entity Framework SqlQuery not working
我有一個SQL查詢,當我在SQL Server中運行它時,它按預期正常工作。 現在我想將此查詢與Entity Framework一起使用,如下所示:
ViewBag.TimeSlots = dbTimeSlots.Data.SqlQuery("SELECT a.id, concat(a.dateSlot, ' - ', a.timeSlot) as dateTimeSlot, sum(IIF(b.dateSlot is null,0,1)) as counter FROM VIP_Preview_TimeSlots as a LEFT OUTER JOIN [CP-VIP-Preview] as b ON a.dateSlot = b.dateSlot AND a.timeSlot = b.timeSlot GROUP BY a.timeSlot, a.dateSlot, a.[order], a.id Having sum(IIF(b.dateSlot is null,0,1)) < 30 ORDER BY a.[order]").ToList();
但是,當我運行它時,我收到此錯誤:
The data reader is incompatible with the specified ‘CP.Models.VIP_Preview_TimeSlots'. A member of the type, 'timeSlot', does not have a corresponding column in the data reader with the same name.
這是我的班級:
public class VIP_Preview_TimeSlots
{
public int id { get; set; }
[DisplayName("Time Slots")]
public string timeSlot { get; set; }
[DisplayName("Date Slots")]
public string dateSlot { get; set; }
public int order { get; set; }
}
public class VIPPreviewTimeSlots : DbContext
{
public DbSet<VIP_Preview_TimeSlots> Data { get; set; }
}
我真的不知道為什么這不起作用,查詢工作,我不知道為什么實體框架有它的問題,我該如何解決這個問題?
即使我嘗試一個簡單的查詢:
ViewBag.TimeSlots = dbTimeSlots.Data.SqlQuery("SELECT id, concat(dateSlot, ' - ', timeSlot) as dateTimeSlot FROM VIP_Preview_TimeSlots").ToList();
我犯了同樣的錯誤。
您的SQL查詢不會將dateSlot
和timeSlot
作為單獨的列返回,而是將它們連接到dateSlot + ' - ' + timeSlot
作為dateTimeSlot
。
從中調整SQL查詢
concat(a.dateSlot, ' - ', a.timeSlot) as dateTimeSlot
至:
a.dateSlot, a.timeSlot
要么
刪除timeSlot
和dateSlot
屬性,並將其替換為dateTimeSlot
屬性。
你有id
, dateTimeSlot
和counter
作為查詢返回的列,沒有timeSlot
。
實體框架代碼正在查找時間段屬性,這在您的選擇查詢中似乎缺失。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.