entity-framework-coreaspnetboilerplateasp.net-boilerplate

How to change ASP.NET Boilerplate Role entity Id from int to long


I need to add a new party entity (table). This entity follows party design pattern where user, organization unit and role entity Id is primary key and also a foreign key that links to the party entity primary key. I was able to achieve this with user entity and organization entity but not role entity because the role Id is int.

EF core complaints the role table's primary key type mismatched with the party table primary key.

Below are the code samples:

[Serializable]
[Table("MdParties")]
public class Party : FullAuditedEntity<long>, IMayHaveTenant
{
      public int? TenantId { get; set; }
}

public partial class User
{
    [Required, ForeignKey(nameof(Party))]
    public override long Id { get; set; } // PK and FK pointing to Party

    public virtual Party Party { get; set; }
}

public class OrganizationUnitExt : OrganizationUnit 
{
    [Required, ForeignKey(nameof(Party))]
    public override long Id { get; set; } // PK and FK pointing to Party

    public virtual Party Party { get; set; }
}

public partial class Role : AbpRole<User>
{
    [Required, ForeignKey(nameof(Party))]
    public override int Id { get; set; } // PK and FK pointing to Party

    public virtual Party Party { get; set; }
}

    modelBuilder.Entity<User>(b =>
    {
        b.HasIndex(e => new { e.UserName });

        b.HasOne(d => d.Party)
            .WithMany()
            .HasForeignKey(d => d.Id)
            .OnDelete(DeleteBehavior.Cascade)
            .HasConstraintName("FK_AbpUsers_PartyId");
    });

    modelBuilder.Entity<OrganizationUnitExt>(entity =>
    {             
        entity.HasOne(d => d.Party)
            .WithMany()
            .HasForeignKey(d => d.Id)
            .OnDelete(DeleteBehavior.Cascade)
            .HasConstraintName("FK_AbpOrganizationUnits_PartyId");
   });

Solution

  • You cannot change RoleId type without rebuilding standard Abp package. This is how it was design unfortunetly. However you can walkaround this and add additional table with RoleId and in modelbuilder make it table as Unique Tenant Wise. This will be not trully SQL strict implementation cause you will get one-to-many relation but behaviour of it will be as you expect.