[英]how to set up models and dbcontext correctly for a one-many LINQ join
考慮一下:
公司型號:
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已刪除
的DbContext:
public DbSet<company> companies { get; set; }
public DbSet<report> reports { get; set; }
//public DbSet<company2report> company2reports { get; set; }
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;
如果要在這兩個實體之間創建一對多關系,則模型將如下所示:
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; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.