Skip to content

Owned type not working when referenced from entity with inheritance #31749

@JoasE

Description

@JoasE

File a bug

Having an owned type property on a base type is not recognized.
The generated migration does not contain a column for the property of the owned type.

Include your code

Repo: https://github.com/JoasE/EfInheritanceOwnedType

Consider the following model.

public abstract class BaseType
{
    public BaseType(OwnedType ownedType)
    {
        OwnedType = ownedType;
    }

    protected BaseType(){}

    public Guid Id { get; } = Guid.NewGuid();
    public OwnedType OwnedType { get; }
}

public class SubTypeOne : BaseType
{
    public SubTypeOne(OwnedType ownedType) : base(ownedType) {}
    private SubTypeOne() {}
}

public class SubTypeTwo : BaseType
{
    public SubTypeTwo(OwnedType ownedType) : base(ownedType) {}
    private SubTypeTwo() {}
}


public class OwnedType
{
    public OwnedType(Guid id)
    {
        Id = id;
    }
    public Guid Id { get; }
}

Using the following configuration:

modelBuilder.Owned<OwnedType>();
modelBuilder.Entity<BaseType>(baseType =>
{
    baseType.UseTpcMappingStrategy();

    baseType.Property(x => x.Id);
    baseType.OwnsOne(x => x.OwnedType, cfg => cfg.Property(x => x.Id));
});

I would expect the generated migration to include the OwnedType Id property for the SubTypeOne and SubTypeTwo tables, but they do not.

migrationBuilder.CreateTable(
    name: "SubTypeOnes",
    columns: table => new
    {
        Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false)
    },
    constraints: table =>
    {
        table.PrimaryKey("PK_SubTypeOnes", x => x.Id);
    });

migrationBuilder.CreateTable(
    name: "SubTypeTwos",
    columns: table => new
    {
        Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false)
    },
    constraints: table =>
    {
        table.PrimaryKey("PK_SubTypeTwos", x => x.Id);
    });

I've also tried to rename the property on the owned type to something else and even giving that property a setter.

Include verbose output

Using project 'C:\Users\joase\source\repos\EfInheritanceOwnedType\EfInheritanceOwnedType\EfInheritanceOwnedType.csproj'.
Using startup project 'C:\Users\joase\source\repos\EfInheritanceOwnedType\EfInheritanceOwnedType\EfInheritanceOwnedType.csproj'.
Writing 'C:\Users\joase\source\repos\EfInheritanceOwnedType\EfInheritanceOwnedType\obj\EfInheritanceOwnedType.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\joase\AppData\Local\Temp\tmpg0lmin.tmp /verbosity:quiet /nologo C:\Users\joase\source\repos\EfInheritanceOwnedType\EfInheritanceOwnedType\EfInheritanceOwnedType.csproj
Writing 'C:\Users\joase\source\repos\EfInheritanceOwnedType\EfInheritanceOwnedType\obj\EfInheritanceOwnedType.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\joase\AppData\Local\Temp\tmprwv3jw.tmp /verbosity:quiet /nologo C:\Users\joase\source\repos\EfInheritanceOwnedType\EfInheritanceOwnedType\EfInheritanceOwnedType.csproj
Build started...
dotnet build C:\Users\joase\source\repos\EfInheritanceOwnedType\EfInheritanceOwnedType\EfInheritanceOwnedType.csproj /verbosity:quiet /nologo /p:PublishAot=false
C:\Users\joase\source\repos\EfInheritanceOwnedType\EfInheritanceOwnedType\Program.cs(14,15): warning CS8618: Non-nullable property 'OwnedType' must contain a non-null value when exiting constructor. Consider declaring the property as nullable. [C:\Users\joase\source\repos\EfInheritanceOwnedType\EfInheritanceOwnedType\EfInheritanceOwnedType.csproj]

Build succeeded.

C:\Users\joase\source\repos\EfInheritanceOwnedType\EfInheritanceOwnedType\Program.cs(14,15): warning CS8618: Non-nullable property 'OwnedType' must contain a non-null value when exiting constructor. Consider declaring the property as nullable. [C:\Users\joase\source\repos\EfInheritanceOwnedType\EfInheritanceOwnedType\EfInheritanceOwnedType.csproj]
    1 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.94
Build succeeded.
dotnet exec --depsfile C:\Users\joase\source\repos\EfInheritanceOwnedType\EfInheritanceOwnedType\bin\Debug\net8.0\EfInheritanceOwnedType.deps.json --additionalprobingpath C:\Users\joase\.nuget\packages --additionalprobingpath "C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages" --runtimeconfig C:\Users\joase\source\repos\EfInheritanceOwnedType\EfInheritanceOwnedType\bin\Debug\net8.0\EfInheritanceOwnedType.runtimeconfig.json C:\Users\joase\.nuget\packages\dotnet-ef\8.0.0-rc.1.23419.6\tools\net8.0\any\tools\netcoreapp2.0\any\ef.dll migrations add Initial --assembly C:\Users\joase\source\repos\EfInheritanceOwnedType\EfInheritanceOwnedType\bin\Debug\net8.0\EfInheritanceOwnedType.dll --project C:\Users\joase\source\repos\EfInheritanceOwnedType\EfInheritanceOwnedType\EfInheritanceOwnedType.csproj --startup-assembly C:\Users\joase\source\repos\EfInheritanceOwnedType\EfInheritanceOwnedType\bin\Debug\net8.0\EfInheritanceOwnedType.dll --startup-project C:\Users\joase\source\repos\EfInheritanceOwnedType\EfInheritanceOwnedType\EfInheritanceOwnedType.csproj --project-dir C:\Users\joase\source\repos\EfInheritanceOwnedType\EfInheritanceOwnedType\ --root-namespace EfInheritanceOwnedType --language C# --framework net8.0 --nullable --working-dir C:\Users\joase\source\repos\EfInheritanceOwnedType\EfInheritanceOwnedType --verbose
Using assembly 'EfInheritanceOwnedType'.
Using startup assembly 'EfInheritanceOwnedType'.
Using application base 'C:\Users\joase\source\repos\EfInheritanceOwnedType\EfInheritanceOwnedType\bin\Debug\net8.0'.
Using working directory 'C:\Users\joase\source\repos\EfInheritanceOwnedType\EfInheritanceOwnedType'.
Using root namespace 'EfInheritanceOwnedType'.
Using project directory 'C:\Users\joase\source\repos\EfInheritanceOwnedType\EfInheritanceOwnedType\'.
Remaining arguments: .
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider in assembly 'EfInheritanceOwnedType'...
Finding Microsoft.Extensions.Hosting service provider...
No static method 'CreateHostBuilder(string[])' was found on class 'Program'.
No application service provider was found.
Finding DbContext classes in the project...
Found DbContext 'TestDbContext'.
Using context 'TestDbContext'.
Finding design-time services referenced by assembly 'EfInheritanceOwnedType'...
Finding design-time services referenced by assembly 'EfInheritanceOwnedType'...
No referenced design-time services were found.
Finding design-time services for provider 'Microsoft.EntityFrameworkCore.SqlServer'...
Using design-time services from provider 'Microsoft.EntityFrameworkCore.SqlServer'.
Finding IDesignTimeServices implementations in assembly 'EfInheritanceOwnedType'...
No design-time services were found.
The property 'OwnedType.BaseTypeId' was created in shadow state because there are no eligible CLR members with a matching name.
Writing migration to 'C:\Users\joase\source\repos\EfInheritanceOwnedType\EfInheritanceOwnedType\Migrations\20230915090151_Initial.cs'.
Writing model snapshot to 'C:\Users\joase\source\repos\EfInheritanceOwnedType\EfInheritanceOwnedType\Migrations\TestDbContextModelSnapshot.cs'.
'TestDbContext' disposed.
Done. To undo this action, use 'ef migrations remove'

Include provider and version information

EF Core version: 8.0.0-rc.1.23419.6
Database provider: Microsoft.EntityFrameworkCore.SqlServer 8.0.0-rc.1.23419.6
Target framework: .NET 8-RC1
Operating system: Windows 11
IDE: Visual Studio 2022 17.8.0 Preview 2

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions