简体   繁体   English

ASP.NET Applicationuser ICollection实体插入UserId为null

[英]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]属性,因此您需要将UserIdAddress一起存储,主要问题是在执行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.

相关问题 ASP.NET 核心 - 如何将 IdentityDbContext 中的 ApplicationUser id 插入到 Employee Model 作为 UserId - ASP.NET Core - How to insert ApplicationUser id in IdentityDbContext into Employee Model as UserId 如何过滤ICollection <ApplicationUser> 关于ASP.NET MVC 5身份中的角色 - How to filter ICollection<ApplicationUser> on a Role in ASP.NET MVC 5's Identity 更新实体中的ICollection(实体框架6,ASP.NET Core) - Update ICollection in Entity (Entity Framework 6, ASP.NET Core) ASP.NET ChangePassword userId = null - ASP.NET ChangePassword userId = null ApplicationUser上的ASP.net Core 1.0映射属性返回null - ASP.net Core 1.0 Mapping property on ApplicationUser returning null ApplicationUser 始终为空 ASP.NET CORE 3.0 标识 - ApplicationUser always null ASP.NET CORE 3.0 Identity ASP.NET 视图中的 ICollection - ICollection in a view ASP.NET 在ASP.NET中获取当前的UserID并插入Access数据库 - Get current UserID in ASP.NET and insert into Access database asp.net中的会话超时后,HttpContext.Current.Session [“ UserID”]也不为空,即使Session [“ UserID”]为null - HttpContext.Current.Session[“UserID”] not null even Session[“UserID”] null after session timeout in asp.net ASP.NET MVC - 登录成功,但userId返回null - ASP.NET MVC - Login Success, but userId returns null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM