2

Context of problem

In my web application I've been using ASP.NET Identity. I've created custom user profiles by inheriting the User class from IdentityUser. To be able to connect the Roles with for example rolegroups I've also created a custom role class within my database which inherits from the IdentityRole class.

My database model looks like this: Part of databasemodel

The problem isn't in creating a user. This just works:

var userManager = new UserManager<User>(new UserStore<User>(_context));
var result = userManager.Create(user, password);

Same with creating a role.

var rm = new RoleManager<Role>(new RoleStore<Role>(_context));
var result = rm.Create(new Role(name));

Problem

But its when I try to attach a role to a user (With the following code)

var um = new UserManager<User>(new UserStore<User>(_context));
var result = um.AddToRole(userId, roleName);

The error I get is basically "The entity type IdentityRole is not part of the model for the current context."

Which is true, btw. But how can I make it that my Role entity is seen as the IdentityRole entity, which it basically is, or has to be.

See the full stacktrace here: http://pastebin.com/sEPv5LiT

Thanks in advance!

Rickest Rick
  • 1,519
  • 1
  • 15
  • 28
Corstian Boerman
  • 848
  • 14
  • 32
  • Is there a specific reason that you have two different contexts? If not using the same context for UserManager and your entities may work: http://stackoverflow.com/a/22672165/433234 – jasonwarford Jun 18 '14 at 19:28
  • I have a single DbContext. Also, these three pieces of code use the same instance of my DbContext, which shouldn't give any problems AFAIK. – Corstian Boerman Jun 18 '14 at 20:04
  • having the same issue, did you figure it out? – Yehia A.Salam Jan 23 '15 at 17:49
  • @YehiaA.Salam In the end I 'manually' connect roles to a specific user after the account is created, which works quite well. With manually I just mean editing the database directly without additional layer (in this case the UserManager). I have some sample code for you if you wish. – Corstian Boerman Jan 23 '15 at 20:24
  • @CorstianBoerman maybe its the UserStore thing that worked with me http://stackoverflow.com/questions/28116426/addtorole-and-identityrole-is-not-part-of-the-model-for-the-current-context – Yehia A.Salam Jan 23 '15 at 20:27

1 Answers1

0

As i understand you want to define custom roles for users i prefer to add library project in your solution and manage all membership and roles through this.

Custom Asp.Net Membership

I make library project for that if you want i will provide you

  • I've already written logic to create custom roles and so on. The only problem right now is that I need to attach these roles to the users of my application. I can't do this by using Entity Framework because I don't have entities of the roles, or wait, what, do I? I'm just thinking that probably the best way to achieve what I want is to manually connect these two entities with entitiy framework. I'm going to try it. :) – Corstian Boerman Jun 18 '14 at 14:12
  • Yes you should do make role and userrole seperate entities – azhar_SE_nextbridge Jun 18 '14 at 14:15
  • Off course! Each user is attached to a company on which also roles and rolegroups can be defined. (Not showed in model) The user automatically inherits these roles. – Corstian Boerman Jun 18 '14 at 14:20