Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TPC returns incorrect items when using generic entities #35727

Open
SpazeDev opened this issue Mar 5, 2025 · 0 comments
Open

TPC returns incorrect items when using generic entities #35727

SpazeDev opened this issue Mar 5, 2025 · 0 comments

Comments

@SpazeDev
Copy link

SpazeDev commented Mar 5, 2025

Bug description

When using TPC with generic entities EF is able to insert rows without a problem but unable to retrieve them correctly.

When running the code EF returns 4 entities of type ReproEntity<int> instead of the added, 2 ReproEntity<int> and 2 ReproEntity<string> entities.
Looking at the generated query shows that EF uses ReproEntity as the Discriminator for both types:

SELECT "r"."Id", "r"."Value", NULL AS "Value0", 'ReproEntity' AS "Discriminator"
FROM "ReproEntity<int>" AS "r"
UNION ALL
SELECT "r0"."Id", NULL AS "Value", "r0"."Value" AS "Value0", 'ReproEntity' AS "Discriminator"
FROM "ReproEntity<string>" AS "r0"

Your code

using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;

namespace Repro;

internal class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("### Databse Setup");
        using (var dbContext = new ReproDbContext())
        {
            dbContext.Database.EnsureDeleted();
            dbContext.Database.EnsureCreated();

            dbContext.Set<BaseEntity>().AddRange([
                new ReproEntity<int>() { Id = 1, Value = 1 },
                new ReproEntity<int>() { Id = 2, Value = 2 },
                new ReproEntity<string>() { Id = 3, Value = "3" },
                new ReproEntity<string>() { Id = 4, Value = "4" },
            ]);
            dbContext.SaveChanges();
        }

        Console.WriteLine("### Select Query");
        using (var dbContext = new ReproDbContext())
        {
            var entities = dbContext.Set<BaseEntity>().ToList();

            foreach (var entity in entities)
            {
                Console.WriteLine($"{entity.Id} - {entity.GetType().FullName}");
            }
        }
    }
}

public class ReproDbContext : DbContext
{
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<BaseEntity>()
            .UseTpcMappingStrategy()
            .HasKey(x => x.Id);

        modelBuilder.Entity<ReproEntity<int>>();
        modelBuilder.Entity<ReproEntity<string>>();
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder
        .UseSqlite("Data Source=Repro.db")
        .LogTo(
            (e, l) => e.Id == RelationalEventId.CommandExecuting,
            e => Console.WriteLine(((CommandEventData)e).Command.CommandText));
}

public abstract class BaseEntity
{
    public int Id { get; set; }
}

public class ReproEntity<T> : BaseEntity
{
    public T Value { get; set; }
}

Stack traces


Verbose output


EF Core version

9.0.2

Database provider

Microsoft.EntityFrameworkCore.Sqlite

Target framework

net9.0

Operating system

Windows 10

IDE

Visual Studio 2022 17.12.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants