2017-01-11 9 views
1

まず第一に、そのような質問がたくさんありますが、私のコードは他のものよりも実際には分かりにくいので、これは重複しません。エンティティタイプApplicationRoleは、現在のコンテキストのモデルの一部ではありません。これは重複していません

Asp.net MVC IDを使用してメンバーシップを実装したいと思います。しかし、私はこのエラーを乗り越えることはできません。のは、コードを見てみましょう:

IdentityStratup.csアイデンティティフォルダの下に

using System; 
using System.Threading.Tasks; 
using Microsoft.Owin; 
using Owin; 
using Microsoft.Owin.Security.Cookies; 
using Microsoft.AspNet.Identity; 
using Login.Models; 

[assembly: OwinStartup(typeof(Login.App_Start.Startup))] 

namespace Login.App_Start 
{ 
    public class Startup 
    { 
     public void Configuration(IAppBuilder app) 
     { 
      // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888 
      app.UseCookieAuthentication(new CookieAuthenticationOptions 
      { 
       AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
       LoginPath = new PathString("/Account/Login") 
      }); 

      app.CreatePerOwinContext(MyDbContext.Create); 

     } 
    } 
} 

- >ApplicationRole.csアイデンティティフォルダの下に

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using Microsoft.AspNet.Identity.EntityFramework; 

namespace Login.Identity 
{ 
    public class ApplicationRole : IdentityRole 
    { 
     public string Description { get; set; } 

     public ApplicationRole() 
     { 

     } 

     public ApplicationRole (string roleName, string description) : base(roleName) 
     { 
      this.Description = description; 
     } 
    } 
} 

- >ApplicationUser。 CS

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using Microsoft.AspNet.Identity.EntityFramework; 

namespace Login.Identity 
{ 
    public class ApplicationUser : IdentityUser 
    { 
     public string Name { get; set; } 
     public string Surname { get; set; } 
    } 
} 

Login.csモデルフォルダ内

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.ComponentModel.DataAnnotations; 
using System.Linq; 
using System.Web; 

namespace Login.Models 
{ 
    public class Login 
    { 
     [Required] 
     [StringLength(50)] 
     [RegularExpression(@"^(?=[a-zA-Z])[-\w.]{0,23}([a-zA-Z\d]|(?<![-.])_)$")] 
     public string UserName { get; set; } 

     [Required] 
     [RegularExpression(@"^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$")] 
     [DataType(DataType.Password)] 
     public string Password { get; set; } 

     [Required] 
     [DisplayName("Remember Me")] 
     public bool RememberMe { get; set; } 
    } 
} 

Register.csモデルフォルダ内

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.ComponentModel.DataAnnotations; 
using System.Linq; 
using System.Web; 

namespace Login.Models 
{ 
    public class Register 
    { 
     [Key] 
     [Required] 
     public int ID { get; set; } 

     [Required] 
     [StringLength(50)] 
     [RegularExpression(@"^(([A-za-z]+[\s]{1}[A-za-z]+)|([A-Za-z]+))$")] 
     public string Name { get; set; } 

     [Required] 
     [StringLength(50)] 
     [RegularExpression(@"^(([A-za-z]+[\s]{1}[A-za-z]+)|([A-Za-z]+))$")] 
     public string Surname { get; set; } 

     [Required] 
     [StringLength(50)] 
     [RegularExpression(@"^(?=[a-zA-Z])[-\w.]{0,23}([a-zA-Z\d]|(?<![-.])_)$")] 
     public string Username { get; set; } 

     [Required] 
     [EmailAddress] 
     public string Email { get; set; } 

     [Required] 
     [RegularExpression(@"^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$")] 
     [DataType(DataType.Password)] 
     public string Password { get; set; } 

     [Required] 
     [RegularExpression(@"^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$")] 
     [Compare("Password")] 
     public string PasswordConfirmation { get; set; } 
    } 
} 

Global.asasx

using Login.Identity; 
using Login.Models; 
using Microsoft.AspNet.Identity; 
using Microsoft.AspNet.Identity.EntityFramework; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using System.Web.Routing; 

namespace Login 
{ 
    public class MvcApplication : System.Web.HttpApplication 
    { 
     protected void Application_Start() 
     { 
      AreaRegistration.RegisterAllAreas(); 
      RouteConfig.RegisterRoutes(RouteTable.Routes); 

      MyDbContext db = new MyDbContext(); 
      RoleStore<ApplicationRole> roleStore = new RoleStore<ApplicationRole>(db); 
      RoleManager<ApplicationRole> roleManager = new RoleManager<ApplicationRole>(roleStore); 



       if (!roleManager.RoleExists("Admin")) 
       { 
        ApplicationRole adminRole = new ApplicationRole("Admin", "System Admnistrator"); 
        roleManager.Create(adminRole); 
       } 

       if (!roleManager.RoleExists("User")) 
       { 
        ApplicationRole userRole = new ApplicationRole("User", "System Contraint User"); 
        roleManager.Create(userRole); 
       } 


     } 
    } 
} 

MyDbContext.cs

using Login.Identity; 
using Microsoft.AspNet.Identity.EntityFramework; 
using System; 
using System.Collections.Generic; 
using System.Data.Entity; 
using System.Linq; 
using System.Web; 

namespace Login.Models 
{ 
    public class MyDbContext : IdentityDbContext<ApplicationUser> 
    { 
     public MyDbContext() : base("name=LoginDBContext") 
     { 

     } 

     public DbSet<Categories> Categories { get; set; } 

     public static MyDbContext Create() 
     { 
      return new MyDbContext(); 
     } 
    } 
} 

私はあなたのアプリケーションの完全なを与えました。私はこの問題のタイトルに記載されたこの問題を解決することができませんでした。私がアプリケーションを実行すると、コードのこの行にあるglobal.asasxファイルにその例外がスローされます if (!roleManager.RoleExists("Admin"))。この問題についてどう思いますか?

+0

の3行目は、私はあなたのアドバイスを試みたが、動作していないMyDbContext

変更と命名されます。ありがとう@ haim770 3 –

+1

あなたのコードは違うかもしれませんが、問題は他の質問と同じです。基本的には、このエラーは、データベースにロールを格納するテーブルがないか、移行がこのテーブルを知らないことを示しています。新規マイグレーションの作成を作成するマイグレーションで追加されたものを参照し、db上でマイグレーションを実行します。 – trailmax

+0

あなたは正しいです。手伝ってくれてどうもありがとう。 –

答えて

1

あなたはLoginDBContextという名前のコンテキストを使用しているが、しかし、あなたのアイデンティティDBのコンテキストがApplication_Start

 MyDbContext db = new MyDbContext(); 
+0

あなたは正しいですが、私はその行を変更することを忘れました。私はあなたの助言をしましたが、それは機能しません。ありがとうございました。 –

+0

パッケージマネージャコンソールを開き、enable-migrationsコマンドを実行してからadd-migration -name Initialを実行すると、Migrationsという名前のフォルダ内にConfigurationsという新しいクラスが見つかるはずです。このクラスでは、シード、このメソッドにロールを追加するコードを移動してもう一度やり直してください –

+0

ありがとうございます。 –

関連する問題