What is a Discriminator column in ASP.NET Migrations?

I needed to add an extra field to Role identity table in ASP.NET MVC 5.

I use migrations.

I have added an extension to role like:

public class ApplicationRole : IdentityRole
{
    public ApplicationRole() : base() { }

    public ApplicationRole(string name)
        : base(name)
    {
    }

    public virtual Project Project { get; set; }
}

My migration class, I’m getting is:

public partial class ProjectToIdentity : DbMigration
{
    public override void Up()
    {
        CreateTable(
            "dbo.Projects",
            c => new
                {
                    ID = c.Int(nullable: false, identity: true),
                    ProjectName = c.String(maxLength: 100),
                })
            .PrimaryKey(t => t.ID);

        AddColumn("dbo.AspNetRoles", "Discriminator", c => c.String(nullable: false, maxLength: 128));
        AddColumn("dbo.AspNetRoles", "Project_ID", c => c.Int());
        CreateIndex("dbo.AspNetRoles", "Project_ID");
        AddForeignKey("dbo.AspNetRoles", "Project_ID", "dbo.Projects", "ID");
    }

    public override void Down()
    {
        DropForeignKey("dbo.AspNetRoles", "Project_ID", "dbo.Projects");
        DropIndex("dbo.AspNetRoles", new[] { "Project_ID" });
        DropColumn("dbo.AspNetRoles", "Project_ID");
        DropColumn("dbo.AspNetRoles", "Discriminator");
        DropTable("dbo.Projects");
    }
}

The question is – what is a Discriminator column? I have no such column in my model. Why does migration tool adds this field and what aim does it have?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Well, a quick answer to understand, or, at least, make it more clear.

As Jasen told, you can read about Table per Hierarchy (TPH) at http://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph or any other links. But, to tell the truth, it’s not so easy to understand from the first time.

Here is a quick answer:

  1. Try to create a new role, using ApplicationRole (the class, that was posted in question), inherited from IdentityRole
  2. After you create a new role, take a look on Discrimination fields.

As you’ll see – the new record contains “ApplicationRole” in the Discrimination column. So to say – that column contains the name of the new class, that inherits IdentityRole.
So, there probably can be more classes, which will be inheriting IdentityRole, but for each record the Identity system will store the value – using which class the record was created.

enter image description here

As shown, ApplicationRole Discriminator appeared only for the record, created by the class, called ApplicationRole that inherits IdentityRole.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x