ASP.NET ID - IdentityDBContext ASPNETROLESテーブルに追加されたフィールドに問題を引き起こす

StackOverflow https://stackoverflow.com//questions/22051140

質問

IdentityDBContext aspnetrolesに追加されたプロパティ/フィールドの問題を引き起こす

問題はIdentityUserタイプを取得するIdentityDBContextとここにあると思います:

public class MyIdentityDb : IdentityDbContext<ApplicationUser>
{
    public IdentityDb()
        : base("IdentityDb")
    {
    }
}
.

しかし私に説明させて...

IdentityUserから継承するApplicationUserクラスにプロパティを追加することによって、ASPNETUSERSテーブルにフィールドを追加できることは素晴らしいことです。例:

public class ApplicationUser : IdentityUser
    {
        [Required]
        [StringLength(50)]
        public string FirstName { get; set; }

        [Required]
        [StringLength(50)]
        public string LastName { get; set; }
    }
.

SO Natural IdentityRoleから継承するApplicationRoleクラスにプロパティを追加することによって、ASPNETROLESテーブルにフィールドを追加できます。例:

public class ApplicationRole : IdentityRole
{
    [Required]
    [StringLength(50)]
    public string ProperName { get; set; }
}
.

は完璧に機能します。データベース内のフィールドを見ることができます。データを追加できます。例:

RoleManager<ApplicationRole> roleManager = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(new MyIdentityDb()));

var role = new ApplicationRole() { Name = name, ProperName = propername };
var result = await roleManager.CreateAsync(role);
.

しかし今、データに到達しようとしているときに問題が発生します。例:

私たちのビューモデル:

public class IndexViewModel
{
    public IList<ApplicationUser> Users { get; set; }
    public IList<ApplicationRole> Roles { get; set; }
}
.

コントローラの場合:

private MyIdentityDb myIdentityDb = new MyIdentityDb();
.

私たちのコントローラの私達の索引手法:

public ViewResult Index(int? page)
{
     return View(new IndexViewModel
     {
          Users = myIdentityDb.Users.ToList(),
          Roles = myIdentityDb.Roles.ToList()
     });
}
.

エラーは "myidentitydb.roles.tolist()"にあり、読み取り "は暗黙的にSystem.Collection.Generic.List<Microsoft.AspNet.Identity.EntityFramework.IdentityRole> to System.Collections.Generic.IList<MyApp.Models.ApplicationRole>の型を変換することはできません...

もちろん、次の例のように型IdentityRoleを使用するようにViewModelを変更することができましたが、ASPNETROLESテーブルの新しい "propername"フィールドにアクセスできない:

public class IndexViewModel
{
    public IList<ApplicationUser> Users { get; set; }
    public IList<IdentityRole> Roles { get; set; }
}
.

だから私たちは別のDBクラスを作成してIdentityUserの代わりにIdentityRole型に渡すことができます。

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

public class MyIdentityRolesDb : IdentityDbContext<ApplicationRole>
{
    public MyIdentityRolesDb()
        : base("MyIdentityDb")
    {
    }
}
.

とコントローラを変更する:

private MyIdentityDb myIdentityDb = new MyIdentityDb();
private MyIdentityRolesDb myIdentityRolesDb = new MyIdentityRolesDb();
.

と私たちのコントローラに索引方式を変更する:

public ViewResult Index(int? page)
{
     return View(new IndexViewModel
     {
          Users = myIdentityDb.Users.ToList(),
          Roles = myIdentityRolesDb.Roles.ToList()
     });
}
.

しかし、私たちは同じ問題に終わります。 IdentityDBContextがIdentityUser型を取ります。

カスタムフィールド/プロパティを使用して役割のリストを取得する方法?

役に立ちましたか?

解決

その2.0.0 Betaパッケージにアップグレードすると、ロールマネージャから直接ApplicationRoleのIQueryableを入手できるはずです。

roleManager.Roles
.

だからDBのコンテキストにドロップダウンする必要はありません。これは、2.0リリースで修正された1.0の制限でした。

他のヒント

あなたがやろうとしているものを詳細に説明している次の記事を見てください http://typecastexcepion.com/post/2014/02/13/aSpnet-mvc-5-Identity-And-Modifyly-Roles.aspx

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top