Reputation: 745
I'm building an MVC 5 app using EF6 Code First and am running into an issue while trying to build some basic user management interfaces. The error I'm receiving is "The entity type IdentityUser is not part of the model for the current context".
The Code:
Service Layer:
public class CoreService
{
private IPrincipal User;
private IdentityDb identDb;
private CoreDb coreDb;
private RoleManager<IdentityRole> _roleManager;
private UserManager<IdentityUser> _userManager;
public CoreService()
{
User = HttpContext.Current.User;
identDb = new IdentityDb();
coreDb = new CoreDb();
_roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(identDb));
_userManager = new UserManager<IdentityUser>(new UserStore<IdentityUser>(identDb));
}
public void TestIdentityManagers(string roleName, string userName)
{
var role = _roleManager.FindByName(roleName);
var user = _userManager.FindByName(userName);
}
}
Database Context:
public class IdentityDb : IdentityDbContext<ApplicationUser>
{
public IdentityDb()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static IdentityDb Create()
{
return new IdentityDb();
}
}
My app has no trouble using the _roleManager to add, delete and otherwise manipulate roles so I know the database connection string is working.
I'm pretty new to EF6 Code First and Identity 2.0 so I'm sure I'm missing something here.
The immediate use I need _userManagement for is to access the .GetRoles(userId) method. I've written a work around to get the data using:
List<string> rolesMemberOf = (from x in identDb.Users.Where(x => x.Id == userId).FirstOrDefault().Roles.Select(x => x.RoleId)
join y in _roleManager.Roles on x equals y.Id
select y.Name).ToList();
However it seems it would be much cleaner and easier if I can fix UserManager.
Edit:
It looks like changing:
_userManager = new UserManager<IdentityUser>(new UserStore<IdentityUser>(identDb));
To:
_userManager = new ApplicationUserManager(new UserStore<ApplicationUser>(identDb));
Is working now. However I'm still unclear why IdentityUser wasn't working or what differences it has versus ApplicationUser.
Upvotes: 1
Views: 387
Reputation: 745
It looks like changing:
_userManager = new UserManager<IdentityUser>(new UserStore<IdentityUser>(identDb));
To:
_userManager = new ApplicationUserManager(new UserStore<ApplicationUser>(identDb));
Is working now. However I'm still unclear why IdentityUser wasn't working or what differences it has versus ApplicationUser.
Upvotes: 1