Asp.net.identity覆盖角色并更改主键

时间:2015-10-30 12:21:04

标签: c# asp.net ef-code-first asp.net-identity ef-fluent-api

我实际上是试图覆盖asp.net.identity附带的IdentityRole类来启用多租户'角色。

我已经按照以下方式覆盖了课程:

public class ApplicationRole : IdentityRole, IIdentifiableEntity<string>, ITenantable
{
    public Tenant Tenant { get; set; }
    public Guid TenantId { get; set; }
    public string Description { get; set; }

    public ApplicationRole() : base() { }
    public ApplicationRole(string name) : base(name) { }
    public ApplicationRole(string name, Guid tenantId) : base(name)
    {
        this.TenantId = tenantId;
    }

    public string EntityId
    {
        get { return Id; }
        set { Id = value; }
    }
}

这很好用。现在我需要更改primaryKey以使用composedKey,这样我才能拥有&#39; admin&#39;使用tenantId = 1和&#39; admin&#39; with tenantId = 2.(注意我使用的是流畅的api)。

我尝试了两件事...... 第一:

public ApplicationRoleMap()
    {
        // Primary Key
        HasKey(t => t.Id);

        Property(t => t.Name).HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_RoleTenant", 1) { IsUnique = true }));
        Property(t => t.TenantId).HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_RoleTenant", 2) { IsUnique = true }));

        HasRequired(t => t.Tenant).WithMany().WillCascadeOnDelete(false);
        Ignore(t => t.EntityId);
    }

这个添加了唯一约束但在名称上保留了唯一主键,因此我不能创建两个具有相同名称但具有不同tenantId的角色。

然后我尝试了:

public ApplicationRoleMap()
    {
        // Primary Key
        HasKey(t => new { t.Id, t.TenantId });

        //Property(t => t.Name).HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_RoleTenant", 1) { IsUnique = true }));
        //Property(t => t.TenantId).HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_RoleTenant", 2) { IsUnique = true }));

        HasRequired(t => t.Tenant).WithMany().WillCascadeOnDelete(false);
        Ignore(t => t.EntityId);
    }

这应该可行,但在运行update-database时,它表示没有待处理的更改,就好像我的主键属性没有负责一样。我猜TKey设置在IRole.Id上取而代之......

我怎样才能实现它?

0 个答案:

没有答案