Search code examples
angularjsaspnetboilerplate

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


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?


Solution

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

    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'.

    I got it to work by adding an InverseProperty attribute:

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