简体   繁体   中英

How to extend IdentityUser with a foreign key

I am trying to extend IdentityUser with a foreign key(BuildingID). Below is my ApplicationUser class which derives from the IdentityUser class. What would be the correct way to add the foreign key?

public class ApplicationUser : IdentityUser
{
    public int BuildingID { get; set; }
}

I am using a Entity Framework Core

Thanks in advance

This is an opinionated answer.

When using ASP.NET Core Identity, in my experience, it is better leave the IdentityUser alone, for authentication etc purpose. But if your application is not that large/complex, I think this user can get into relationship with others entities.

In your case, since Building and ApplicationUser are in one-to-one relation, I'd suggest not to put the foreign key property in the ApplicationUser class. Instead put it in the Building class. That way, no new column will be created in the AspNetUser table (ie it will be left alone), and you will be making Building the dependent entity, not the ApplicationUser . So if you with to use Cascading feature, deleting a Building will not delete the related ApplicationUser but the other way around.

public class ApplicationUser : IdentityUser
{
    public Building Building { get; set; }
}

public class Building
{
    public int Id { get; set; }
    public string Address { get; set; }
    public int ApplicationUserId { get; set; }
    
    public ApplicationUser ApplicationUser { get; set; }    
}

Configuration is something like -

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Building>().ToTable("Building");
    modelBuilder.Entity<Building>(e =>
    {
        e.HasKey(p => p.Id);
        e.Property(p => p.Id).UseIdentityColumn<int>();     
        e.Property(p => p.Address).IsRequired(false).HasColumnType<string>("nvarchar(128)");
        e.Property(p => p.ApplicationUserId).IsRequired(true);
    });
    modelBuilder.Entity<Building>()
        .HasOne(e => e.ApplicationUser)
        .WithOne(p => p.Building)
        .HasForeignKey<Building>(e => e.ApplicationUserId)
        .OnDelete(DeleteBehavior.Cascade);
}

Update:

This was for a One-To-Many Approach

I changed my ApplicationUser class to the following:

public class ApplicationUser : IdentityUser
{
    public int BuildingID { get; set; }
    [ForeignKey("BuildingID")]
    public Building building { get; set; }
}

I then added a migration via the Package Manager Console:

add-migration identity-user-extention

After the migration was complete I edited the generated code and removed all of the code that created and dropped tables. The reasoning behind this was that it was trying to recreate all of my tables that were already generated.

public partial class identity-user-extention: Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.AddColumn<int>(
            name: "BuildingID",
            table: "AspNetUsers",
            nullable: false,
            defaultValue: 0);

        migrationBuilder.CreateIndex(
            name: "IX_AspNetUsers_BuildingID",
            table: "AspNetUsers",
            column: "BuildingID");

        migrationBuilder.AddForeignKey(
            name: "FK_AspNetUsers_BUILDING_BuildingID",
            table: "AspNetUsers",
            column: "BuildingID",
            principalTable: "BUILDING",
            principalColumn: "BUILDING_ID",
            onDelete: ReferentialAction.Cascade);
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropForeignKey(
            name: "FK_AspNetUsers_BUILDING_BuildingID",
            table: "AspNetUsers");

        migrationBuilder.DropIndex(
            name: "IX_AspNetUsers_BuildingID",
            table: "AspNetUsers");

        migrationBuilder.DropColumn(
            name: "BuildingID",
            table: "AspNetUsers");
    }
}

I saved the migration and then ran the following in the Package Manager Console:

Update-Database

I was then able to create an account successfully with a buildingID as a foreign key(the Buildings table was already populated)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM