我試圖通過對聲明進行分組來擴展AspNet標識。我正在用一個新的QueryAppGroup實體來做這件事。我希望能夠添加&刪除任何特定組的聲明。添加是不錯,但我找不到任何刪除特定的要求(例如,通過標識)的方式,使這裏是我的計劃:不能將類轉換爲與泛型接口
創建一個名爲QueryAppGroupClaim的中介實體,以便IdentityUserClaim有一到零與它的一對一的關係。
通過刪除任何特定的QueryAppGroupClaim實體,刪除應級聯到IdentityUserClaim。
- 要做到這一點,我需要IdentityUserClaim與導航屬性回延伸到QueryAppGroupClaim(見ApplicationUserClaim)
代碼:
public class ApplicationUserClaim : IdentityUserClaim<string>
{
[ForeignKey("QueryAppGroupClaim")]
public virtual int QueryAppGroupClaimId { get; set; }
public virtual QueryAppGroupClaim QueryAppGroupClaim { get; set; }
}
public class QueryAppGroup
{
public QueryAppGroup()
{
}
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[ForeignKey("ApplicationUser")]
public String UserId { get; set; }
public String Desc { get; set; }
public String ImpersonateClaimType { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
}
public class QueryAppGroupClaim
{
public QueryAppGroupClaim()
{ }
[Key, ForeignKey("IdentityUserClaim")]
public int ClaimId { get; set; }
public virtual ApplicationUserClaim IdentityUserClaim { get; set; }
[ForeignKey("QueryAppGroup")]
public int QueryAppGroupId { get; set; }
public virtual QueryAppGroup QueryAppGroup { get; set; }
}
public class ApplicationUser : IdentityUser<string, IdentityUserLogin, IdentityUserRole, ApplicationUserClaim>, IUser, IUser<string>
{
//...
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole, string, IdentityUserLogin, IdentityUserRole, ApplicationUserClaim>
{
//...
public DbSet<QueryAppGroup> QueryAppGroups { get; set; }
public DbSet<QueryAppGroupClaim> QueryAppGroupClaims { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUserClaim>()
.HasOptional(t => t.QueryAppGroupClaim)
.WithRequired(t => t.IdentityUserClaim)
.WillCascadeOnDelete();
}
}
public class ApplicationUserStore : UserStore<ApplicationUser,IdentityRole,string, IdentityUserLogin, IdentityUserRole, ApplicationUserClaim>
{
//...
//Already exists with an override of RemoveClaimAsync(...)
}
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{
}
public ApplicationUserManager(ApplicationUserStore store)
: base(store)
{
}
//...
}
我得到一個編圖誤差public ApplicationUserManager(ApplicationUserStore store) : base(store)
時調用基礎構造函數說Argument 1: cannot convert from 'ApplicationUserStore' to 'Microsoft.AspNet.Identity.IUserStore<ApplicationUser>'
。我的類ApplicationUserStore擴展了UserStore,它本身實現了許多接口,其中一個接口是IUserStore。 我看不出如何解決這個問題。請幫忙。
任何關於簡化主要目標的建議也將受到歡迎!
嗨斯科特,並執行'IUserStore'的'ApplicationUserStore'解決這個問題?如果還有問題,我還有其他一些想法。 –
Peter
是的,這工作,並允許我調整我的模型,所以我的應用程序按預期工作,謝謝! –