这是我的查询,用于从 SQL 服务器的多个表中获取数据
SELECT
Blob.transactionId,
Blob.status,
COUNT(Bsp._id) AS processed,
Blob.total,
Blob.reason,
(SELECT MAX(MyMaxName)
FROM (VALUES
(MAX(Fail.ProcessTime)),
(MAX(Bsp.ProcessTime))
) MyAlias(MyMaxName)) AS ProcessTime ,
COUNT(DISTINCT Fail.id) AS failed
FROM
BlobStatus AS Blob
LEFT JOIN
BspResponse AS Bsp ON Bsp.tid = Blob.transactionId
LEFT JOIN
FailedResponse AS Fail ON Fail.transactionId = Blob.transactionId
WHERE
Blob.transactionId = "084f4b75-c38e-449c-a9eb-148dce67111a"
GROUP BY
Blob.transactionId, Blob.status, Blob.total, Blob.reason, Blob.failed
在 .NET 代码中,我通过调用下面提到的方法来尝试这种方式。通过参考本教程 https://www.entityframeworktutorial.net/Querying-with-EDM.aspx。实体 SQL
public BlobResponse GetBlobDetailsById(string tid)
{
try
{
string sqlQuery = $"SELECT " +
" Blob.transactionId, Blob.status, " +
"COUNT(Bsp._id) AS processed, Blob.total, " +
" Blob.reason, " +
"(SELECT" +
" MAX(MyMaxName) " +
"FROM(VALUES " +
$"(MAX(Fail.ProcessTime))," +
$" (MAX(Bsp.ProcessTime)) ) " +
"MyAlias(MyMaxName)" +
") as ProcessTime, " +
"COUNT(DISTINCT Fail.id) AS failed " +
"FROM BlobStatus AS Blob " +
"LEFT JOIN BspResponse AS Bsp ON Bsp.tid = Blob.transactionId " +
"LEFT JOIN FailedResponse AS Fail ON Fail.transactionId = Blob.transactionId " +
$"WHERE Blob.transactionId = {tid} " +
" GROUP BY Blob.transactionId, Blob.status, Blob.total, Blob.reason, Blob.failed";
var objctx = ((IObjectContextAdapter)_mriDbContext).ObjectContext;
ObjectQuery<BlobResponse> res = objctx.CreateQuery<BlobResponse>(sqlQuery);
BlobResponse blobResponse = res.First<BlobResponse>();
return blobResponse;
}
catch (Exception e)
{
_logger.LogInformation(e.Message);
return null;
}
}
我收到以下错误
[17:27:43 INF] 无法将“Flash.MultiRecordInquiry.Subscriber.Models.Context.MriDbContext”类型的对象转换为“System.Data.Entity.Infrastructure.IObjectContextAdapter”类型。
这是数据库上下文
public class MriDbContext : DbContext
{
public MriDbContext(DbContextOptions<MriDbContext> options) : base(options)
{ }
public DbSet<BSPReponse> BspResponse { get; set; }
public DbSet<BspStatusDetails> BspStatusDetails { get; set; }
public DbSet<FhaStatusDetails> FhaStatus { get; set; }
public DbSet<BlobStatus> BlobStatus { get; set; }
public DbSet<FailedResponse> FailedResponses { get; set; }
}
我是 EF 新手,不熟悉在实体框架中运行如此复杂的查询。请帮助我如何解决此问题
提前致谢