c# - 尝试在种子方法中添加角色时出错 - 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>();
}
}
}
错误的原因可能是什么?
解决方案
推荐阅读
- testing - 如何测试 Flutter 小部件的固有大小
- ios - OpenCV HSV:iOS 与 Python 和 C++
- django - 我有两个数据要渲染
- ruby - 用于断言集合中多个对象的属性的更好的失败消息
- django - Django 控制循环
- vb.net - 如何在 vb.net 中不使用 Linq 将数据表按多列分组到另一个数据表中?
- rust - 如何要求显式返回语句并禁止隐式返回?
- r - 使用 step_naomit 进行预测并使用 tidymodels 保留 ID
- r - R:如何在动画包中指定分辨率/质量
- node.js - Nightwatch.js - 如何在不重复代码的情况下通过多个链接并输入值?