User1415305511 posted
I get this error when I tried to use ASP.NET identity on an existing database.
Here's my context class :
public partial class sampleUserEntities : IdentityDbContext<sampleUserTbl>
{
public sampleUserEntities()
: base("name=sampleUserEntities")
{
}
public static sampleUserEntities Create()
{
return new sampleUserEntities();
}
public virtual DbSet<sampleUserTbl> sampleUserTbls { get; set; }
}
Here is the class that inherits from IdentityUser :
public partial class sampleUserTbl : IdentityUser
{
public string Password { get; set; }
public Nullable<bool> User_Flag { get; set; }
public Nullable<bool> Emp_Flag { get; set; }
public Nullable<bool> Pat_Flag { get; set; }
}
And here's my AccountController :
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginModel details, string returnUrl)
{
if (ModelState.IsValid)
{
sampleUserTbl user = await UserManager.FindAsync(details.UserName, details.Password);
if (user == null)
{
ModelState.AddModelError("", "Invalid name or password.");
}
else
{
ClaimsIdentity claimsident = await UserManager.CreateIdentityAsync(user,
DefaultAuthenticationTypes.ApplicationCookie);
AuthenticationMgr.SignOut();
AuthenticationMgr.SignIn(new AuthenticationProperties { IsPersistent = false }, claimsident);
return RedirectToLocal(returnUrl);
}
}
ViewBag.returnUrl = returnUrl;
return View(details);
}
While here's the UserViewModel :
public class CreateModel
{
[Required]
public string UserName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
public class LoginModel
{
[Required]
public string UserName { get; set; }
[Required]
public string Password { get; set; }
}
Login is successful when I am using code first. However I need to use it on the existing database. Any idea why it generates that error ? Thank you very much !