Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TPH Mapping: ApplyConfiguration specific order required? #18385

Closed
AEonWho opened this issue Oct 15, 2019 · 4 comments · Fixed by #18559
Closed

TPH Mapping: ApplyConfiguration specific order required? #18385

AEonWho opened this issue Oct 15, 2019 · 4 comments · Fixed by #18559
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@AEonWho
Copy link

AEonWho commented Oct 15, 2019

I'm trying to migrate from efcore 2.2 to 3.0 and ran into a issue with our Entities that have inheritance defined. It seems that now the order in which our mappings are configured is important, and since it worked before, it feels like a bug.

I simplified our structure as much as possible to reproduce the issue.

Steps to reproduce

Entity classes:

public abstract class BaseData
{
    [Key]
    public int Id { get; set; }
}

public abstract class SubBaseData : BaseData
{
}

public class MainData : BaseData
{
}

public class SubData : SubBaseData
{
}

Mappings:

public class BaseDataMap : IEntityTypeConfiguration<BaseData>
{
    public void Configure(EntityTypeBuilder<BaseData> builder)
    {
        builder.HasDiscriminator<int>("TypeDiscriminator")
               .HasValue<MainData>(1)
               .HasValue<SubData>(2);
    }
}

public class MainDataMap : IEntityTypeConfiguration<MainData>
{
    public void Configure(EntityTypeBuilder<MainData> builder)
    {
        // We have properties defined, but it also fails when it's empty.
    }
}

public class SubBaseDataMap : IEntityTypeConfiguration<SubBaseData>
{
    public void Configure(EntityTypeBuilder<SubBaseData> builder)
    {
        // We have properties defined, but it also fails when it's empty.
    }
}

DBContext (this one fails when trying to query DataEntries)

public DbSet<BaseData> DataEntries { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.ApplyConfiguration(new BaseDataMap());
    modelBuilder.ApplyConfiguration(new MainDataMap());
    modelBuilder.ApplyConfiguration(new SubBaseDataMap());
}

Exception:

Unhandled exception. System.InvalidOperationException: The entity type 'SubData' is part of a hierarchy, but does not have a discriminator value configured.

When i switch the order, so that my base mapping with the discriminator definition is last, it works.

modelBuilder.ApplyConfiguration(new MainDataMap());
modelBuilder.ApplyConfiguration(new SubBaseDataMap());
modelBuilder.ApplyConfiguration(new BaseDataMap());

For me it seems that the SubBaseDataMap mapping is somehow overriding the discriminator/inheritance defined in the BaseDataMap.

Here is my demo wich demonstrates this with the InMemory Provider, i also tested it on an local sqlserver instance.
Program.zip

Further technical details

EF Core version: 3.0.0 (current NuGet)
Database provider: Microsoft.EntityFrameworkCore.InMemory, Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.0
Operating system: Windows 10
IDE: Visual Studio 2019 16.3.5

@Rubenisme
Copy link

Thanks for finding out it was the order, we also had this problem and I hadn't tried changing the orders of the mappings, although in another case it worked so it kind of makes sense that it was this.

@ajcvickers ajcvickers added this to the 3.1.0 milestone Oct 18, 2019
@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Oct 24, 2019
@AndriySvyryd AndriySvyryd removed their assignment Oct 24, 2019
@ajcvickers ajcvickers modified the milestones: 3.1.0, 3.1.0-preview2 Oct 24, 2019
@ajcvickers ajcvickers modified the milestones: 3.1.0-preview3, 3.1.0 Dec 2, 2019
@bdaniel7
Copy link

bdaniel7 commented May 3, 2020

Is this fixed for EF Core 3.1.x? This used to work in EF Core 2.2.
I have a bunch of configuration classes that implement IEntityTypeConfiguration<> and are loaded and applied automatically via reflection.
Now I have to sort the configuration files so the base class is applied last?

@ajcvickers
Copy link
Member

@bdaniel7 This was fixed in 3.1.0, as indicated by the milestone.

@bdaniel7
Copy link

bdaniel7 commented May 3, 2020

Ok, I removed the IEntityTypeConfiguration<> for the derived types and then it worked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants