我需要添加一个新的聚会实体(表)。该实体遵循参与方设计模式,其中用户,组织单位和角色实体Id是主键,并且还是链接到参与方主键的外键。我能够通过用户实体和组织实体(而不是角色实体)实现此目标,因为角色ID为int。
EF核心投诉角色表的主键类型与聚会表的主键不匹配。
下面是代码示例:
[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");
});
如果不重建标准Abp程序包,则无法更改RoleId类型。这就是不幸的设计方式。但是,您可以解决此问题,并添加带有RoleId的其他表,并在modelbuilder中使其成为“唯一租户明智”表。这并不是真正严格的SQL严格实现,因为您将获得一对多关系,但是行为会如您所愿。