Repository navigation
Closed
Milestone
Description
Steps to reproduce
public class Budget
{
public Guid Id { get; set; }
public Guid? DescriptionTextId { get; set; }
public decimal PostCost { get; set; }
public string PostNr { get; set; }
public decimal PostPrice { get; set; }
public decimal Quantity { get; set; }
public decimal UnitCost { get; set; }
public string UnitName { get; set; }
public decimal UnitPrice { get; set; }
public decimal VatAmount { get; set; }
public decimal VatBasis { get; set; }
public decimal VatFreeAmount { get; set; }
public decimal VatFreePercent { get; set; }
public decimal VatIncluded { get; set; }
public decimal VatRate { get; set; }
public Text DescriptionText { get; set; }
}
public class Text
{
public Guid Id { get; set; }
public string Plain { get; set; }
public string Rtf { get; set; }
public ICollection<Budget> BudgetDescriptionText { get; set; } = new List<Budget>();
}
public class DataContext : DbContext
{
public DataContext(DbContextOptions options)
: base(options)
{
}
public DbSet<Budget> Budget => Set<Budget>();
public DbSet<Text> Text => Set<Text>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Budget>(entity =>
{
entity.HasKey(e => e.Id);
entity.Property(e => e.Id).ValueGeneratedNever();
entity.Property(e => e.DescriptionTextId);
entity.Property(e => e.PostCost);
entity.Property(e => e.PostNr).IsRequired().HasMaxLength(1000);
entity.Property(e => e.PostPrice);
entity.Property(e => e.Quantity);
entity.Property(e => e.UnitCost);
entity.Property(e => e.UnitName).HasMaxLength(100);
entity.Property(e => e.UnitPrice);
entity.Property(e => e.VatAmount);
entity.Property(e => e.VatBasis);
entity.Property(e => e.VatFreeAmount);
entity.Property(e => e.VatFreePercent);
entity.Property(e => e.VatIncluded);
entity.Property(e => e.VatRate);
entity.HasOne(e => e.DescriptionText)
.WithMany(e => e.BudgetDescriptionText).HasForeignKey(e => e.DescriptionTextId);
});
modelBuilder.Entity<Text>(entity =>
{
entity.HasKey(e => e.Id);
entity.Property(e => e.Id).ValueGeneratedNever();
entity.Property(e => e.Plain);
entity.Property(e => e.Rtf);
});
}
}
The issue
We expect that the following query returns values for two columns
var result = context.Budget
.Select(p => new
{
p.PostCost,
p.DescriptionText.Plain
})
.ToArray();
but in reality, EF generates SQL query that returns all columns.
SELECT
[p].[Id],
[p].[DescriptionTextId],
[p].[PostCost],
[p].[PostNr],
[p].[PostPrice],
[p].[Quantity],
[p].[UnitCost],
[p].[UnitName],
[p].[UnitPrice],
[p].[VatAmount],
[p].[VatBasis],
[p].[VatFreeAmount],
[p].[VatFreePercent],
[p].[VatIncluded],
[p].[VatRate],
[p.DescriptionText].[Id],
[p.DescriptionText].[Plain],
[p.DescriptionText].[Rtf]
FROM
[Budget] AS [p]
LEFT JOIN [Text] AS [p.DescriptionText] ON [p].[DescriptionTextId] = [p.DescriptionText].[Id]
ORDER BY
[p].[DescriptionTextId]
Further technical details
EF Core version: 1.0.1
Operating system: Windows 10
Visual Studio version: VS 2015
Activity
smitpatel commentedon Sep 30, 2016
This is due to requiring materialization of both the sides in group join.
DescriptionTextis optional navigation hence it will generate a group join query. In group join query changes in the outer elements need to be recorded (bug #6360). This was fixed in #6387 by pulling all columns to figure out the changes in outer element.rowanmiller commentedon Oct 3, 2016
@maumar to check if we already have work items covering this
maumar commentedon Oct 5, 2016
Another commit related to this issue: e514b49 (fix to #4858)
[-]Left Join causes unnecessary data pulling[/-][+]Left Join (GroupJoin) always materializes elements resulting in unnecessary data pulling[/+]tuespetre commentedon Feb 3, 2017
#7543 covers this.
Add support for translating GROUP BY and OUTER APPLY; optimize transl…
18 remaining items
Fix to #6647 - Left Join (GroupJoin) always materializes elements res…
Fix to #6647 - Left Join (GroupJoin) always materializes elements res…
Fix to #6647 - Left Join (GroupJoin) always materializes elements res…
maumar commentedon Mar 15, 2017
Fixed in 03a990c
[-]Left Join (GroupJoin) always materializes elements resulting in unnecessary data pulling[/-][+]Query: Left Join (GroupJoin) always materializes elements resulting in unnecessary data pulling[/+]