c# - An entity with an interface property - is there a better approach when using Entity Framework Core? - Stack Overflow

admin2025-04-28  3

I'm currently running into an issue where the structure of my object is not conducive with what is expected by Entity Framework Core (v7), as my model has an interface as a property. I'm wondering if there is a better approach to achieve what I am trying to do.

Here is my (abridged) object structure:

public class ReviewProcess 
{
    public IReviewStageType CurrentStage { get; private set; }
}

public class ManagerReview : IReviewStageType
{
    public string Name { get; private set;}
    // State/actions for manager review
}

public class SeniorManagerReview : IReviewStageType
{
    public string Name { get; private set;}
    // State/actions for senior manager review
}

The different IReviewStageType interfaces define different actions and behaviour that can be executed on the ReviewProcess. However, I am trying to persist this data to a relational database with EF Core.

I thought I could create an IEntityTypeConfiguration where I could map the IReviewStageType.Name to my database, so my ReviewProcess table would look like: [Id, ReviewStageName]. And then set up some logic to convert the Name back to the relevant concrete type (ManagerReview/SeniorManagerReview) on reading back from the database, something like this (abridged):

public void Configure(EntityTypeBuilder<ReviewProcess> builder)
{
    builder.OwnsOne(r => r.CurrentStage, stage =>
    {
        stage.Property(s => s.Name).HasColumnName("ReviewStage_Name");
    });
}

I am getting an error:

The specified type '' must be a non-interface reference type to be used as an entity type

Is there a way to achieve what I am trying to do? Or should I be approaching this from a different perspective, e.g. not with an interface? I'd welcome any suggestions. Thanks!

转载请注明原文地址:http://anycun.com/QandA/1745782190a91218.html