1

这是我的查询,用于从 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 新手,不熟悉在实体框架中运行如此复杂的查询。请帮助我如何解决此问题

提前致谢

4

1 回答 1

0

我希望它可能对其他人有所帮助,就像在看到其他人的许多建议和回复之后一样。我刚刚想通了,有一种改变的方式可以像在 SQL Server 中一样执行原始查询。

例如,您可以通过在 EF 中这样使用来执行原始查询

using (var command = _ctx.Database.GetDbConnection().CreateCommand())
       {
           command.CommandText = "select * from MARK where tid ='123'";
           _ctx.Database.OpenConnection();
           using (var reader = command.ExecuteReader())
                {

                    reader.Read();
                    var firstColumnObject = reader.GetValue(0); 
                }
             _ctx.Database.CloseConnection();
       
        }

于 2020-06-27T18:36:38.250 回答