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

Inheritance: Generated database model contains duplicate base properties #13998

Closed
MaStr11 opened this issue Nov 22, 2018 · 3 comments
Closed
Labels
breaking-change closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-enhancement
Milestone

Comments

@MaStr11
Copy link

MaStr11 commented Nov 22, 2018

Steps to reproduce

I have a class hierarchy with three inheritance levels:

    public abstract class EditorOptionsBase
    {
        public EditorType EditorType { get; private set; }
        public bool ReadOnly { get; set; } = false;
    }

    public abstract class EditorOptionsText : EditorOptionsBase
    {
        public string Mask { get; set; }
    }

    public class EditorOptionsTextBox : EditorOptionsText
    {
    }

    public class EditorOptionsTextArea : EditorOptionsText
    {
        public int? MaxLength { get; set; }
    }

    public class EditorOptionsNumberBox : EditorOptionsText
    {
        public string Format { get; set; }
    }

    public class EditorOptionsCalendar : EditorOptionsBase
    {
    }

The model is configured like this

            var editorOptionsBase = modelBuilder.Entity<EditorOptionsBase>();
            editorOptionsBase.HasDiscriminator<EditorType>(nameof(EditorType)).
                HasValue<EditorOptionsNumberBox>(EditorType.dxNumberBox).
                HasValue<EditorOptionsTextBox>(EditorType.dxTextBox).
                HasValue<EditorOptionsTextArea>(EditorType.dxTextArea).
                HasValue<EditorOptionsCalendar>(EditorType.dxCalendar);

Expected:
There is only one Mask property created in the table:

Mask = table.Column<string>(nullable: true),

Actual:
Three properties are created:

Mask = table.Column<string>(nullable: true),
EditorOptionsTextArea_Mask = table.Column<string>(nullable: true),
EditorOptionsTextBox_Mask = table.Column<string>(nullable: true),

A complete sample project can be found here.

Remark
I was able to configure the model to do the right mapping like so:

modelBuilder.Entity<EditorOptionsTextBox>().Property(eotb => eotb.Mask).HasColumnName(nameof(EditorOptionsText.Mask));

But this doesn't scale because it needs to be repeated for each concrete type.

Further technical details

EF Core version: 2.1.4
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Win 10 x64
IDE: Visual Studio 2017 15.9.1

@ajcvickers
Copy link
Member

Note for triage: If Mask is on the base type, then we get only one column. When it's on a middle type of the hierarchy, it seems like any derived types from this middle type should also only get one column.

@MaStr11 A slightly better workaround is to bulk-update the model at the end of OnModelCreating. Something like:

foreach (var entityType in modelBuilder.Model.GetEntityTypes()
    .Where(e => typeof(EditorOptionsText).IsAssignableFrom(e.ClrType)))
{
    entityType
        .FindProperty(nameof(EditorOptionsText.Mask))
        .Relational()
        .ColumnName = nameof(EditorOptionsText.Mask);
}

@ajcvickers
Copy link
Member

Note from triage: EditorOptionsText is not mapped.

@AndriySvyryd
Copy link
Member

AndriySvyryd commented Dec 18, 2018

Related to #12963 and #10446

@AndriySvyryd AndriySvyryd modified the milestone: 3.0.0 Mar 26, 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 Mar 26, 2019
@AndriySvyryd AndriySvyryd removed their assignment Mar 28, 2019
@ajcvickers ajcvickers modified the milestones: 3.0.0, 3.0.0-preview4 Apr 15, 2019
@ajcvickers ajcvickers modified the milestones: 3.0.0-preview4, 3.0.0 Nov 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-enhancement
Projects
None yet
Development

No branches or pull requests

3 participants