【发布时间】:2014-10-15 17:18:42
【问题描述】:
我的数据库上下文中有 2 个实体:
员工
EmployeeHolidayEntitlement
我认为这是一种相当正常的一对一关系 - 但 Employee 可以在没有 EmployeeHolidayEntitlement 的情况下存在,但 EmployeeHolidayEntitlement 不能在没有 Employee 的情况下存在
Employee 被映射到我数据库中的 视图
EmployeeHolidayEntitlement 是一个表格
我的课程是:
EmployeeHolidayEntitlement
[Table("tblEmployeeHolidayEntitlement")]
public class EmployeeHolidayEntitlement
{
[Key]
public int EmployeeNumber { get; set; }
public virtual Employee Employee { get; set; }
public decimal StandardEntitlement { get; set; }
//.....omitted for brevity
}
员工
[Table("vEmployee")] //note v - it's a view
public class Employee
{
[Key]
public int EmployeeNumber { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
在构建上下文时,我会:
(不确定这是否正确!)
modelBuilder.Entity<EmployeeHolidayEntitlement>()
.HasRequired(w => w.Employee)
.WithOptional();
查询时,如果可能的话,我希望每个员工都有一个 EmployeeEntitlement 记录(不管它是否存在于 tblEmployeeHolidayEntitlement 中) -
我的查询目前如下所示:
from
userEntitlement in db.ADUserHolidayEntitlement
join
adUser in db.ADUsers
on
userEntitlment.EmployeeNumber equals adUser.EmployeeNumber
select userEntitlement
但这是(我认为)进行左连接 - 它只返回在 tblEmployeeHolidayEntitlement 中有条目的 2 个实体
我想生成的 SQL 需要看起来像:
SELECT
employee.EmployeeNumber,
employeeHol.*
FROM tblEmployeeHolidayEntitlement employeeHol
RIGHT JOIN vEmployee employee
ON
employeeHol.EmployeeNumber = employee.EmployeeNumber
这可能吗?
【问题讨论】:
标签: c# sql linq entity-framework