首页 > 解决方案 > 尝试在种子方法中添加角色时出错 - ASP.NET MVC 5

问题描述

这是我在 ASP.NET MVC 5 和 C# 中的第一个项目。我正在尝试制作一个简单的 CRM 网络应用程序。

当我尝试在种子方法中设置角色时,执行Update-Database命令后出现以下错误:

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

我已经搜索了很多,但还没有找到解决方案。

配置.cs

    using System.Collections.Generic;
    using Microsoft.AspNet.Identity;
    using Microsoft.AspNet.Identity.EntityFramework;
    using simpleCRM.Models;

    namespace simpleCRM.Migrations
    {
        using System;
        using System.Data.Entity;
        using System.Data.Entity.Migrations;
        using System.Linq;

        internal sealed class Configuration : DbMigrationsConfiguration<simpleCRM.DAL.crmContext>
        {
            public Configuration()
            {
                AutomaticMigrationsEnabled = false;
                ContextKey = "simpleCRM.DAL.crmContext";
            }

            protected override void Seed(simpleCRM.DAL.crmContext context) //Takes our context as input
            {
                var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));

                //Creating the admin role
                if (!roleManager.RoleExists("Director"))
                {
                    //Admin role
                    var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole("Director");

                    roleManager.Create(role);
                }

                context.SaveChanges();
        }
    }

crmContext.cs

    using System.Data.Entity;
    using System.Data.Entity.ModelConfiguration.Conventions;
    using simpleCRM.Models;

    /*A class that coordinates the Entity Framework functionality for a given data model.
      It derives from System.Data.Entity.DbContext class*/
    namespace simpleCRM.DAL
    {
        public class crmContext: DbContext
        {   
            public crmContext() : base("crmContext")//Passing the connection string to the constructor
            {
            }

            public DbSet<Customer> Customers { get; set; }
            public DbSet<Call> Calls { get; set; }

            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Conventions.Remove<PluralizingEntitySetNameConvention>();
            }
        }
    }

错误的原因可能是什么?

标签: c#asp.net-mvcentity-framework

解决方案


推荐阅读