C# 尝试在种子方法-ASP.NET MVC 5中添加角色时出错

C# 尝试在种子方法-ASP.NET MVC 5中添加角色时出错,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,这是我在ASP.NETMVC5和C#中的第一个项目。我正在尝试制作一个简单的CRM web应用程序 当我尝试在seed方法中设置角色时,在执行updatedatabase命令后,出现以下错误: 实体类型IdentityRole不是当前上下文模型的一部分 我找了很多,但还没有找到解决办法 配置.cs using System.Collections.Generic; using Microsoft.AspNet.Identity; using Microsoft.AspNe

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

当我尝试在seed方法中设置角色时,在执行
updatedatabase
命令后,出现以下错误:

实体类型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();
        }
    }
    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>();
            }
        }
    }
使用System.Collections.Generic;
使用Microsoft.AspNet.Identity;
使用Microsoft.AspNet.Identity.EntityFramework;
使用simplerm.模型;
命名空间simplerm.Migrations
{
使用制度;
使用System.Data.Entity;
使用System.Data.Entity.Migrations;
使用System.Linq;
内部密封类配置:DBMigOptionsConfiguration
{
公共配置()
{
AutomaticMiggerationsEnabled=假;
ContextKey=“simplerm.DAL.crmContext”;
}
受保护覆盖无效种子(simplerm.DAL.crmContext context)//将我们的上下文作为输入
{
var角色管理器=新角色管理器(新角色存储(上下文));
//创建管理员角色
如果(!roleManager.RoleExists(“董事”))
{
//管理角色
var role=new Microsoft.AspNet.Identity.EntityFramework.IdentityRole(“董事”);
角色管理器。创建(角色);
}
SaveChanges();
}
}
crmContext.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();
        }
    }
    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>();
            }
        }
    }
使用System.Data.Entity;
使用System.Data.Entity.ModelConfiguration.Conventions;
使用simplerm.模型;
/*协调给定数据模型的实体框架功能的类。
它派生自System.Data.Entity.DbContext类*/
名称空间simplerm.DAL
{
公共类crmContext:DbContext
{   
public crmContext():base(“crmContext”)//将连接字符串传递给构造函数
{
}
公共数据库集客户{get;set;}
公共DbSet调用{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove();
}
}
}

错误的原因可能是什么?

如果要使用标识表,则您的上下文需要从IdentityDbContext继承,并且需要将表添加(或迁移)到数据库中。谢谢!在添加语句base.OnModelCreating(modelBuilder)后,这解决了问题;-->在OnModelCreating方法中,请将其作为正式答案,以便我可以接受。我的答案只是一个链接。请投票选择帮助您的答案。如果要使用标识表,则您的上下文需要从IdentityDbContext继承,您需要添加(或迁移)将表添加到数据库中。谢谢!在添加了语句库之后解决了问题。OnModelCreating(modelBuilder);-->在OnModelCreating方法中,请将其作为正式答案,以便我可以接受。我的只是一个链接。请投票表决对您有帮助的答案。