Deep loading data - MVC / Entity Framework / Repository Pattern

前提是你 提交于 2019-12-25 03:22:40

问题


I am implementing an ASP.NET MVC 5 web app using ASP.NET Identity 2 and Troy Goode's PagedList. I need to display UserRole data in the following format in a JqGrid:


User Name | Role Name


This is my UserRole class:

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):

        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 (I.E. Users.UserName and Roles.Name) - could you please help?


回答1:


This is just another of the many reasons why using the repository pattern with Entity Framework is a silly idea. EF already implements the repository (DbSet) and unit of work (DbContext) patterns. All you're doing here is putting a wrapper around it that is less functional. Rant aside, the place to do this would be on your GetAsQueryable method. You can add an additional parameter to accepted a string of properties to include, and then use that to call Include in your method:

public virtual IQueryable<UserRole> GetAsQueryable(Expression<Func<UserRole, bool>> where, string includeProperties = null)
{
    IQueryable<UserRole> query = _dataContext.UserRoles.Where(where);

    if (includeProperties != null)
    {
        foreach (var includeProperty in includeProperties.Split
            (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
        {
            query = query.Include(includeProperty);
        }
    }
    return query.AsQueryable();
}


来源:https://stackoverflow.com/questions/24963070/deep-loading-data-mvc-entity-framework-repository-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!