简体   繁体   English

一对多关系的 EF Core 联接表

[英]EF Core Join Table for One To Many relationship

I have a Database Schema like this.我有一个这样的数据库模式。

数据库架构

I am working on gettingDetailByPostID API which needs to return 1 Object as follows.我正在获取DetailByPostID API,它需要返回1 Object,如下所示。

    Guid PostId;
    Guid UserId;
    String? Username;
    String? MediaPath;
    int Likes; // count the number of likes of the post
    String? Caption;
    bool IsLiked;
    IEnumerable<Comment>? Comments; // List of comments in the post

I am having difficulty in joining the tables together to get more data from the User and Comment tables.我很难将这些表连接在一起以从 User 和 Comment 表中获取更多数据。 How can I do this with ef core 6.我怎么能用 ef core 6 做到这一点。

firsr have a DbSet property for in your db context inherited class which may look like this public DbSet<Post> Posts { get; set; }首先在您的数据库上下文中有一个DbSet属性继承 class 可能看起来像这个public DbSet<Post> Posts { get; set; } public DbSet<Post> Posts { get; set; } public DbSet<Post> Posts { get; set; } . public DbSet<Post> Posts { get; set; }

then inject the dbcontext to your required service as constructor injection.然后将 dbcontext 作为构造函数注入注入到您所需的服务中。

the you can do var post = yourDbContenxt.Posts.FirstOrDefaultAsync(p=>p.PostId==yourPostId);你可以做var post = yourDbContenxt.Posts.FirstOrDefaultAsync(p=>p.PostId==yourPostId);

The best way to do this would be in a single transaction.最好的方法是在单个事务中。 I believe you already have configured EF with navigation and you should have an entities like this我相信您已经为 EF 配置了导航,并且您应该拥有这样的实体

    public class Post
    {
        public int post_id { get; set; }
        public int user_id { get; set; }
        public string caption { get; set; }
        public string? media_path { get; set; }
        virtual public List<Like> Likes { get; set; }

    }
    public class Like
    {
        public int post_id { get; set; }
        public int user_id { get; set; }
        virtual public Post Post { get; set; }
        virtual public UserProfile UserProfile { get; set; }
    }
    public class UserProfile
    {
        public int post_id { get; set; }
        public int user_id { get; set; }
        public string username { get; set; }
        virtual public Post Post { get; set; }
        virtual public List<Like> Likes { get; set; }
    }

If not, you should have a look on the microsoft documentation or on this great tutorial site如果没有,您应该查看microsoft 文档或这个很棒的教程站点

If it is configured with navigation (but without lazy loading to avoid infinite loop, and to have an ecological use of your server ressources:) )如果它配置了导航(但没有延迟加载以避免无限循环,并生态使用您的服务器资源:))

Then you can build your object within the same context instanciation such as this short exemple然后您可以在相同的上下文实例中构建您的 object,例如这个简短的示例


 public class DetailedPost
    {
        Guid PostId;
        Guid UserId;
        String? Username;
        String? MediaPath;
        int Likes; // count the number of likes of the post
        String? Caption;
        bool IsLiked;
        IEnumerable<Comment>? Comments; // List of comments in the post
        
        public DetailedPost GettingDetailByPostID(int id)
        {
            DbContext context = new DbContext();

            UserProfile userprofile = context.UserProfile
                .Include(user => user.Posts.Where(p => p.post_id == id)) // Select post by id
                .ThenInclude(post => post.Comments) // Include the Comment Db relative to post selected
                .Include(user => user.Likes); // Include like db relative to userProfile db

// Return object construction
            return new DetailedPost()
            {
                PostId = userprofile.post_id,
                UserId = userprofile.user_id,
                Username = userprofile.username,
                Caption = userprofile.Posts.FirstOrDefault().Caption,
                Likes = userprofiles.Likes.Count(),

            };

        }
    }

I hope it helps !我希望它有帮助!

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

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