linq2db / linq2db.EntityFrameworkCore

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

Fix sub-inheritance mapping attributes generation #347

Closed tarasverq closed 9 months ago

tarasverq commented 10 months ago

Hello.

This PR fixes generation of InheritanceMappingAttribute when you have stronger hierarchy than just one child.

Example

Let's pretend we have such class hierarchy:

public class WithInheritance
{
  public int Id { get; set; }
  public string Discriminator { get; set; } = null!;
}

public class WithInheritanceA : WithInheritance { }

public class WithInheritanceA1 : WithInheritanceA { }

public class WithInheritanceA2 : WithInheritanceA { }

Then when you try to get all WithInheritanceA objects linq2db would create this query:

SELECT
  t1."Id",
  t1."Discriminator"
FROM
  "WithInheritance" t1
WHERE
  t1."Discriminator" = 'WithInheritanceA'

But that's wrong: inherited entities WithInheritanceA1 and WithInheritanceA2 also should be included in query.

(t1."Discriminator" = 'WithInheritanceA'  OR
 t1."Discriminator" = 'WithInheritanceA1' OR 
 t1."Discriminator" = 'WithInheritanceA2')


My PR fixes that problem by finding all relations between inherited types and then adding it to collection of mapping attributes. Unit test also included, was tested on PgSql

MaceWindu commented 9 months ago

Thanks!