简体   繁体   English

ASP.NET Core 3.0 将身份用户分配给自定义 model

[英]ASP.NET Core 3.0 Assign Identity User to custom model

Lets say i have model Post.cs and u want to add user who create this post.I think i can use Identity users directly and attach them to post, or maybe only their ID's.假设我有 model Post.cs 并且您想添加创建此帖子的用户。我想我可以直接使用身份用户并将他们附加到帖子中,或者可能只是他们的 ID。 I don't want to extend it, just to assign it to the post.我不想扩展它,只是将它分配给帖子。 Here is my custom model:这是我的自定义 model:

 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; }
}

On the fourth and fifth row is the APS.NET default identity user data that i want to assign to posts第四行和第五行是我要分配给帖子的 APS.NET 默认身份用户数据

I am not sure what is your question exactly.我不确定你的问题到底是什么。 If you are trying to create a one-to-many relationship, see this simple example.如果您尝试创建一对多关系,请参阅这个简单的示例。

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; }
}

This will create a one-to-many relationship between User and Posts.这将在用户和帖子之间创建一对多的关系。 Post has one User but User can have multiple Posts.帖子有一个用户,但用户可以有多个帖子。 With that you have also extended the IdentityUser.这样,您还扩展了 IdentityUser。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM