简体   繁体   English

使用EF6和LINQ深度加载数据

[英]Deep loading data with EF6 and LINQ

I am implementing an ASP.NET MVC 5 web app using ASP.NET Identity 2 and Troy Goode's PagedList . 我正在使用ASP.NET Identity 2和Troy Goode的PagedList实现ASP.NET MVC 5 Web应用程序。 I need to display UserRole data in the following format in a JqGrid: 我需要在JqGrid中以以下格式显示UserRole数据:


User Name | 用户名| Role Name 角色名称


This is my UserRole class: 这是我的UserRole类:

public class UserRole : Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole<int>
{
    [ForeignKey("UserId")]
    public virtual User User { get; set; }

    [ForeignKey("RoleId")]
    public virtual Role Role { get; set; }
}

This is my repository method 这是我的存储库方法

public virtual IQueryable<UserRole> GetAsQueryable(Expression<Func<UserRole, bool>> where)
    {
        return _dataContext.UserRoles.Where(where).AsQueryable();
    } 

This my Controller method (the GetAsPagedList method simply orders the input list, applies PagedList's .ToPagedList method to it and returns a subset of it): 这是我的Controller方法(GetAsPagedList方法只是对输入列表进行排序,将PagedList的.ToPagedList方法应用于它,并返回它的一个子集):

        public ActionResult GridData(MvcJqGrid.GridSettings gridSettings)
        {
        IQueryable<UserRole> items = _repository.GetAsQueryable();

        IPagedList<T> pagedOrderedItems = PagingHelper<UserRole>.GetAsPagedList(
            items,
            gridSettings.SortOrder, gridSettings.SortColumn,
            gridSettings.PageIndex, gridSettings.PageSize);

        var jsonData = new
        {
            total = pagedOrderedItems.TotalItemCount / gridSettings.PageSize + 1,
            page = gridSettings.PageIndex,
            records = pagedOrderedItems.TotalItemCount,
            rows = (
                from c in pagedOrderedItems
                select new
                {
                    id = c.UserId + '-' + c.RoleId,
                    cell = new[]
                    {
                        "Edit",
                        "Details",
                        // TODO: something like this:
                        // [UserName] = c.User.UserName
                        // [RoleName] = c.Role.Name
                        c.Role.Name
                    }
                }).ToArray()
        };

        return Json(jsonData, JsonRequestBehavior.AllowGet);
    } 

I can'.t figure out where and how I should deep load data from the tables linked through foreign keys to my table (IE Users.UserName and Roles.Name) - could you please help? 我不知道应该在哪里以及如何将通过外键链接到我的表(IE Users.UserName和Roles.Name)的表中的数据深度加载-您能帮忙吗?

_dataContext.UserRoles.Include("Role").Include("User").Where(where).AsQueryable();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM