[英]ASP.NET Applicationuser ICollection Entity insert UserId null
I have ApplicationUser class and with a ICollection like below: 我有ApplicationUser类,并具有以下ICollection:
public class ApplicationUser : IdentityUser
{
public virtual ICollection<Info> Infos { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
}
And Info Class: 和信息类:
public partial class Info
{
public int Id { get; set; }
[Required]
[StringLength(128)]
public string UserId { get; set; }
[Required]
public string Address { get; set; }
public virtual ApplicationUser AspNetUser { get; set; }
}
And when I create a applicationuser like this: 当我创建一个像这样的应用程序用户时:
IList<Info> infos = new List<Info>() { new Info() { Address = "testAddr"} };
var user = new ApplicationUser { UserName = model.Email, Email = model.Email, Addresses = a };
var result = await UserManager.CreateAsync(user, model.Password);
This will occur validation error:UserId is required.Is any thing I missed? 这将发生验证错误:必填UserId。我错过了什么吗?
In your Infos
class, the UserId
property doesn't relate to the navigation property AspNetUser
at all, this is why it is failing. 在您的Infos
类中, UserId
属性根本与导航属性AspNetUser
不相关,这就是它失败的原因。 You either need to name them correctly: 您要么需要正确命名它们:
[Required]
[StringLength(128)]
public string UserId { get; set; }
public virtual ApplicationUser User { get; set; }
Or you can add a ForeignKey
attribute: 或者,您可以添加ForeignKey
属性:
[Required]
[StringLength(128)]
public string UserId { get; set; }
[ForeignKey("UserId")]
public virtual ApplicationUser AspNetUser { get; set; }
From what I understand from your code , you need to store multiple address associated to a user. 据我从您的代码了解,您需要存储与一个用户关联的多个地址。 Your UserId property is decorated with [Required]
attribute therefore you will need to store UserId
along with Address
, the main problem is that UserId
will be auto generated after you do UserManager.CreateAsync
. 您的UserId属性装饰有[Required]
属性,因此您需要将UserId
和Address
一起存储,主要问题是在执行UserManager.CreateAsync
之后将自动生成UserId
。 So what I suggest is that you assign the list of address after user has been successfully created. 因此,我建议您在成功创建用户之后分配地址列表。
`var user = new ApplicationUser { UserName = model.Email, Email = model.Email,Addresses =a };
var result = await UserManager.CreateAsync(user, model.Password);
if(result.Succeeded)
{
//assign list of address to user
IList<Info> infos = new List<Info>() { new Info() { Address ="testAddr",UserId = User.Identity.GetUserId()} }
dbcontext.SaveChanges()
//login here
}`
Hope it helps. 希望能帮助到你。 :-) :-)
Do you want UserId to be auto-incremented? 您是否要自动增加UserId? Because right now it is not set anywhere. 因为现在没有在任何地方设置它。 You receive this error because you defined that this field is required: 因为定义了此字段为必填字段,所以收到此错误:
[Required]
[StringLength(128)]
public string UserId { get; set; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.