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");
});