我试图通过对声明进行分组来扩展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
是的,这工作,并允许我调整我的模型,所以我的应用程序按预期工作,谢谢! –