简体   繁体   English

实体框架核心2未急于加载包含的实体

[英]Entity Framework Core 2 is not eagerly loading Included entity

I have some code which I'm upgrading from dotnetcore1.1 to dotnetcore2.0 (and correspondingly EF core from 1 to 2) 我有一些代码要从dotnetcore1.1升级到dotnetcore2.0(以及相应的EF核心,从1升级到2)

I have the following Model (anonymised, of course), and using Npgsql to talk to a postgres database. 我有以下模型(当然是匿名的),并且使用Npgsql与postgres数据库进行通信。

[Table("company")]
public class Company
{
  [Key, Column("id")]
  public int Id { get; set; }

  [Column("name", TypeName = "VARCHAR")]
  public string Name { get; set; }

  [Column("serialnumber")]
  public int SerialNumber { get; set; }

  [ForeignKey("serialnumber")]
  public virtual CompanyTypeMarker CompanyTypeMarker { get; set; }
}

[Table("companytypemarker")]
public class CompanyTypeMarker
{
    [Key, Column("serialnumber"), DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int SerialNumber { get; set; }

    [Column("sitetype")]
    public CompanyType CompanyType { get; set; } = CompanyType.Customer;
}

I have this LINQ query: 我有这个LINQ查询:

var company = await db.Companies
  .Include(c => c.CompanyTypeMarker)
  .FirstOrDefault(cc => cc.SerialNumber == serialNumber);

In dotnetcore1.1, the Company entity would load, and it's associated CompanyTypeMarker entity would also be loaded and accessible via the Company.CompanyTypeMarker property 在dotnetcore1.1中,将加载Company实体,并且其关联的CompanyTypeMarker实体也将通过Company.CompanyTypeMarker属性加载并访问

In dotnetcore2.0, the Company entity loads, but the Company.CompanyTypeMarker entity is null. 在dotnetcore2.0中,将加载Company实体,但是Company.CompanyTypeMarker实体为null。

Looking at the log output, EF core is generating the following SQL: 查看日志输出,EF内核正在生成以下SQL:

  SELECT "c"."id", "c"."name", "c"."serialnumber", "c.CompanyTypeMarker"."serialnumber", "c.CompanyTypeMarker"."companytype"
  FROM "company" AS "c"
  INNER JOIN "companytypemarker" AS "c.CompanyTypeMarker" ON "c"."serialnumber" = "c.CompanyTypeMarker"."serialnumber"
  WHERE "c"."serialnumber" = @__SerialNumber_0
  LIMIT 1

The generated SQL is evidence that EF core is picking up the navigation property, as it generates the correct sql JOIN for the correct properties... Just that when it comes back into C#, for some reason it's not picking up and populating the JOINed values. 生成的SQL证明EF核心正在选择导航属性,因为它为正确的属性生成了正确的sql JOIN ...只是当它返回C#时,由于某种原因,它没有选择并填充JOINed值。

Can anyone please help? 谁能帮忙吗? As noted previously, this works in EF Core 1, and it seems like fairly standard use of navigation properties 如前所述,这在EF Core 1中有效,并且似乎相当标准地使用了导航属性

Ok, turns out I also had this in my OnModelCreating 好的,原来我在OnModelCreating中也有这个

modelBuilder
  .Entity<Company>(entity => {
    entity.HasOne(x => { x.CompanyTypeMarker).WithOne().OnDelete(DeleteBehavior.Restrict);
  })

I changed it to HasOne(...).WithMany() and it eagerly loads correctly now 我将其更改为HasOne(...).WithMany() ,现在它急切地正确加载

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

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