2017-01-11 34 views
1

首先,有很多類似的問題,但這不是重複的,因爲我的代碼比其他代碼真的不同。實體類型ApplicationRole不是當前上下文的模型的一部分。這不是重複的

我想要實現使用Asp.net MVC身份會員。但我不能接受這個錯誤。讓我們來看看代碼:

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在Models文件夾

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在Models文件夾

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(); 
     } 
    } 
} 

我給你充分應用。我無法解決此問題標題中提到的這個問題。當我運行該應用程序時,會在代碼爲 if (!roleManager.RoleExists("Admin"))的這一行中的global.asasx文件中引發該exeption。你對這個問題有什麼看法?

+0

我想你的建議,但不工作的第三行。謝謝@ haim770 3 –

+1

您的代碼可能不同,但問題與其他問題相同。基本上這個錯誤表明你的數據庫沒有表來存儲角色或者遷移不知道這個表。創建創建新遷移查看遷移中添加的內容,在數據庫上運行遷移。 – trailmax

+0

絕對你是對的。非常感謝你的幫助。 –

回答

1

您正在使用一個名爲LoginDBContext背景,但是你的身份DB上下文被命名爲MyDbContext

變化的Application_Start

 MyDbContext db = new MyDbContext(); 
+0

你是對的,但我確實忘記改變這條線。我做了你的建議,但沒有它沒有工作。謝謝。 –

+0

請執行以下操作: 打開包管理器控制檯,運行enable-migrations命令,然後運行add-migration -name Initial,然後在名爲Migration的文件夾中找到一個名爲Configurations的新類,在該類中有一個名爲「種子,將添加角色的代碼移動到此方法中,然後再試一次 –

+0

謝謝我現在正在嘗試。 –

相關問題