如何在EntityDataSource中处理自联接

本文关键字:处理 EntityDataSource | 更新日期: 2024-07-23 05:40:13

我想知道如何在EntityDataSource中处理自联接。

如果我有这样的查询:

SELECT b.degree_name ,c.degree_name as degree_next
FROM EMPDEGPROM a INNER JOIN  DEGREEWORK b
ON a.degree_code=b.degree_code
INNER JOIN DEGREEWORK c
ON a.next_degree_code =c.degree_code
WHERE a.emp_num=6777

如何将此数据源用作EntityDataSource。?

我试着这样做,但我无法获得degree_next

<asp:EntityDataSource ID="EmpPromotionsDS" runat="server"
            ConnectionString="name=CTX" DefaultContainerName="CTX" EnableFlattening="False"
            EntitySetName="EMPDEGPROMs" EntityTypeFilter="EMPDEGPROM" Where="it.EMP_NUM =@emp_num"  Include="DEGREEWORK" >
            <WhereParameters>
                <asp:SessionParameter Name="emp_num" SessionField="emp_num" DbType="Int32" />
            </WhereParameters>

如何在EntityDataSource中处理自联接

创建一个视图。

CREATE VIEW XXX AS
SELECT a.emp_num, b.degree_name ,c.degree_name as degree_next
FROM EMPDEGPROM a INNER JOIN  DEGREEWORK b
ON a.degree_code=b.degree_code
INNER JOIN DEGREEWORK c
ON a.next_degree_code =c.degree_code

EF阅读观点没有问题。

  • 如果只希望只读,则可以删除EntitySetName、EntityTypeFilter、Where和Include属性,而使用CommandText属性。

  • 如果要直接使用实体,请确保您有一个带有自引用的导航属性,并在include属性中对其进行精确定位。

<asp:EntityDataSource ID="EmpPromotionsDS" runat="server"
            ConnectionString="name=CTX" 
            DefaultContainerName="CTX" 
            EnableFlattening="False"
            CommandText= SELECT b.degree_name ,c.degree_name as degree_next
                         FROM EMPDEGPROM a INNER JOIN  DEGREEWORK b
                         ON a.degree_code=b.degree_code
                         INNER JOIN DEGREEWORK c
                         ON a.next_degree_code =c.degree_code
                         WHERE a.emp_num=@EmployeeId>
<CommandParameters>
  <asp:ControlParameter Name="EmployeeId" ControlID="EmployeeIdTextBox" Type="Int32"/>
</CommandParameters>