当我们做多表查询时  用Include 强制加载 或用 select 去查询时  发现生成的SQL语句 有时是左连接  有时是inner join。

其实EF是根据我们实体类的连接字段 是否可空来判断的~比如外键 RefID 

inner join写法:

  public  int RefID { get; set; }

在OnModelCreating事件中配置

modelBuilder.Entity<MyProject>.HasRequired(r=>r.RefProject).WithMany().HasForeignKey(t=>t.RefId); 

 

left join写法:

public  int? RefID { get; set; }

在OnModelCreating事件中配置 

modelBuilder.Entity<MyProject>.HasOptional(r=>r.RefProject).WithMany().HasForeignKey(t=>t.RefId);

红色部分为两者区别,要特别注意。

相关文章:

  • 2022-01-10
  • 2022-12-23
  • 2021-12-31
  • 2021-11-14
  • 2021-11-15
  • 2022-02-08
  • 2021-12-05
  • 2021-12-26
猜你喜欢
  • 2021-12-26
  • 2021-12-24
  • 2021-06-11
  • 2021-08-18
  • 2021-12-26
  • 2021-12-26
  • 2022-12-23
相关资源
相似解决方案