如何延长Microsoft.AspNet.Identity.EntityFramework.IdentityRole [英] How to Extend Microsoft.AspNet.Identity.EntityFramework.IdentityRole

查看:230
本文介绍了如何延长Microsoft.AspNet.Identity.EntityFramework.IdentityRole的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够延长IdentityRole的默认实现包括像描述字段。这是很容易为IdentityUser做到这一点,因为IdentityDbContext一个类型IdentityUser的泛型参数。然而,IdentityDbContext不允许你为IdentityRole做到这一点。我怎样才能做到这一点?

I want to be able to extend the default implementation of IdentityRole to include fields like Description. It's easy enough to do this for IdentityUser because IdentityDbContext takes a generic parameter of type IdentityUser. However, IdentityDbContext doesn't allow you to do this for IdentityRole. How can I accomplish this?

我知道我可以创建一个基本的DbContext,并实现自己IUserStore,这样我就可以用我自己的角色类,但我真的不希望有这样做。

I know I can create a basic DbContext, and implement my own IUserStore, so that I can use my own role class, but I really don't want to have to do that.

有什么想法?

推荐答案

我刚刚经历这种痛苦了自己。它实际上原来是pretty简单。只需用新属性扩展IdentityRole。

I have just gone through this pain myself. It actually turned out to be pretty simple. Just extend IdentityRole with your new properties.

public class ApplicationRole : IdentityRole
{
    public ApplicationRole(string name)
        : base(name)
    { }

    public ApplicationRole()
    { }

    public string Description { get; set; }
}

然后你需要添加一行

Then you need to add the line

new public DbSet<ApplicationRole> Roles { get; set; }

到您的ApplicationDbContext类这样,否则你会得到错误。

into your ApplicationDbContext class like this otherwise you will get errors.

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {}

    new public DbSet<ApplicationRole> Roles { get; set; }
}

多数民众赞成我需要做的。请确保您更改IdentityRole的所有实例ApplicationRole包括任何你正在播种。另外,不要忘记发出更新数据库将更改应用到您的数据库。在那里的任何现有行不会被新的RoleManager可以看出,除非你有ApplicationRole设置为鉴。您可以轻松地添加这个自己。

thats all I needed to do. Make sure you change all instances of IdentityRole to ApplicationRole including anything you are seeding. Also, dont forget to issue a "update-database" to apply the changes to your DB. Any existing rows in there won't be seen by your new RoleManager unless you have the "ApplicationRole" set as a discriminator. You can easily add this yourself.

心连心

埃里克

这篇关于如何延长Microsoft.AspNet.Identity.EntityFramework.IdentityRole的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆