EF Core owned entity requires a primary key

792 Views Asked by At

Having issues with EF Core 7 requiring a primary key for an owned entity.

I have reviewed the Owned Entity Type document on Microsoft and even their examples require a primary key.

When trying to create the migrations using

dotnet ef migrations add "Project Init"

I get the following error:

The entity type 'SegmentPoint' requires a primary key to be defined

Below is the parent class that has three properties using the same owned class (SegmentPoint)

//BaseDomain contains the Id property
public class Segment : BaseDomain
{
    public string Name { get; private set; }
    public SegmentPoint PointOne { get; private set; }
    public SegmentPoint? PointTwo { get; private set; }
    public SegmentPoint? PointThree { get; private set; }
    
    //Removed
}

Here is the owned class

public class SegmentPoint
{
    public PointType? Type { get; internal set; }
    public string? Label { get; internal set; }
    public string? Format { get; internal set; }
}

Inside the entity configuration for Segment, I have included the OwnsOne for all three properties and a column rename.

I have no entity configuration for SegmentPoint.

internal class SegmentConfig : IEntityTypeConfiguration<Segment>
{
    public void Configure(EntityTypeBuilder<Segment> builder)
    {
        builder.ToTable("ReportSegment");
        builder.HasKey(b => b.Id);
        builder.HasOne(b => b.Report)
            .WithMany(r => r.Segments);

        builder.OwnsOne(
            b => b.PointOne,
            sa =>
            {
                sa.Property(p => p.Label).HasColumnName("PointOneLabel");
                sa.Property(p => p.Format).HasColumnName("PointOneFormat");
                sa.Property(p => p.Type).HasColumnName("PointOneType");
            });

        builder.OwnsOne(b => b.PointTwo,
            sa =>
            {
                sa.Property(p => p.Label).HasColumnName("PointTwoLabel");
                sa.Property(p => p.Format).HasColumnName("PointTwoFormat");
                sa.Property(p => p.Type).HasColumnName("PointTwoType");
            });

        builder.OwnsOne(b => b.PointThree,
            sa =>
            {
                sa.Property(p => p.Label).HasColumnName("PointThreeLabel");
                sa.Property(p => p.Format).HasColumnName("PointThreeFormat");
                sa.Property(p => p.Type).HasColumnName("PointThreeType");
            });
    }
}

Has anyone experienced this before? I have done many owned entity types in other projects without any issues like this.

I have tried removing PointTwo and PointThree from the Segment entity to see if the multiple owned properties to a single class was causing an issue but EF Core still requires a primary key be defined.

1

There are 1 best solutions below

0
Steven Ryssaert On

I just stumbled upon the same issue. For me, the problem was that I had a constructor with Dependency Injection in the EntityTypeConfiguration class. When removing the constructor (and commenting the line needing the injected service) all works as expected.