Reputation: 31
i'm using ASP.Net identity base on Entity framework for my MVC(5) app.
problem :
when i want to register a new user , i get a Entity validation error. i looked at the validation error property and i get 2 error(both the same actually) on this line :
var result = await UserManager.CreateAsync(user, model.Password);
Errors :
http://i62.tinypic.com/11acy0y.png
now.....i have no idea where is this 'UserId' field coming from. i looked at IdentityUser base classes and it does not belong to that either , there is just an "Id" field which works properly.
if more info is needed just mention in Comment plz.
So does anyone have any idea....what is this field ? and how to solve this?
Edit : forgot to mention i create a base user in seed method of dbinitializer....that works fine. but in controller i get this error.
Controller & related Action :
//
//POST : /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> register(Register_ViewModel model)
{
UserManager.UserValidator = new MyUserValidation();
//Complete User Object if Model is Valid
if (ModelState.IsValid)
var user = new Employee
{
.......
}
//Store Claims
........
// Where i get the error
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInAsync(user, IsPersistent: false);
return RedirectToAction("Index", "Home");
}
else
{
AddErrors(result);
}
}
return View(model);
}
View :
@model Univercity_Project.Models.ViewModels.Register_ViewModel
@{
ViewBag.Title = "Register_tab";
Layout = "~/Views/Shared/Layout.cshtml";
}
<h2>Register_tab</h2>
@using (Html.BeginForm("Register","Account",FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Employee</h4>
<hr />
<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist">
<!-- Some Nav Tabs Here! -->
</ul>
<!-- Tab panes -->
<div class="tab-content">
<!-- Nav-tabs Content Here! -->
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Submit" class="btn btn- default" />
</div>
</div>
</div>
Employee(or 'ApplicationUser' ) model :
public class Employee : IdentityUser
{
public string Employment_Number { get; set; }
public string Gender { get; set; }
public int National_ID { get; set; }
public string First_Name { get; set; }
public string Last_Name { get; set; }
public string Father_Name { get; set; }
public string Birth_Place { get; set; }
public DateTime Birth_Date { get; set; }
public string Serial_Alphabetic { get; set; }
public int Serial_Numeric { get; set; }
public int Number_of_People_Under_Support { get; set; }
//<----------------------------------------------------------------------------------------------------------->
public virtual Employment_Status Employment_Status { get; set; }
public int Employment_Info_ID { get; set; }
public virtual Marriage_Status Marriage_Status { get; set; }
public int Marriage_Info_ID { get; set; }
public virtual Job_Status Job_Status { get; set; }
public virtual Academic_Status Academic_Status { get; set; }
public virtual Phone_Number Phone_Number { get; set; }
public virtual War_Record War_Record_Info { get; set; }
public virtual Address Address { get; set; }
public virtual Insuarance Insuarance_Info { get; set; }
}
Thank u in advance.
Upvotes: 1
Views: 3313
Reputation: 454
Has the Id field been set to Identity within the EF mapping for your User POCO:
`Property(c => c.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
EDIT:
I will Include my comment in this answer, which led to the solution:
there are other places where the userId is referenced. try adding the following to test as well:
user.Claims.Add(new IdentityUserClaim {UserId = user.Id}); user.Roles.Add(new IdentityUserRole {UserId = user.Id});
Upvotes: 0
Reputation: 31
ok here is the solution :
in the part that i was adding Claims i didnt know that i should assign value to UserId Property here is what i was doing :
user.Claims.Add(new IdentityUserClaim() { ClaimType = ClaimTypes.Gender, ClaimValue = user.Gender});
and here is what i should have done :
user.Claims.Add(new IdentityUserClaim() { ClaimType = ClaimTypes.Gender, ClaimValue = user.Gender, UserId = user.Id });
Upvotes: 1