我想在我的MVC 5 MarketPlace项目中使用EF 6。我的数据库中有许多相关的行。当我使用上下文模型时,一切都很好,但我无法使用视图模型来访问相关数据。我是新手,所以我阅读了有关相关数据的懒惰、急切和显式加载的信息。当代码调用带有 ID 的控制器时,目标行和关联也会响应,但我想列出我所有的类别行以及它们在 Home 索引中的相关行,所以如果可能的话,我需要一些提示。
型
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);
}
请我列出我的所有类别及其相关数据(图片,列表,统计数据(。非常感谢您的任何提示。
我想要列出我的所有类别及其相关数据(图片,列表,统计信息(
然后对查询运行 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();
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);
}