c# - ASP.NET Core 3.0 将身份用户分配给自定义模型
问题描述
假设我有模型 Post.cs 并且您想添加创建此帖子的用户。我想我可以直接使用身份用户并将他们附加到帖子,或者可能只是他们的 ID。我不想扩展它,只是将它分配给帖子。这是我的自定义模型:
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public string UserId { get; set; }
public User User { get; set; }
public DateTime DateCreated { get; set; }
public DateTime? DateUpdated { get; set; }
public DateTime DateDeleted { get; set; }
public bool Deleted { get; set; }
public bool Approved { get; set; }
}
第四行和第五行是我要分配给帖子的 APS.NET 默认身份用户数据
解决方案
我不确定你的问题到底是什么。如果您尝试创建一对多关系,请参阅这个简单的示例。
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public string UserId { get; set; }
public User User { get; set; }
}
public class User : IdentityUser
{
public virtual ICollection<Post> Posts { get; set; }
}
这将在用户和帖子之间创建一对多的关系。帖子有一个用户,但用户可以有多个帖子。这样,您还扩展了 IdentityUser。
推荐阅读
- javascript - useEffect 的奇怪行为
- actionscript-3 - Actionscript-3 和 android-sqlite 问题
- ruby - 动态填充 Chef Cookbook 属性
- javafx - 场景中的 JavaFX 更新节点不起作用
- java - Http请求和响应没有返回任何东西
- javascript - 从左到右的动画列表
- python - 使用python和ffmpeg从dicts列表中的两个值循环
- node.js - 使用 typescript 和 express 时,我无法让 res.send 在我的控制器中使用显式类型,但是当类型为 anny 时它确实有效
- c# - c# DataTable 单元格更新后出现意外的空行
- appdynamics - 当我启动 app-dynamics 机器代理时,它没有将任何日志写入日志文件夹