Sql server EF多对多核心线路组

Sql server EF多对多核心线路组,sql-server,linq,asp.net-core,entity-framework-core,Sql Server,Linq,Asp.net Core,Entity Framework Core,我有一个多对多关系表,试图在表中显示报告列表 我的桌子看起来像这样: 报告表: Id| ReportName | 1 | report 1 | 2 | report 2 | 3 | report 3 | 报告类别表: Id| Name | 1 | General | 2 | Specific | 报表映射连接表: Id| ReportId | CategoryId | 1 | 1 | 1 | 2 | 1 | 2

我有一个多对多关系表,试图在表中显示报告列表

我的桌子看起来像这样:

报告表:

Id| ReportName |
1 | report 1   |
2 | report 2   |
3 | report 3   |
报告类别表:

Id| Name     |
1 | General  |
2 | Specific |
报表映射连接表:

Id| ReportId | CategoryId |
1 | 1        | 1          |
2 | 1        | 2          |
3 | 2        | 1          |
4 | 2        | 2          |
报告可以有多个类别在本例中只有2个,但也可能有更多类似的类别,比如说1个报告可以有5个类别,如常规、特定、Test2、Test3和Test4

我想在我的.net核心应用程序的表/列表上显示一种格式,大致如下:

ReportId| Report Name | Report Categories
1       | report 1    | General, Specific
2       | report 2    | General, Specific
我在sql server和EF core linq中都很难实现这一点。有关于如何开始的建议吗?到目前为止,我能够将这些表连接在一起,但不知道如何将我的结果连接到具有多个类别的报告的一行中。我得到的是下面的结果,而不是上面的例子:

ReportId | Report Name | Report Categories
1        | report 1    | General
1        | report 1    | Specific
2        | report 2    | General
2        | report 2    | Specific

任何帮助都将不胜感激,谢谢

您所描述的模型与EF核心文档中示例中的
Post
/
标签
模型几乎相同

因此,有3个类表示表记录

public class Report
{
    public int Id { get; set; }
    public string ReportName { get; set; }
    public ICollection<ReportMapping> Mappings { get; set; }  // navigation
}

public class ReportCategory
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<ReportMapping> Mappings { get; set; } // navigation
}

public class ReportMapping
{
    public int Id { get; set; }
    public int ReportId { get; set; }
    public int CategoryId { get; set; }
    public Report Report { get; set; } // navigation
    public ReportCategory Category { get; set; } // navigation
}

注意,结实体(表)中的<代码> ID>代码>属性(列)是冗余的,因此,在不被约束到现有数据库的情况下,考虑删除它,并在示例

中配置复合PK。
modelBuilder.Entity<ReportMapping>()
    .HasKey(e => new { e.ReportId, e.CategoryId });
注意,我将
ReportCategories
定义为字符串序列,而不是单个字符串。这是因为首先,数据库本身不支持结果集的字符串连接,其次,使用逗号连接只是显示此数据的多种方式之一。通常,数据的格式化是客户机的责任。因此,您以原始本机格式(字符串列表)返回数据,并让客户机对其进行格式化(在本例中,它可以通过使用
string.Join(“,”,info.ReportCategories)
)轻松实现这一点)

最后是实际查询。有了导航属性,它非常简单-基本上只需选择s:

var result = db.Reports
    .Select(r => new ReportInfo
    {
        ReportId = r.Id,
        ReportName = r.ReportName,
        ReportCategories = r.Mappings
            .Select(m => m.Category.Name)
            .ToList() // <-- to avoid N + 1 subquery in EF Core 2.1+
    })
    .ToList();
var result=db.Reports
.选择(r=>newreportinfo
{
ReportId=r.Id,
ReportName=r.ReportName,
ReportCategories=r.映射
.Select(m=>m.Category.Name)

.ToList()//啊,非常感谢你,你的解释也很清楚。
public class ReportInfo
{
    public int ReportId { get; set; }
    public string ReportName { get; set; }
    public IEnumerable<string> ReportCategories { get; set; }
}
var result = db.Reports
    .Select(r => new ReportInfo
    {
        ReportId = r.Id,
        ReportName = r.ReportName,
        ReportCategories = r.Mappings
            .Select(m => m.Category.Name)
            .ToList() // <-- to avoid N + 1 subquery in EF Core 2.1+
    })
    .ToList();