dotnet / dotnet/efcore

Improve exception message for TPC + Owned Entities Configuration

Open
#32,870 4 comments 0 reactions 0 assignees View on GitHub
area-model-building customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

I've been attempting to use the TPC strategy where a owned entity is configured on the base entity, but I cannot get the migrations to generate the owned entity properties on the derived entities.

I've instead needed to move the `OwnsOne` configuration to each derived entity (`FileSystemDirectory`, `FileSystemFile`) to get the migration to include the `PathInfo` properties. If I do that however, I cannot filter or sort on the base entity as the LINQ expression cannot be translated.

It's something I've been banging my head against a wall at for some time now, as being able to sort and filter on the base class is important, but it cannot be done inside the generated SQL statement as the following fails.

```C#
await database
.Entries
.OrderBy(x => x.PathInfo.Name)
.ToListAsync();
```

I'd ideally want to setup the entities and configuration like below, but it doesn't seem to be supported when the `OwnsOne` is configured on the base class.

```C#
public abstract class FileSystemEntry
{
public int Id { get; set; }

public PathInfo PathInfo { get; set; }

public FileSystemDirectory? ParentDirectory { get; set; }
}

public class FileSystemDirectory : FileSystemEntry
{
}

public class FileSystemFile : FileSystemEntry
{
public long Size { get; set; }
}

public class PathInfo
{
public string Name { get; init; }

public string FullName { get; init; }

public string Extension { get; init; }

public string DirectoryPath { get; init; }
}

public class FileSystemEntryConfiguration : IEntityTypeConfiguration
{
public void Configure(EntityTypeBuilder builder)
{
builder
.UseTpcMappingStrategy();

builder
.OwnsOne(p => p.PathInfo);
}
}
```

Does this issue relate to https://github.com/dotnet/efcore/issues/9630? Or is something missing or being configured incorrectly here?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.