だから私は最近MVC 5で遊んでいて、とても気に入っています。簡単にできると思っていたのに、一日のうち何時間も食べてしまい、賢明ではありませんでした。
基本的に、独自のプロパティを追加してUserモデルを拡張したいと考えています。これは私がしました:
public class IdentityUser : User
{
public IdentityUser()
//: this(String.Empty)
{
}
public IdentityUser(string userName)
: base(userName)
{
}
public string UserId { get; set; }
public string CompanyId { get; set; }
[Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "RequiredTitle")]
public string Title { get; set; }
[Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "RequiredFirstName")]
public string Forename { get; set; }
[Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "RequiredLastName")]
public string Surname { get; set; }
[Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "RequiredUserName")]
public string UserName { get; set; }
[Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "RequiredEmail")]
public string Email { get; set; }
public string JobTitle { get; set; }
public string Telephone { get; set; }
public string Mobile { get; set; }
public string Photo { get; set; }
public string LinkedIn { get; set; }
public string Twitter { get; set; }
public string Facebook { get; set; }
public string Google { get; set; }
public string Bio { get; set; }
public string CompanyName { get; set; }
[Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "RequiredCredentialId")]
public string CredentialId { get; set; }
[Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "RequiredSecurityCode")]
public string Code { get; set; }
public bool IsLockedOut { get; set; }
public bool IsApproved { get; set; }
public DateTime LastLoginDate { get; set; }
[Display(Name = "Can only edit own assets")]
public bool UserCanEditOwn { get; set; }
[Display(Name = "Can edit assets")]
public bool UserCanEdit { get; set; }
[Display(Name = "Can download assets")]
public bool UserCanDownload { get; set; }
[Display(Name = "Require approval to upload assets")]
public bool UserRequiresApproval { get; set; }
[Display(Name = "Can approve assets")]
public bool UserCanApprove { get; set; }
[Display(Name = "Can synchronise assets")]
public bool UserCanSync { get; set; }
public bool AgreedTerms { get; set; }
}
public class IdentityUserContext : IdentityStoreContext
{
public IdentityUserContext(DbContext db)
: base(db)
{
this.Users = new UserStore<IdentityUser>(this.DbContext);
}
}
public class IdentityUserDbContext : IdentityDbContext<IdentityUser, UserClaim, UserSecret, UserLogin, Role, UserRole>
{
}
Userは EF の既定の User クラスです
namespace Microsoft.AspNet.Identity.EntityFramework
{
public class User : IUser
{
public User();
public User(string userName);
[Key]
public string Id { get; set; }
public string UserName { get; set; }
}
}
カスタムクラスを使用したい場合はそれをしなければならないことをどこかで読んだので、私はそれを継承しました。とにかく、次に Register モデルを次のように変更しました。
public class RegisterViewModel
{
[Required]
[Display(Name = "User name")]
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; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
public string Title { get; set; }
[Required]
public string Forename { get; set; }
[Required]
public string Surname { get; set; }
[Required]
public string Email { get; set; }
public string CompanyName { get; set; }
[Required]
public string CredentialId { get; set; }
[Required]
public string Code { get; set; }
}
メソッドは次のようになります。
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
try
{
// Create a profile, password, and link the local login before signing in the user
var companyId = Guid.NewGuid().ToString();
var user = new IdentityUser( model.UserName)
{
CompanyId = companyId,
Title = model.Title,
Forename = model.Forename,
Surname = model.Surname,
Email = model.Email,
CompanyName = model.CompanyName,
CredentialId = model.CredentialId
};
if (await IdentityStore.CreateLocalUser(user, model.Password))
{
await AuthenticationManager.SignIn(HttpContext, user.Id, isPersistent: false);
return RedirectToAction("Setup", new { id = user.CompanyId });
}
else
{
ModelState.AddModelError("", "Failed to register user name: " + model.UserName);
}
}
catch (IdentityException e)
{
ModelState.AddModelError("", e.Message);
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
登録しようとすると、次のエラーが表示されます。
Mapping and metadata information could not be found for EntityType 'TestProject.Models.IdentityUser'.
理由がわかりません。
ステップか何かが欠けていますか?
どんな助けでも大歓迎です