c# - EntityDataSource 查询内连接

标签 c# entity-framework entitydatasource

我有一个包含 3 个表的数据库:

User{UserId,UserName}
Role{RoleId,RoleName}
User_Role{UserId,RoleId}

这个查询:

int userIdPassByUrl = 0;
MyDbContext ctx = new MyDbContext();
var query = (from role in ctx.Role
        join userRole in ctx.User_Role on role.RoleId equals userRole.RoleId
        where userRole.UserId == userIdPassByUrl
        select new { role.RoleId, role.RoleName }).Distinct();

我需要在带有 EntityDataSource 的 Gridview 中显示上述查询的结果,代码或在设计模式中设置它。

这是我的实体数据源:

<asp:EntityDataSource ID="EdsRolesByUser" runat="server" 
        ConnectionString="name=myDbEntities"
        DefaultContainerName="myDbEntities" EnableFlattening="False"
        EntitySetName="Roles" EntityTypeFilter="Role"
        Select="it.[RoleId], it.[RoleName]">
    </asp:EntityDataSource>

如有任何帮助,我们将不胜感激。

最佳答案

终于明白了。 必须修改 EntityDataSource 删除 EntitySetName 和 EntityTypeFilter 属性,并像这样添加 CommandText:

CommandText="SELECT DISTINCT userRole.RoleId, role.RoleName FROM Role AS role
INNER JOIN User_Role as userRole
ON role.RoleId = userRole.RoleId
WHERE userRole.UserId = @UserIdPassbyUrl"

这个链接帮助我: http://msdn.microsoft.com/en-us/library/aa697427(v=vs.80).aspx

关于c# - EntityDataSource 查询内连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10878713/

相关文章:

c# - 提供者未返回 ProviderManifest 实例

c# - 适用于 Mac 和 PC 的 .NET Core 2.0 连接字符串

c# - 在 Powershell 中使用名称参数构造函数创建对象

c# - 如何查找存储在整数变量中的整数值的字节数

c# - 从 Entity Framework 表中选择特定列

asp.net - 如何在 EntityDataSource 中使用 CASE 语句进行排序?

C# 显示隐藏窗口

c# - EF : Passing a table valued parameter to a user-defined function from C#

c# - 在 docker 容器中包含 Entity Framework 工具

asp.net - Entity Framework 5 + EntityDataSource + ASP.NET + .NET 4.0 找不到 CLR 类型