postgresql EF核心如何使用数组来建立关系?

f0ofjuux  于 2022-12-03  发布在  PostgreSQL
关注(0)|答案(1)|浏览(106)

我有一些实体

public class Post
{
    public Guid Id { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    public Guid[] TagIds { get; set; }
    public ICollection<Tag> Tags { get; set; }
}

public class Tag
{
    public string Id { get; set; }
    public string Name { get; set; }
}

如果posts.tag_ids是uuid[],如何建立关系?我想使用

_dbContext.Posts.Include(x => x.Tags)

预期为SQL

select * from posts p
left join tags t on t.id = any(p.tag_ids)
8yparm6h

8yparm6h1#

这是

public Guid[] TagIds { get; set; }

不是关系数据库建模关系的方式。如果要关联实体,请尝试以下操作:

public class Post
{
    public Guid Id { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    public ICollection<Tag> Tags { get; set; }
}

public class Tag
{
    public string Id { get; set; }
    public string Name { get; set; }

    public ICollection<Post> Posts { get; set; }
}

它将在后台创建一个链接表。

相关问题