首页 > 解决方案 > 如何为一对多 LINQ 连接正确设置模型和 dbcontext

问题描述

考虑一下:

公司型号:

public int id { get; set; }
public string name{ get; set; }

报告模型:

public int id { get; set; }
public string title { get; set; }

company2report(一家公司可以有许多报告):

public int id { get; set; }
public int companyID { get; set; }
public int reportID{ get; set; }
public int sortorder{ get; set; }

其中 dbcontext 是:

public DbSet<company> companies { get; set; }
public DbSet<report> reports { get; set; }
public DbSet<company2report> company2reports { get; set; }

如果我想在 sql 中加入它,它很简单:

SELECT *
FROM   companies as c
INNER JOIN company2reports as c2r ON c.id = c2r.companyID
INNER JOIN reports as r ON c2r.reportID = r.id
ORDER BY c2r.sortorder ASC

所以这是我的 linq

    var q = (from c in db.companies
              join c2r in db.company2reports on c.id equals c2r.companyID
              join r in db.hreports on c2r.reportID equals r.id
              orderby c2r.sortorder
              select new {
                     c.id, c.name,
                     r.id, r.title
                 });

这一切都很好,除了这失
​​败了,生成的 sql 看起来像这样:

SELECT c.id, c.name, r.id, r.title
FROM   companies as c
INNER JOIN company2report as c2r ON c.id = c2r.companyID
INNER JOIN reports as r ON c2r.reportID = r.id
ORDER BY c2r.sortorder ASC

(参见第 3 行的第一个连接)它生成 COMPANY2REPORT 而不是 COMPANY2REPORTS

所以然后我尝试将 c2r 模型调整为如下所示:

public int id { get; set; }
public int companyID { get; set; }
public int reportID{ get; set; }
public ICollection<report> reports { get; set; }
public int sortorder{ get; set; }


这让我如何让我的模型与我的上下文一起工作然后与加入一起工作 变得更糟了?

-------------------------------------------------- --------------
更新:
下面提供的答案是正确的
,但我想在理论上澄清:
这是因为我将我的模式与关系 db Fkeys 与对象关联混合在一起

最后我选择使用对象关联
我调整了我的模型和代码看起来像这样:

公司型号:

public class company
{
    public int id { get; set; }
    public string name { get; set; }

    // navigation properties
    public ICollection<report> reports { get; private set; }
}

报告模型:

public class report
{
    public int id { get; set; }
    public string title { get; set; }
    public int companyId { get; set; }
    public short sortorder { get; set; }

    // navigation properties
    public virtual company company { get; set; }
}

company2report 已删除

数据库上下文:

    public DbSet<company> companies { get; set; }
    public DbSet<report> reports { get; set; }
    //public DbSet<company2report> company2reports { get; set; }

林克:

        var q = from c in db.companies
                join r in db.reports on c.id equals r.companyId
                where c.id == 4
                orderby r.sortorder
                select r;

标签: c#asp.netlinqlinq-to-entities

解决方案


如果您想在这两个实体之间创建一对多的关系,您的模型将如下所示:

public class Company
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Report> Reports { get; set; }
}

public class Report
{
    public int Id { get; set; }
    public string Title { get; set; }
    public int? CompanyId { get; set; }
    public Company Company { get; set; }
}

一家公司有许多报告,每个报告都有一个公司。这种关系表示如下:

protected override void OnModelCreating(Modelbuilder modelBuilder)
{
    modelBuilder.Entity<Company>()
        .HasMany(c => c.Reports)
        .WithOne(e => e.Company);
}

也可以从关系的另一端开始配置:

protected override void OnModelCreating(Modelbuilder modelBuilder)
{
    modelBuilder.Entity<Report>()
        .HasOne(e => e.Company)
        .WithMany(c => c.Reports);
}

和 dbContext

public class SampleContext : DbContext
{
    public DbSet<Company> Companies { get; set; }
    public DbSet<Report> Reports { get; set; }
}

推荐阅读