-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Allow complex types in base types in TPC and unmapped base types in TPT #35025
Comments
Not Smaller reprousing Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
await using var context = new MyDbContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
await context
.Set<RealEvent>()
.Where(e => e.Knowledge.To == null)
.ToListAsync();
public abstract class EventBase
{
protected EventBase(int id)
{
Id = id;
}
public int Id { get; set; }
public Period Knowledge { get; set; }
}
public class RealEvent : EventBase
{
public RealEvent(int id) : base(id)
{ }
}
public class Period
{
public DateTimeOffset From { get; set; }
public DateTimeOffset? To { get; set; }
}
public class MyDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.EnableDetailedErrors()
.EnableSensitiveDataLogging()
.UseSqlite()
.LogTo(Console.WriteLine, LogLevel.Information);
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<EventBase>(builder =>
{
builder.ComplexProperty(e => e.Knowledge);
builder.UseTpcMappingStrategy();
});
modelBuilder.Entity<RealEvent>();
}
} The table created is only: CREATE TABLE "RealEvent" (
"Id" INTEGER NOT NULL CONSTRAINT "PK_RealEvent" PRIMARY KEY
); Also, query throws exception:
|
When trying to bind complex property we are looking for table mapping so that we know which table/columns to bind. However the table mapping is missing here - We build those as part of RelationalModel.Create. Here, the complex type is defined on the abstract base type, and we skip those when creating table mappings. When processing the derived type we only look at declared complex types and so we miss the type defined on the base. As a result the complex property ends up with no table mapping |
Hi, Actually this is blocking us. We have a rather huge (potential) project by a large company, but we need an example that is production ready. The expectations for this in August 2025. If it is successful, it will be one of our biggest projects ever. TPH mapping is not very suitable for this:
TPT is too slow, because of the huge number of abstract subclasses. |
@dvdwouwe There are two workarounds that you can consider using for now:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<EventBase>(builder =>
{
builder.Ignore(e => e.Knowledge);
builder.UseTpcMappingStrategy();
});
modelBuilder.Entity<EventWithName>(builder =>
{
builder.ComplexProperty(e => e.Knowledge);
});
modelBuilder.Entity<EventWithPartnerType1>(builder =>
{
builder.ComplexProperty(e => e.Knowledge);
});
modelBuilder.Entity<EventWithPartnerType2>(builder =>
{
builder.ComplexProperty(e => e.Knowledge);
});
} |
Thanks for the suggestions. I have already experimented with both approaches:
Both workarounds present the same significant issue:
The only solution (workaround) that works for me is using TPH (Table Per Hierarchy). With TPH, the complex properties are recognized correctly, and I can use polymorphic queries. |
You are right, for owned types this is tracked by #32028 |
@cincuranet for the query part, assigning to you as a complex type query issue; I recommend looking into this together with #35392. |
Include your code
The combination of inheritance mapping and the use of Complex types, gives strange behavior in TPC mapping.
I tried TPH and TPT (line 118), and there everything works as expected.
What I see is:
See https://github.com/dvdwouwe/danny-playground-ef/tree/main for reproducing the behavior we see.
Commit SHA: d194a6f9a7a783a5579a03ed9ceb764955e0dd2d
Include provider and version information
EF Core version: 8.0.10
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 8.0.403
Operating system: Windows 11
IDE: Rider
The text was updated successfully, but these errors were encountered: