We are using .NET Core 3.1
, Microsoft.EntityFrameworkCore 3.1.9 and Npgsql.EntityFrameworkCore.PostgreSQL 3.1.4. We have the following database models:
Testrunconfig.cs
[Table("testrunconfig", Schema = "fit")]
public partial class Testrunconfig
{
public Testrunconfig()
{
Testruntestgrp = new HashSet<Testruntestgrp>();
}
[Key]
[Column("id")]
public int Id { get; set; }
[Column("product_id")]
public int ProductId { get; set; }
[Column("configfilename")]
[StringLength(512)]
public string Configfilename { get; set; }
[Column("started")]
public DateTime? Started { get; set; }
[Column("finished")]
public DateTime? Finished { get; set; }
[ForeignKey(nameof(ProductId))]
[InverseProperty("Testrunconfig")]
public virtual Product Product { get; set; }
[InverseProperty("Testrunconfig")]
public virtual ICollection<Testruntestgrp> Testruntestgrp { get; set; }
}
Testruntestgrp.cs
[Table("testruntestgrp", Schema = "fit")]
public partial class Testruntestgrp
{
public Testruntestgrp()
{
}
[Key]
[Column("id")]
public int Id { get; set; }
[Column("testrunconfig_id")]
public int? TestrunconfigId { get; set; }
[Column("status")]
[StringLength(30)]
public string Status { get; set; }
[Column("testname")]
[StringLength(256)]
public string Testname { get; set; }
[ForeignKey(nameof(TestrunconfigId))]
[InverseProperty("Testruntestgrp")]
public virtual Testrunconfig Testrunconfig { get; set; }
}
Product.cs
[Table("product", Schema = "fit")]
public partial class Product
{
public Product()
{
Testrunconfig = new HashSet<Testrunconfig>();
}
[Key]
[Column("id")]
public int Id { get; set; }
[Required]
[Column("name")]
[StringLength(128)]
public string Name { get; set; }
[Column("description")]
public string Description { get; set; }
[InverseProperty("Product")]
public virtual ICollection<Testrunconfig> Testrunconfig { get; set; }
}
TestExecutionHelper.cs
helper class:
public class TestExecutionHelper
{
public Testrunconfig TestrunConfig { get; set; }
public bool TestSelected { get; set; }
public string SelectedTestName { get; set; }
public string FinalResult { get; set; }
}
We have the following query in C# code:
private static string GetFinalResult(Testrunconfig trc)
{
if (trc == null)
{
return null;
}
if (trc.Finished == null)
{
return "NOT FINISHED";
}
bool error = trc.Testruntestgrp?.Any(x => x.Status != "OK") ?? true;
return error ? "ERROR" : "OK";
}
private void TestFunction()
{
string configuration = "config_123";
string testName = "test_123";
var test = _fitDbContext.Testrunconfig
.Include(x => x.Testruntestgrp)
.Include(x => x.Product)
.Where(x => x.Configfilename == configuration)
.Where(x => x.Testruntestgrp.Select(y => y.Testname).Contains(testName))
.OrderByDescending(x => x.Id)
.Select(x => new TestExecutionHelper()
{
TestrunConfig = x,
TestSelected = testSelected,
SelectedTestName = testName,
FinalResult = GetFinalResult(x),
})
.ToList();
}
When I call TestFunction()
, the following SQL query is generated:
SELECT t.id, t.product_id, t.configfilename, t.started, t.finished, p.id, p.description, p.name, t0.id, t0.testrunconfig_id, t0.status, t0.testname, t1.id, t1.testrunconfig_id, t1.status, t1.testname
FROM fit.testrunconfig AS t
INNER JOIN fit.product AS p ON t.product_id = p.id
LEFT JOIN fit.testruntestgrp AS t0 ON t.id = t0.testrunconfig_id
LEFT JOIN fit.testruntestgrp AS t1 ON t.id = t1.testrunconfig_id
WHERE (t.configfilename = @__configuration_0) AND @__testName_1 IN (
SELECT t2.testname
FROM fit.testruntestgrp AS t2
WHERE t.id = t2.testrunconfig_id
)
ORDER BY t.id DESC, p.id, t0.id, t1.id
Why is LEFT JOIN fit.testruntestgrp ON t.id
performed twice?
Why is LEFT JOIN fit.testruntestgrp ON t.id performed twice?
- Perhaps this is a flaw in the Npgsql provider. In my personal experience, some EF queries that were processed at lightning speed in Sql Server worked for a very long time in Postgresql due to inefficient generated sql.Npgsql
isn't aware thattrc.Testruntestgrp
collection inGetFinalResult(Testrunconfig trc)
was already populated byInclude(x => x.Testruntestgrp)
insideTestFunction()
. Therefore, it tries to include it again, although it wouldn't need to.Testruntestgrp
navigation property in result - do not include.