linq2db / linq2db.EntityFrameworkCore

Bring power of Linq To DB to Entity Framework Core projects
MIT License
469 stars 38 forks source link

.ToLinq2Db() after .AsNoTracking() produce same query, but incorrect results #66

Closed Kirill-Maurin closed 1 month ago

Kirill-Maurin commented 4 years ago

Steps:

  1. Use code like Details.Include(d => d.Master).AsNoTracking().ToLinq2Db().ToArray()
  2. Use code like Details.Include(d => d.Master).ToLinq2Db().AsNoTracking().ToArray()

Expected result: resul of 1 == result of 2

Actual result: result of 1 duplicates same master records in memory for all details

Sample: https://github.com/Kirill-Maurin/linq2db.Sample/blob/a8c2561e379163915a4925e5a2aa91228170548f/linq2db.Sample.Tests/IdTests.cs#L140

sdanyliv commented 4 years ago

Any version details? And maybe you can post SQL?

Kirill-Maurin commented 4 years ago

Versions:

     <PackageReference Include="linq2db.EntityFrameworkCore" Version="3.6.0" />
     <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.7" />
     <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.4" />

SQL 1:

info: LinqToDB[49]
      BeforeExecute
      --  PostgreSQL.9.5 PostgreSQL

      SELECT
        t1.id,
        t1.master_id,
        t1.name,
        "a_Master".id,
        "a_Master".name
      FROM
        details t1
                INNER JOIN entitites "a_Master" ON t1.master_id = "a_Master".id

SQL 2:

info: LinqToDB[49]
      BeforeExecute
      --  PostgreSQL.9.5 PostgreSQL

      SELECT
        t1.id,
        t1.master_id,
        t1.name,
        "a_Master".id,
        "a_Master".name
      FROM
        details t1
                INNER JOIN entitites "a_Master" ON t1.master_id = "a_Master".id
sdanyliv commented 4 years ago

@Kirill-Maurin, linq2db does not have ChangeTracker and Identity Management. So it is expected behavior. When you enable Track changes, we use EF Core's Change Tracker, which knows about loaded entities.

MaceWindu commented 1 month ago

This is expected behavior. As[No]Tracking is EF.Core method, which checks that it is applied to efcore's IQueryable and if not - it does nothing. Meaning it does nothing when called after ToLinq2Db call