I have read lots of other posts/discussions regarding this error and have not found a clear answer.
I have customised ASP.NET Identity to store FirstName
and Country
as well as username/password/email address, and I'm now having issues actually storing this within the generated tables within my database.
I have already enabled migrations, added a migration and updated the database to reflect the 2 additional fields within my Identity.
Error at IdentityResult result = manager.Create(user, model.Password);
$exception {"The entity type ApplicationUser is not part of the model for the current context."} System.Exception {System.InvalidOperationException}
web.config:
<connectionStrings>
<remove name="DefaultConnection" />
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=WIN8-SUNEETAGU\SQLSERVER;Initial Catalog=UnderConstruction;Integrated Security=True" />
</connectionStrings>
AuthenticationModels.cs
namespace UnderConstruction.Models
{
public class AuthenticationModels
{
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string Country { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<UnderConstruction.Models.AuthenticationModels.ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{}
}
public class RegisterModel
{
[Required]
[StringLength(10, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 5)]
[Display(Name = "Username")]
public string Username { get; set; }
[Required]
[StringLength(10, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 4)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Required]
[StringLength(10, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 4)]
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
public string ConfirmPassword { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
[EmailAddress(ErrorMessage="Please enter a valid email address.")]
[Display(Name = "Email address")]
public string Email { get; set; }
[Required]
[StringLength(10, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 5)]
[Display(Name = "First name")]
public string FirstName { get; set; }
[Required]
[StringLength(50)]
[Display(Name = "Country")]
public string Country { get; set; }
}
}
}
AuthenticationController.cs
public class AuthenticationController : Controller
{
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(AuthenticationModels.RegisterModel model)
{
if (ModelState.IsValid)
{
try
{
var userStore = new UserStore<UnderConstruction.Models.AuthenticationModels.ApplicationUser>();
var manager = new UserManager<UnderConstruction.Models.AuthenticationModels.ApplicationUser>(userStore);
var user = new UnderConstruction.Models.AuthenticationModels.ApplicationUser() { UserName = model.Username, Email = model.Email, FirstName = model.FirstName, Country = model.Country };
IdentityResult result = manager.Create(user, model.Password);
return View("RegisterSuccessful");
}
catch (Exception e)
{
ModelState.AddModelError("", e);
}
}
return View("Register",model);
}
}