c# - 一对多 MVC 个人用户帐户
问题描述
我将 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) 的完整模型。所以我不知道如何将项目添加到用户并为用户生成控制器(在单个用户模板中)
解决方案
从 Models 文件夹中的模板,在 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
也已更新。
推荐阅读
- c++ - 如何编写一个通用或灵活的函数,它可以接受任意数量的参数,以便对它们执行指定的任务?
- python - matplotlib 字体与 LaTeX 字体不匹配
- php - .htaccess RewriteRule 更改特定目录中的一些 php 页面
- angular - 将 css 道具应用到最后一个角度内
- sql - 如何设置每天自动更新的查询和聚合表?
- ios - 根据 UILabel 高度调整 UIView 大小
- node.js - 如何通过 SSH NPM 将私有存储库安装为依赖项
- javascript - 正则表达式从 git log 中多次匹配任何内容
- python - Python 将 xlsx 文件保存在另一个文件夹中,然后声明
- android - 在 org.gradle.api.plugins.quality.internal.findbugs.FindBugsXmlReportImpl 类型的报告 xml 上找不到参数的方法 destination()