[英]One-to-many MVC Individual User Accounts
我在个人用户帐户模板中使用asp.net mvc,在该模板中尝试建立从用户到项目的一对多关系。
在没有个人用户帐户模板的项目中,我可以简单地执行以下操作:
public class User
{
public int UserID { get; set; }
public string Username { get; set; }
public virtual ICollection<Item> Items { get; set; }
}
和
public class Item
{
public int ItemID { get; set; }
public string ItemName { get; set; }
public int UserID { get; set; }
public virtual User User { get; set; }
}
问题是我无法在个人用户帐户模板中找到用户(ApplicationUser)的完整模型。 所以我不知道如何将项目添加到用户并为用户生成控制器(在单个用户模板中)
从“模型”文件夹中的模板中,在“ IdentityModels”中:
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
// Add here ..
public string DisplayName { get; set; }
public virtual ICollection<Item> Items { get; set; }
}
通过这种方式在ApplicationUser
模型中使用身份关系。 您还可以在此处将名称ApplicationUser
更改为User
,前提是对ApplicationUser
所有其他引用也已更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.