Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET Core 3.0将身份用户分配给自定义模型_C#_Asp.net_Asp.net Core - Fatal编程技术网

C# ASP.NET Core 3.0将身份用户分配给自定义模型

C# ASP.NET Core 3.0将身份用户分配给自定义模型,c#,asp.net,asp.net-core,C#,Asp.net,Asp.net Core,假设我有一个模型Post.cs,你想添加创建此帖子的用户。我想我可以直接使用身份用户并将其附加到帖子中,或者只添加其ID。我不想扩展它,只是想把它分配到岗位上。这是我的定制模型: public class Post { public int Id { get; set; } public string Title { get; set; } public string Content { get; set; } public string UserId {

假设我有一个模型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; }
}

这将在用户和帖子之间创建一对多关系。Post有一个用户,但用户可以有多个Post。你也扩展了身份。这是一个正确的答案,我实现了我想要的。谢谢,还有一个问题。通过上面的代码,当我请求获取创建它们的用户的所有帖子时,我的UserId字段填充了Identity service中用户的id,但是字段user为null,应该通过引用UserId来获取吗?请阅读本文,它解释了EF中的include方法。