身份ASP.NET MVC在更改AspNetUsers表时收到错误 [英] Identity ASP.NET MVC getting error when changing the AspNetUsers table
本文介绍了身份ASP.NET MVC在更改AspNetUsers表时收到错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在一整天中,我只是尝试将一个属性添加到由identity生成的AspNetUsers表中。我希望该属性成为客户表的外键,以便每个客户端拥有自己的用户,客户端的所有数据都在相应的客户表中。
所以这就是我的意思已完成:
在IdentityModels中:
During the whole day I just tried to add a single property to my AspNetUsers table generated by identity. I wanted that property to be a foreign key to a "Clients" table so that each client has it's own user and all the data of the client is in that respective clients table.
So here is what I've done:
In the IdentityModels:
public class ApplicationUser : IdentityUser
{
[ForeignKey("Client")]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int ClientID { get; set; }
public virtual Client Client { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Tenga en cuenta que el valor de authenticationType debe coincidir con el definido en CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Agregar reclamaciones de usuario personalizado aquí
return userIdentity;
}
}
并且在AccountController中
And in the AccountController
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
//Para crear el cliente y el contexto
var currentClient = new Client { Email = model.Email, Name = model.Name, Surname = model.Surname, PhoneNumber = model.PhoneNumber };
var context = new TNEAContext();
context.Clients.Add(currentClient);
context.SaveChanges();
//Esto ya venia con el codigo
var user = new ApplicationUser { UserName = model.Email, Email = model.Email, ClientID = currentClient.ClientID };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
// Para obtener más información sobre cómo habilitar la confirmación de cuenta y el restablecimiento de contraseña, visite http://go.microsoft.com/fwlink/?LinkID=320771
// Enviar correo electrónico con este vínculo
// string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
// var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
// await UserManager.SendEmailAsync(user.Id, "Confirmar cuenta", "Para confirmar la cuenta, haga clic <a href=\"" + callbackUrl + "\">aquí</a>");
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
// Si llegamos a este punto, es que se ha producido un error y volvemos a mostrar el formulario
return View(model);
}
以及accountViewModel和寄存器视图中的一些其他细微更改(为了获取这些参数)。
我知道我的问题在于UserManager.CreateAsync方法,但我不知道如何修改它,或者我需要做什么为了做那个让我发疯的小改变:(
推荐答案
在回复所有评论时,是的,我做了迁移(但没有添加任何内容)这是我得到的错误(是的,它是关于迁移):
http://i.imgur.com/6NBiYXc.jpg [ ^ ]
In reply to all comments, yeah I did migrations (but nothing was added) and this is the error I'm getting (and yes it is about migrations):
http://i.imgur.com/6NBiYXc.jpg[^]
这篇关于身份ASP.NET MVC在更改AspNetUsers表时收到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文