This question has evolved so I've updated the title.
This was the original title: Identity 2 UserManager.Find throws "Invalid object name 'dbo.ApplicationUser'" error
I'm converting from SimpleMembership to Identity 2. I've ran the conversion script and refactored the various files for Identity use. I can build and run the app but when attempt to login an "Invalid object name 'dbo.ApplicationUser'" error is thrown on var user = UserManager.Find(vM.UserName, vM.Password);
Account Controller:
[RequireHttps]
[Authorize]
public class AccountController : Controller
{
private readonly IUserService _userService;
public UserManager<ApplicationUser> UserManager { get; private set; }
public AccountController()
: this(new UserService(), new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new MyDb()))) { }
public AccountController(IUserService userService, UserManager<ApplicationUser> userManager)
{ _userService = userService; UserManager = userManager; }
// GET: /Account/Login
[AllowAnonymous]
public ActionResult Login() { return View(); }
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginVm vM)
{
if (ModelState.IsValid)
{
var user = UserManager.Find(vM.UserName, vM.Password);
if (user != null)
{
FormsAuthentication.SetAuthCookie(user.UserName, false);
return RedirectToAction("Index", "Home");
}
}
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(vM);
}
ApplicationUser:
public class ApplicationUser : IdentityUser
{
[StringLength(15)]
public new string UserName { get; set; }
public int AcId { get; set; }
public int LcId { get; set; }
public string ConfirmationToken { get; set; }
public bool IsConfirmed { get; set; }
public string PasswordResetToken { get; set; }
}
DbContext:
public class MyDb : IdentityDbContext<ApplicationUser> // DbContext
{
public MyDb() : base("MyApplicaiton") { }
// public virtual DbSet<UserProfiles> Users { get; set; }
public virtual DbSet<MyTable> MyTables { get; set; } // properties marked virtual for Mocking override
...
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
}
}
Why is user manager trying to access dbo.[ApplicationUser] (which doesn't exist) rather than dbo.[AspNetUsers]?
UPDATE 1: I downgraded to Microsoft.AspNet.Identity.EntityFramework 1.0 and Microsoft.AspNet.Identity.Core 1.0 and I now get a "Invalid object name 'dbo.IdentityUser'" error when UserManager.Find is called.
UPDATE 2:
I upgraded back to Identity 2.0 and just to see what would happen backed up and deleted the database and regenerated it with code first (enable-migrations, update-database).
Instead of adding the default Identity tables of:
AspNetRoles
AspNetClaims
AspNetUserLogins
AspNetUserRoles
AspNetUsers
It added these tables:
dbo.ApplicationUser
dbo.IdentityRole
dbo.IdentityUserClaim
dbo.IdentityUserLogin
dbo.IdentityUserRole
Which would explain why it is looking for ApplicationUser. What is it about my configuration that is forcing those names instead of the standard Identity names? I probably could change my migration script to those names but then I would end up with non-standard table names which would only lead to confusion going forward. How do I configure things to get the default Identity table names?