Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架-右键连接到视图_C#_Sql_Linq_Entity Framework - Fatal编程技术网

C# 实体框架-右键连接到视图

C# 实体框架-右键连接到视图,c#,sql,linq,entity-framework,C#,Sql,Linq,Entity Framework,我的数据库上下文中有2个实体: 员工 雇员假期权利 我认为这是一种相当正常的一对一关系——但员工可以在没有员工假期权利的情况下生存,但员工假期权利不能在没有员工的情况下生存 员工被映射到我的数据库中的视图 员工假期权利是一个表格 我的课程是: 员工假期权利 [Table("tblEmployeeHolidayEntitlement")] public class EmployeeHolidayEntitlement { [Key] public int EmployeeNumbe

我的数据库上下文中有2个实体:

员工
雇员假期权利

我认为这是一种相当正常的一对一关系——但员工可以在没有员工假期权利的情况下生存,但员工假期权利不能在没有员工的情况下生存

员工被映射到我的数据库中的视图 员工假期权利是一个表格

我的课程是:

员工假期权利

[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; }
}
员工

[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; }
}
在构建上下文时,我会:
(不确定这是否正确!)

但这(我认为)是在做一个左连接- 它只返回在TblemployeeHolidayAuthentication中有条目的两个实体

我可以想象,生成的SQL需要如下所示:

SELECT 
employee.EmployeeNumber, 
employeeHol.* 

FROM tblEmployeeHolidayEntitlement employeeHol

RIGHT JOIN vEmployee employee
ON 
employeeHol.EmployeeNumber = employee.EmployeeNumber
from user in db.....
select new{user,user.Entitlement};

这可能吗?

它按照您的要求进行内部联接

您根本不需要显式联接

你需要像这样的东西:

SELECT 
employee.EmployeeNumber, 
employeeHol.* 

FROM tblEmployeeHolidayEntitlement employeeHol

RIGHT JOIN vEmployee employee
ON 
employeeHol.EmployeeNumber = employee.EmployeeNumber
from user in db.....
select new{user,user.Entitlement};

它按照你的要求进行内部连接

您根本不需要显式联接

你需要像这样的东西:

SELECT 
employee.EmployeeNumber, 
employeeHol.* 

FROM tblEmployeeHolidayEntitlement employeeHol

RIGHT JOIN vEmployee employee
ON 
employeeHol.EmployeeNumber = employee.EmployeeNumber
from user in db.....
select new{user,user.Entitlement};

您的代码正在进行内部联接。我相信您要求检索所有员工,以及每个员工的
EmployeeHolidayAuthentication
(如果存在)

要执行左连接,请使用类似以下内容的查询:

from adUser in db.ADUsers
join userEntitlement in db.ADUserHolidayEntitlement on
    adUser.EmployeeNumber equals userEntitlment.EmployeeNumber into g
from userEntitlement in g.DefaultIfEmpty()
select new 
{
    adUser, 
    userEntitlement // Will be null of no entitlement exists
}

您的代码正在进行内部联接。我相信您要求检索所有员工,以及每个员工的
EmployeeHolidayAuthentication
(如果存在)

要执行左连接,请使用类似以下内容的查询:

from adUser in db.ADUsers
join userEntitlement in db.ADUserHolidayEntitlement on
    adUser.EmployeeNumber equals userEntitlment.EmployeeNumber into g
from userEntitlement in g.DefaultIfEmpty()
select new 
{
    adUser, 
    userEntitlement // Will be null of no entitlement exists
}