2015-10-26 51 views
0

這裏是我的ApplicationUser和的DbContext類如何標識2 mvc5創建角色

public class ApplicationUser : IdentityUser 
{ 

    public int? studentID { get; set; } 
    [ForeignKey("studentID")] 
    public virtual Student Student { get; set; } 
} 


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

    } 
    public DbSet<ApplicationRole> ApplicationRoles { get; set; } 

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

和我的應用程序角色類

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

    public ApplicationRole() 
    { } 

    public string Description { get; set; } 
} 
在我的角色

控制器

public class RolesController : Controller 
{ 
    private ApplicationDbContext db = new ApplicationDbContext(); 

    // GET: /Roles/ 
    public ActionResult Index() 
    { 
     return View(db.Roles.ToList()); 
    } 

db.Roles.ToList()什麼都沒顯示但我已經把一些角色放在數據庫中。 db.Roles.ToList()不返回任何列表。顯示「枚舉沒有結果」

請幫助我。 我想打包角色,我必須使用roleID到另一個表作爲外鍵。 這是什麼問題。我需要一個簡單的解決方案 謝謝。

+0

答案是否爲你做了訣竅:)? –

回答

1
我覺得這是做

最好的辦法是在遷移(創建如果不存在):

internal sealed class Configuration : DbMigrationsConfiguration<ApplicationDbContext> 
{ 
    public Configuration() 
    { 
     AutomaticMigrationsEnabled = false; 
    } 

    protected override void Seed(ApplicationDbContext context) 
    { 
     ParameterSeeder.Seed(context); 
     RoleSeeder.Seed(context); 
     UserSeeder.Seed(context); 
    } 
} 

然後爲用戶創建一個自定義的播種機,...和角色

internal static class RoleSeeder 
{ 
    internal static void Seed(ApplicationDbContext context) 
    { 
     CreateRole("Admin", "Administrator"); 
    } 

    private static void CreateRole(string name, string description) 
    { 
     var idManager = new IdentityManager(); 

     var newRole = new Role 
     { 
      Name = name, 
      Description = description 
     }; 

     if (!idManager.RoleExists(newRole.Name)) 
      idManager.CreateRole(newRole.Name, newRole.Description); 
    } 
} 

你IdentityManager,您將在應用程序中使用,不只是種子:

public class IdentityManager 
{ 
    public bool RoleExists(string name) 
    { 
     var rm = new RoleManager<Role>(new RoleStore<Role>(new ApplicationDbContext())); 

     return rm.RoleExists(name); 
    } 

    public bool CreateRole(string name, string description) 
    { 
     var rm = new RoleManager<Role>(new RoleStore<Role>(new ApplicationDbContext())); 

     var newRole = new Role { Description = description, Name = name }; 
     var idResult = rm.Create(newRole); 

     return idResult.Succeeded; 
    } 

    public bool CreateUser(User user, string password) 
    { 
     var um = new UserManager<User>(new UserStore<User>(new ApplicationDbContext())); 
     um.UserValidator = new UserValidator<User>(um) 
     { 
      AllowOnlyAlphanumericUserNames = false, 
      RequireUniqueEmail = false 
     }; 

     user.UserInfo.ModifiedDate = DateTime.Now; 
     user.UserInfo.ModifiedBy = "Migrations"; 
     user.UserInfo.ValidFrom = DateTime.Now; 

     var idResult = um.Create(user, password); 

     return idResult.Succeeded; 
    } 

    public bool AddUserToRole(string userId, string roleName) 
    { 
     var um = new UserManager<User>(new UserStore<User>(new ApplicationDbContext())); 
     um.UserValidator = new UserValidator<User>(um) 
     { 
      AllowOnlyAlphanumericUserNames = false, 
      RequireUniqueEmail = false 
     }; 

     var idResult = um.AddToRole(userId, roleName); 

     return idResult.Succeeded; 
    } 
} 

希望這一起讓你...

0

一個問題我與您的設置看到的是

public DbSet<ApplicationRole> ApplicationRoles { get; set; } 

一旦你初始化你的背景,你在做了你並不需要添加爲角色變得可用

private ApplicationDbContext db = new ApplicationDbContext(); 

然後你

// GET: Roles 
public ActionResult AllRoles() 
{ 
    return View(db.Roles.ToList()); 
} 

應該工作。

當你添加這個時,腳手架可能會在IdentityModels的ApplicationDbContext中添加一個Dbset。繼續並將其刪除,因爲該角色已在您的上下文中可用,並且您可以使用db.Roles

現在您可以爲此創建一個視圖。喜歡的東西

@model IEnumerable<Microsoft.AspNet.Identity.EntityFramework.IdentityRole> 
<table class="table"> 
<tr> 
    <th>Role</th> 
</tr> 
@foreach (var item in Model) 
{ 
<tr> 
    <td> 
     @Html.DisplayFor(modelItem => item.Name) 
    </td> 
    <td> 
} 
</table> 

有多種資源,說明如何種子用戶和角色,所以我將介紹如何從用戶界面創建角色像一個管理控制檯,因爲我發現,大多數現有的資源是要麼過於複雜或者與其他自定義模型的處理方式不一致。

爲了創建從UI一個角色,你需要像

[HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult CreateRole([Bind(Include = "Id, Name")] IdentityRole role) 
    { 
     if (ModelState.IsValid) 
     { 
      db.Roles.Add(role); 
      db.SaveChanges(); 
      //If everything is good, save changes and redirect the user to the page with the list of all the roles. 
      return RedirectToAction("AllRoles"); 
     } 
     //If data is not valid return the original view with what the user has filled in. 
     return View(role); 
    } 

這確實基本的工作,但你可以添加的權限,驗證和錯誤消息的控制器。爲這個控制器創建一個適當的視圖,這不應該太難。在該模型的視圖中,使用

@model Microsoft.AspNet.Identity.EntityFramework.IdentityRole