在将Tenant添加到User类时,数据库迁移会添加新列Tenancy_Id [英] Database migration adds new column Tenancy_Id when adding Tenant to User class

查看:204
本文介绍了在将Tenant添加到User类时,数据库迁移会添加新列Tenancy_Id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use boilerplate template with Angular, .NET Framework and Module Zero. I have added Tenant to User class and User list to Tenant 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;
    }
}

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

Now when I add migration I get

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

为什么实体框架创建另一个外键,以及如何将用户列表添加到租户?

Why entity framework creates another foreign key and how should I add users list to Tenant?

推荐答案

我很惊讶您可以添加迁移.我在尝试重现时得到了这个:

I'm surprised you could add a migration. I got this while trying to reproduce:

无法确定类型为"Tenant"的导航属性"User.Tenant"表示的关系.要么手动配置关系,要么使用"[NotMapped]"属性或"OnModelCreating"中的"EntityTypeBuilder.Ignore"忽略此属性.

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属性使它起作用:

I got it to work by adding an InverseProperty attribute:

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

这篇关于在将Tenant添加到User类时,数据库迁移会添加新列Tenancy_Id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆