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

Model configuration: Ignoring base types can leave behind unnecessary discriminator column #7539

Closed
dealdiane opened this issue Feb 2, 2017 · 3 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@dealdiane
Copy link

Suppose we have this model:

public class Cart
{
    public int Id { get; set; }
}

public class CartItem
{
    public Cart Cart { get; set; }

    public int Id { get; set; }
}

public class ExtendedCart : Cart
{
}

public class ExtendedCartItem : CartItem
{
    public ExtendedCart InternalCart { get; set; }
}

and is configured like this:

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

    modelBuilder.Ignore<CartItem>();
    modelBuilder.Entity<ExtendedCartItem>()
        .Ignore(i => i.Cart);

    modelBuilder.Ignore<Cart>();
    modelBuilder.Entity<ExtendedCart>();
}

Running migrations will produce this:

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.CreateTable(
        name: "ExtendedCart",
        columns: table => new
        {
            Id = table.Column<int>(nullable: false)
                .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
            Discriminator = table.Column<string>(nullable: true)
        },
        constraints: table =>
        {
            table.PrimaryKey("PK_ExtendedCart", x => x.Id);
        });

    migrationBuilder.CreateTable(
        name: "ExtendedCartItem",
        columns: table => new
        {
            Id = table.Column<int>(nullable: false)
                .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
            InternalCartId = table.Column<int>(nullable: true)
        },
        constraints: table =>
        {
            table.PrimaryKey("PK_ExtendedCartItem", x => x.Id);
            table.ForeignKey(
                name: "FK_ExtendedCartItem_ExtendedCart_InternalCartId",
                column: x => x.InternalCartId,
                principalTable: "ExtendedCart",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
        });

    migrationBuilder.CreateIndex(
        name: "IX_ExtendedCartItem_InternalCartId",
        table: "ExtendedCartItem",
        column: "InternalCartId");
}

Notice the Discriminator column in the ExtendedCart table. I'm ignoring the Cart type and Cart property of the CartItem type. The Cart type is referenced nowhere. Do we really still need a discriminator column here?

This used to work fine without the need for a discriminator column before 1.1.0.

Further technical details

EF Core version: 1.1.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
IDE: Visual Studio 2015

@rowanmiller rowanmiller added this to the 2.0.0 milestone Feb 10, 2017
@ajcvickers ajcvickers modified the milestones: 2.0.0-preview1, 2.0.0 Apr 19, 2017
@smitpatel
Copy link
Contributor

@AndriySvyryd - Can I poach this?
Solution: Remove discriminator column when no base type & no directly derived type?

@AndriySvyryd
Copy link
Member

@smitpatel You can poach all of my issues, no need to ask upfront.
The proposed solution is correct.

@smitpatel smitpatel assigned smitpatel and unassigned AndriySvyryd Apr 28, 2017
@smitpatel
Copy link
Contributor

This reproduces in 1.1.1 release but works correctly in nightly builds. This seem to be related to #8151 In #8151, Ignored entity type was still base type, throwing exception while setting key in model builder but later gets cleaned up. In this one, perhaps the base type is still set so that DiscriminatorConvention did not remove the discriminator column but the base type was removed later, leaving the extra column behind.

@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label May 5, 2017
@smitpatel smitpatel modified the milestones: 2.0.0-preview1, 2.0.0 May 5, 2017
@smitpatel smitpatel changed the title Unnecessary discriminator column generated Ignoring base types can leave behind unnecessary discriminator column May 5, 2017
@ajcvickers ajcvickers changed the title Ignoring base types can leave behind unnecessary discriminator column Model configuration: Ignoring base types can leave behind unnecessary discriminator column May 9, 2017
@divega divega added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. and removed closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. labels May 10, 2017
@ajcvickers ajcvickers modified the milestones: 2.0.0-preview1, 2.0.0 Oct 15, 2022
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. type-bug
Projects
None yet
Development

No branches or pull requests

6 participants