使用ViewModels与Entity Framework 6读取多列表相关数据

时间:2018-01-21 15:13:34

标签: c# asp.net-mvc entity-framework

我想在我的MVC 5 MarketPlace项目中使用EF 6。我的数据库中有很多相关的行。当我使用Context Models Everything时,我无法使用ViewModels访问相关数据。我是新手,所以我读到了相关数据的懒惰,渴望和显式加载。当代码调用带有ID的控制器然后目标行和关联也响应,但我想列出我的所有类别行和他们在Home Index相关的行,所以我需要一些小技巧,如果可能的话。

模型

    public partial class Category : Repository.Pattern.Ef6.Entity
{
    public Category()
    {
        this.CategoryPictures = new List<CategoryPicture>();
        this.CategoryListingTypes = new List<CategoryListingType>();
        this.CategoryStats = new List<CategoryStat>();
        this.Listings = new List<Listing>();
        this.MetaCategories = new List<MetaCategory>();
    }

    public int ID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public int Parent { get; set; }
    public bool Enabled { get; set; }
    public int Ordering { get; set; }
    public virtual ICollection<CategoryListingType> CategoryListingTypes { get; set; }
    public virtual ICollection<CategoryStat> CategoryStats { get; set; }
    public virtual ICollection<Listing> Listings { get; set; }
    public virtual ICollection<CategoryPicture> CategoryPictures { get; set; }
    public virtual ICollection<MetaCategory> MetaCategories { get; set; }
}


视图模型

    public class CategoryItemModel
{
    public List<ListingItemModel> CategoryListings { get; set; }
    public List<Category> CategoryOthers { get; set; }
    public Category CategoryCurrent { get; set; }
    public string UrlPicture { get; set; }
    public List<CategoryPictureModel> Pictures { get; set; }
}


类别详情控制器

 public virtual async Task<ActionResult> Category(int id)
    {
        var itemQuery = await _categoryService.Query(x => x.ID == id)
            .Include(x => x.CategoryPictures)
            .Include(x => x.CategoryListingTypes)
            .Include(x => x.Listings)
            .Include(x => x.CategoryStats)
            .SelectAsync();

        var item = itemQuery.FirstOrDefault();
        if (item == null)
            return new HttpNotFoundResult();

        var itemsList = _listingService.Queryable()
            .Where(x => x.CategoryID == id
                && (x.Enabled != false || x.Active != false)
                && (x.EndDate >= DateTime.Now || x.StartDate <= DateTime.Now))
                .ToList();

        var itemsModel = new List<ListingItemModel>();
        foreach (var list in itemsList.OrderByDescending(x => x.Created))
        {
            itemsModel.Add(new ListingItemModel()
            {
                ListingCurrent = list,
                UrlPicture = list.ListingPictures.Count == 0 ? ImageHelper.GetListingImagePath(0) : ImageHelper.GetListingImagePath(list.ListingPictures.OrderBy(x => x.Ordering).FirstOrDefault().PictureID)
            });
        }


        var pictures = await _categoryPictureservice.Query(x => x.CategoryID == id).SelectAsync();

        var picturesModel = pictures.Select(x =>
            new CategoryPictureModel()
            {
                ID = x.PictureID,
                Url = ImageHelper.GetCategoryImagePath(x.PictureID),
                CategoryID = x.CategoryID,
                Ordering = x.Ordering
            }).OrderBy(x => x.Ordering).ToList();

        var itemModel = new CategoryItemModel()
        {
            CategoryCurrent = item,
            CategoryListings = itemsModel,
            Pictures = picturesModel

        };

        return View("~/Views/Listing/Category.cshtml", itemModel);
    }

请列出我的所有类别及其相关数据(图片,列表,统计信息)。 非常感谢您的任何提示。

2 个答案:

答案 0 :(得分:0)

  

我希望列出所有类别及其相关数据(图片,列表,统计信息)

然后在您的查询上运行ToList:

    var  categories = await _categoryService.Query(x => x.ID == id)
        .Include(x => x.CategoryPictures)
        .Include(x => x.CategoryListingTypes)
        .Include(x => x.Listings)
        .Include(x => x.CategoryStats)
        .ToList();

答案 1 :(得分:0)

 public virtual async Task<ActionResult> Categories()
    {
        var items = await _categoryService.GetAll()
            .Include(x => x.CategoryPictures)
            .Include(x => x.CategoryListingTypes)
            .Include(x => x.Listings)
            .Include(x => x.CategoryStats)
            .ToListAsync();



        return View(items);
    }