简体   繁体   English

如何为一对多LINQ联接正确设置模型和dbcontext

[英]how to set up models and dbcontext correctly for a one-many LINQ join

consider this: 考虑一下:

company model: 公司型号:

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

report model: 报告模型:

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

company2report (where one company can have many reports): company2report(一个公司可以有许多报告):

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

where dbcontext is: dbcontext是:

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

if i wanted to join this in sql it is simply: 如果我想在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

so here is my linq 所以这是我的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
                 });

which is all great except this fails 一切都很好,除了失败
the sql which is generated looks like this: 生成的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

(see the first join on 3rd line) it generates COMPANY2REPORT rather than COMPANY2REPORTS (请参阅第3行的第一个联接),它生成COMPANY2REPORT而不是COMPANY2REPORTS

so then i tried adjusting the c2r model to look like this: 因此,我尝试将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; }

which made it worse 更糟的是
how do i get my models to work with my context and then to work with the join? 如何使我的模型与上下文配合使用,然后与联接配合使用?

---------------------------------------------------------------- -------------------------------------------------- --------------
UPDATE: 更新:
the answer supplied below is correct 下面提供的答案是正确的
but i wanted to clarify on a theoretical level: 但我想在理论上进行澄清:
it is because i was mixing my modes with relational db Fkeys vs. object association 这是因为我将我的模式与关系db Fkeys与对象关联混合在一起

in the end i chose to go with object association 最后,我选择了对象关联
i adjusted my models and code to look like this: 我调整了模型和代码,使其看起来像这样:

company model: 公司型号:

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

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

report model: 报告模型:

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 DELETED company2report已删除

dbcontext: 的DbContext:

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

linq: LINQ:

        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;

If you want to create an one-to-many relationship between those two entities your model would be like this: 如果要在这两个实体之间创建一对多关系,则模型将如下所示:

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

A company has many reports, each with one company. 一个公司有很多报告,每个报告都有一个公司。 That relationship is represented as follows: 该关系表示如下:

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

It can also be configured by starting with the other end of the relationship: 也可以从关系的另一端开始进行配置:

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

And dbContext 和dbContext

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

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

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