【问题标题】:How to change ASP.NET Boilerplate Role entity Id from int to long如何将 ASP.NET 样板角色实体 ID 从 int 更改为 long
【发布时间】:2020-02-23 10:16:39
【问题描述】:

我需要添加一个新的派对实体(表格)。该实体遵循党派设计模式,其中用户、组织单位和角色实体 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");
   });

【问题讨论】:

标签: entity-framework-core aspnetboilerplate asp.net-boilerplate


【解决方案1】:

如果不重新构建标准 Abp 包,您将无法更改 RoleId 类型。不幸的是,这就是它的设计方式。但是,您可以解决此问题并使用 RoleId 添加其他表,并在模型构建器中将其设置为 Unique Tenant Wise。这将不是真正的 SQL 严格实现,因为您将获得一对多的关系,但它的行为将如您所愿。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-24
    • 1970-01-01
    • 1970-01-01
    • 2019-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多