实体类型IdentityRole不是当前上下文的模型的一部分

时间:2015-08-16 22:05:13

标签: asp.net-mvc-5 asp.net-identity

我在MVC项目上实现ASPNetIdentity时遇到了麻烦 我在var行中收到此错误(实体类型IdentityRole不是当前上下文模型的一部分。):

using System;
using System.Linq;
using System.Web.Mvc;

using EZ.Models;
using Microsoft.AspNet.Identity.EntityFramework;

namespace EZ.Controllers
{
public class RoleController : Controller
{
    ApplicationDbContext context;

    public RoleController()
    {
        context = new ApplicationDbContext(); 
    }



    /// <summary>
    /// Get All Roles
    /// </summary>
    /// <returns></returns>
    public ActionResult Index()
    {
        var roles = context.Roles.ToList();
        return View(roles);
    } 

    /// <summary>
    /// Create  a New role
    /// </summary>
    /// <returns></returns>
   // GET: /Roles/Create
    public ActionResult Create()
    {
        return View();
    }

    //
    // POST: /Roles/Create
    [HttpPost]
    public ActionResult Create(FormCollection collection)
    {
        try
        {
            context.Roles.Add(new Microsoft.AspNet.Identity.EntityFramework.IdentityRole()
            {
                Name = collection["RoleName"]
            });
            context.SaveChanges();
            ViewBag.ResultMessage = "Role created successfully !";
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    } 

    /// <summary>
    /// Set Role for Users
    /// </summary>
    /// <returns></returns>
    public ActionResult SetRoleToUser()
    {
        var list = context.Roles.OrderBy(role => role.Name).ToList().Select(role => new SelectListItem { Value = role.Name.ToString(), Text = role.Name }).ToList();
        ViewBag.Roles = list;
        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult UserAddToRole(string uname, string rolename)
    {
        ApplicationUser user = context.Users.Where(usr => usr.UserName.Equals(uname, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();

        // Display All Roles in DropDown

        var list = context.Roles.OrderBy(role => role.Name).ToList().Select(role => new SelectListItem { Value = role.Name.ToString(), Text = role.Name }).ToList();
        ViewBag.Roles = list;

        if (user != null)
        {
            var account = new AccountController();
            account.UserManager.AddToRoleAsync(user.Id, rolename);

            ViewBag.ResultMessage = "Role created successfully !";

            return View("SetRoleToUser");
        }
        else
        {
            ViewBag.ErrorMessage = "Sorry user is not available";
            return View("SetRoleToUser");
        }

    }
}

}

我已经在我的数据库中编写了表格。

这与CodeProject.com中的role-security-mvc5-master项目完全相同。唯一的区别是我在我的数据库中移动了表,并且更改了连接字符串。我错过的是什么?

在我的IdentityModel.cs中我有:

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

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

如果您需要更多代码,请告诉我,我会发布。

1 个答案:

答案 0 :(得分:1)

创建项目时,身份,locadb上的数据库。当我在我的数据库中移动表时,我用我为E创建的表覆盖整个默认连接字符串。这里的大问题是默认连接需要providerName =&#34; System.Data.SqlClient&#34; /&GT;和NOT providerName =&#34; System.Data.EntityClient&#34;。那里要小心。信用需要转到Daniel Eagle的这篇优秀文章:gif showing problem。关于如何首先使用Identity与DB的详细信息。