SqlQuery<T> 在 EF Core 3.1 中等效
SqlQuery<T> equivalent in EF Core 3.1
我正在从 ASP.NET Framwork 4.7 迁移到 Asp .NET Core 3.1,我真的陷入了以下困境:
我有一个 SQL 查询:
string Query="
SELECT I.Id,I.Combokod,I.S_Omr,I.Date,M.Price,M.Name,M.Dmedel,M.Year,M.Miles
FROM dbo.Itemannounces AS I
INNER JOIN dbo.MotorDBs AS M ON I.MotorDBs_Id=M.Id where M.MotorID=1" ;
我的定制class:
public class Fordonlista
{
public int Id { get; set; }
public byte Combokod { get; set; }
public byte Affarsform { get; set; }
public byte Model { get; set; }
public byte Dmedel { get; set; }
public short Year { get; set; }
public int Miles { get; set; }
public short S_Omr { get; set; }
public DateTime Date { get; set; }
public int Price { get; set; }
public string Name { get; set; }
public string Lan { get; set; }
public string Kommun { get; set; }
public byte Typ { get; set; }
}
为了检索我使用的数据:
var result = Context.Database.SqlQuery<Fordonlista>(Query).ToList();
问题是如何在 EF Core 中获取结果?如果 EF Core 重量轻,为什么还要从 Framwork 迁移到降级的 EF Core?
You can use the new EF Core 3 method FromSqlRaw()
for this.
In your example that would then be
string Query="
SELECT I.Id,I.Combokod,I.S_Omr,I.Date,M.Price,M.Name,M.Dmedel,M.Year,M.Miles
FROM dbo.Itemannounces AS I
INNER JOIN dbo.MotorDBs AS M ON I.MotorDBs_Id=M.Id
where M.MotorID=1" ;
var result = Context.Fordonlista.FromSqlRaw(Query);
But even better would be to pass a parameter as following:
int motorId = 1; //or passed in from other variable
var result = Context.Fordonlista.FromSqlInterpolated(
$"SELECT I.Id,I.Combokod,I.S_Omr,I.Date,M.Price,M.Name,M.Dmedel,M.Year,M.Miles
FROM dbo.Itemannounces AS I
INNER JOIN dbo.MotorDBs AS M ON I.MotorDBs_Id=M.Id
WHERE M.MotorID={motorId}";
我正在从 ASP.NET Framwork 4.7 迁移到 Asp .NET Core 3.1,我真的陷入了以下困境:
我有一个 SQL 查询:
string Query="
SELECT I.Id,I.Combokod,I.S_Omr,I.Date,M.Price,M.Name,M.Dmedel,M.Year,M.Miles
FROM dbo.Itemannounces AS I
INNER JOIN dbo.MotorDBs AS M ON I.MotorDBs_Id=M.Id where M.MotorID=1" ;
我的定制class:
public class Fordonlista
{
public int Id { get; set; }
public byte Combokod { get; set; }
public byte Affarsform { get; set; }
public byte Model { get; set; }
public byte Dmedel { get; set; }
public short Year { get; set; }
public int Miles { get; set; }
public short S_Omr { get; set; }
public DateTime Date { get; set; }
public int Price { get; set; }
public string Name { get; set; }
public string Lan { get; set; }
public string Kommun { get; set; }
public byte Typ { get; set; }
}
为了检索我使用的数据:
var result = Context.Database.SqlQuery<Fordonlista>(Query).ToList();
问题是如何在 EF Core 中获取结果?如果 EF Core 重量轻,为什么还要从 Framwork 迁移到降级的 EF Core?
You can use the new EF Core 3 method FromSqlRaw()
for this.
In your example that would then be
string Query="
SELECT I.Id,I.Combokod,I.S_Omr,I.Date,M.Price,M.Name,M.Dmedel,M.Year,M.Miles
FROM dbo.Itemannounces AS I
INNER JOIN dbo.MotorDBs AS M ON I.MotorDBs_Id=M.Id
where M.MotorID=1" ;
var result = Context.Fordonlista.FromSqlRaw(Query);
But even better would be to pass a parameter as following:
int motorId = 1; //or passed in from other variable
var result = Context.Fordonlista.FromSqlInterpolated(
$"SELECT I.Id,I.Combokod,I.S_Omr,I.Date,M.Price,M.Name,M.Dmedel,M.Year,M.Miles
FROM dbo.Itemannounces AS I
INNER JOIN dbo.MotorDBs AS M ON I.MotorDBs_Id=M.Id
WHERE M.MotorID={motorId}";