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

别说谁变了你拦得住时间么 提交于 2020-01-07 04:57:15

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/46338234/database-migration-adds-new-column-tenancy-id-when-adding-tenant-to-user-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!