有一个奇怪的问题。(也许根本不奇怪)
我有三个对象,Employee(员工),Rota(排班表)和Department(部门)。
public class Employee
{
public int Id { get; set; }
public String Name { get; set; }
public virtual Department Department { get; set; }
}
internal class EmployeeMapping : EntityTypeConfiguration<Employee>
{
public EmployeeMapping()
{
HasKey(a => a.Id);
Property(a => a.Id).HasColumnName("UserId");
HasRequired<Department>(a => a.Department).WithOptional().Map(a => a.MapKey("DepartmentId"));
}
}
public class Department
{
public int Id { get; set; }
public String Name { get; set; }
}
internal class DepartmentMapping : EntityTypeConfiguration<Department>
{
public DepartmentMapping()
{
HasKey(a => a.Id);
Property(a => a.Id).HasColumnName("DepartmentId");
}
}
public class Rota
{
public int Id { get; set; }
public virtual Employee Employee { get; set; }
public virtual Department Department { get; set; }
}
internal class RotaMapping : EntityTypeConfiguration<Rota>
{
public RotaMapping()
{
HasKey(a => a.Id);
Property(a => a.Id).HasColumnName("RotaId");
HasOptional<Employee>(a => a.Employee).WithOptionalDependent().Map(a => a.MapKey("EmployeeId"));
HasOptional<Department>(a => a.Department).WithOptionalDependent().Map(a => a.MapKey("DepartmentId"));
}
}
并不复杂。Rota可以分配给员工和/或部门,这些都是使用Fluent配置的。所有关联都正确(模式完美),但我有一个奇怪的异常。
当我执行myContext.Departments.FirstOrDefault()
并查看生成的SQL时,Employee和Rota之间有一个 LEFT OUTER JOIN 。为什么会出现这种情况?
我不希望它这样做。也许我的Fluent映射不正确?我尝试了所有可能,但似乎找不出原因。如果我想要Rota对象,则理解为需要连接到Department。但反过来则不应该!
如果我执行myContext.Departments.AsNoTracking().FirstOrDefault()
,它就不会做 LEFT OUTER JOIN's。
有任何想法吗?
谢谢, D