1

This is my first ever project in ASP.NET MVC 5 and C# in general. I'm trying to make a simple CRM web app.

When I try to set roles in my seed method I get the following error after executing the Update-Database command:

The entity type IdentityRole is not part of the model for the current context.

I've searched a lot but didn't find a solution yet.

Configuration.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>();
            }
        }
    }

What might be the cause of the error?

John
  • 410
  • 1
  • 9
  • 21
  • 3
    If you are going to use Identity tables, then your context needs to inherit from IdentityDbContext and you need to add (or migrate) the tables to the database. https://stackoverflow.com/questions/19902756/asp-net-identity-dbcontext-confusion – Steve Greene Feb 19 '20 at 22:18
  • Thank you! That solved the problem after also adding the statement base.OnModelCreating(modelBuilder); --> in OnModelCreating method – John Feb 20 '20 at 05:32
  • Please make it as the official answer so I can accept it. – John Feb 20 '20 at 05:44
  • Mine is just a link. Upvote the answer that helped you. – Steve Greene Feb 20 '20 at 19:51

0 Answers0