[英]Use a table-valued function in EF Core 5.0
我正在將 Asp.Net MVC 項目從.Net 2.2 遷移到.Net 5.0。
最后一個絆腳石是讓表值 function 與 EF Core 5 一起使用。
在 EF Core 2.2 中,語法為:
public IQueryable<OutOfService> UnitOutOfService(DateTime startingDate, DateTime endingDate) =>
Query<OutOfService>().FromSql($"SELECT * FROM UnitOutofServiceView(NULL, 1, '1,2,3,4,6', 0, {startingDate}, {endingDate}, 0, 0, 0)");
我只對提供startingDate
和endingDate
參數感興趣。
我能夠通過 controller 中的表加入 TVF:
model = await (
from v in context.Vehicles
join r in context.UnitOutOfService(DateTime.Now.Date, DateTime.Now.Date) on v.ID equals r.ID into loj
from rs in loj.DefaultIfEmpty()
where v.Schedule == true && v.Suspend == false
orderby v.Name
select new IndexViewModel
{
ID = v.ID,
Name = v.Name,
...
ServiceDescription = rs.Description
}
).ToListAsync();
不幸的是,我無法讓它在 EF Core 5.0 中工作。
我在ApplicationDbContext
class 中定義了 TVF:
public IQueryable<OutOfService> UnitOutOfService(string sns, int expectedversion, string Site, bool UnitHistory, DateTime HistoryStartDate, DateTime HistoryDateEnd, int DaysOutMin, bool InServiceUnits, bool NotSowFromRO)
=> FromExpression(() => UnitOutOfService(null, 1, "1,2,3,4,6", false, HistoryStartDate, HistoryDateEnd, 0, false, false));
“默認”,參數值在 controller 中似乎沒有任何影響,所以我再次提供它們:
return await (
from v in _context.Vehicles
join r in _context.UnitOutOfService(DateTime.Now.Date, DateTime.Now.Date) on v.VehicleID equals r.UnitNumber into loj
join r in _context.UnitOutOfService(null, 1, "1,2,3,4,6", false, today, today, 0, false, false) on v.VehicleID equals r.UnitNumber into loj
from rs in loj.DefaultIfEmpty()
orderby v.Name
select new IndexViewModel
{
ID = v.ID,
Name = v.Name,
...
ServiceDescription = rs.Description
}
).ToArrayAsync();
運行控制器的代碼會產生許多錯誤。
我錯過了什么?
這段代碼是真的:
public IQueryable<OutOfService> UnitOutOfService(string sns, int expectedversion, string Site, bool UnitHistory, DateTime HistoryStartDate, DateTime HistoryDateEnd, int DaysOutMin, bool InServiceUnits, bool NotSowFromRO)
=> FromExpression(() => UnitOutOfService(sns, expectedversion, Site, UnitHistory, HistoryStartDate, HistoryDateEnd, DaysOutMin, InServiceUnits, NotSowFromRO);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.