如何通过Linq-to-Entity获取关系实体对象?

时间:2012-04-02 09:04:13

标签: c# asp.net .net linq linq-to-entities

 using (var dataContext = new realtydbEntities())
        {
            var user =
                (
               from aspnet_Roles rol in dataContext.aspnet_Roles.Include("aspnet_Users")
               from aspnet_Users usr in rol.aspnet_Users
               where rol.RoleId == roleID
               select usr
                );
            return user.ToList();

        }

我想用

  

usr.MemberShip.Email

MemberShip是外表的外表

aspnet_Roles-> aspnet_Users-> Membership.Email

但是我收到了一个错误: ObjectContext实例已被处理,不能再用于需要连接的操作。

如何将成员资格对象包含在结果??????

1 个答案:

答案 0 :(得分:0)

您很可能需要包含挂起aspnet_User表的aspnet_Membership表,如下所示:

using (var dataContext = new realtydbEntities())
{
      var user =
      (
          from aspnet_Roles rol in dataContext.aspnet_Roles.Include("aspnet_Users")
          from aspnet_Users usr in rol.aspnet_Users.Include("aspnet_Membership")
          where rol.RoleId == roleID
          select usr
      );
   return user.ToList();
 }