将租户添加到用户 class 时,数据库迁移会添加新列 Tenancy_Id

Database migration adds new column Tenancy_Id when adding Tenant to User class

我将样板模板与 Angular、.NET Framework 和零模块一起使用。我已将租户添加到用户 class 并将用户列表添加到租户 class

public class Tenant : AbpTenant<User>
{
   public Tenant()
   {
     this.Users = new HashSet<User>();
   }
   public ICollection<User> Users { get; set; }

   public Tenant(string tenancyName, string name)
     : base(tenancyName, name)
   {
   }
}

public class User : AbpUser<User>
{
    public const string DefaultPassword = "123qwe";
    public virtual Tenant Tenant { get; set; }
    public static string CreateRandomPassword()
    {
        return Guid.NewGuid().ToString("N").Truncate(16);
    }

    public static User CreateTenantAdminUser(int tenantId, string emailAddress, string password)
    {
        var user = new User
        {
            TenantId = tenantId,
            UserName = AdminUserName,
            Name = AdminUserName,
            Surname = AdminUserName,
            EmailAddress = emailAddress,
            Password = new PasswordHasher().HashPassword(password)
        };

        return user;
    }
}

现在,当我添加迁移时,我得到

public override void Up()
    {
        AddColumn("dbo.AbpUsers", "Tenant_Id", c => c.Int());
        CreateIndex("dbo.AbpUsers", "TenantId");
        CreateIndex("dbo.AbpUsers", "Tenant_Id");
        AddForeignKey("dbo.AbpUsers", "Tenant_Id", "dbo.AbpTenants", "Id");
        AddForeignKey("dbo.AbpUsers", "TenantId", "dbo.AbpTenants", "Id");
    }

为什么 entity framework 创建另一个外键以及我应该如何将用户列表添加到租户?

我很惊讶你能添加一个迁移。我在尝试重现时得到了这个:

Unable to determine the relationship represented by navigation property 'User.Tenant' of type 'Tenant'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

我通过添加 InverseProperty 属性让它工作:

public class Tenant : AbpTenant<User>
{
    [InverseProperty("Tenant")]
    public ICollection<User> Users { get; set; }
}