c# - Nopcommerce 4.2 - 如何获取 _ProductBox 部分视图的类别
问题描述
我将此代码添加到 ProductModelFactory.cs 中 ProductCategories = product.ProductCategories.ToList()
public virtual IEnumerable<ProductOverviewModel> PrepareProductOverviewModels(IEnumerable<Product> products,
bool preparePriceModel = true, bool preparePictureModel = true,
int? productThumbPictureSize = null, bool prepareSpecificationAttributes = false,
bool forceRedirectionAfterAddingToCart = false)
{
if (products == null)
throw new ArgumentNullException(nameof(products));
var models = new List<ProductOverviewModel>();
foreach (var product in products)
{
var model = new ProductOverviewModel
{
Id = product.Id,
Name = _localizationService.GetLocalized(product, x => x.Name),
ShortDescription = _localizationService.GetLocalized(product, x => x.ShortDescription),
FullDescription = _localizationService.GetLocalized(product, x => x.FullDescription),
SeName = _urlRecordService.GetSeName(product),
Sku = product.Sku,
ProductType = product.ProductType,
MarkAsNew = product.MarkAsNew &&
(!product.MarkAsNewStartDateTimeUtc.HasValue || product.MarkAsNewStartDateTimeUtc.Value < DateTime.UtcNow) &&
(!product.MarkAsNewEndDateTimeUtc.HasValue || product.MarkAsNewEndDateTimeUtc.Value > DateTime.UtcNow),
ProductCategories = product.ProductCategories.ToList()
};
现在刷新第二次页面时出现错误:
$exception {“为警告'Microsoft.EntityFrameworkCore.Infrastructure.LazyLoadOnDisposedContextWarning生成错误:在关联的DbContext被释放后,尝试延迟加载实体类型'ProductCategoryProxy'上的导航属性'Category'。'。可以抑制此异常或通过将事件 ID 'CoreEventId.LazyLoadOnDisposedContextWarning' 传递给 'DbContext.OnConfiguring' 或 'AddDbContext' 中的 'ConfigureWarnings' 方法来记录。"} System.InvalidOperationException
_ProductBox.cshtml
<div class="category">
@foreach (var item in Model.ProductCategories)
{
@item.Category.Name
}
</div>
解决方案
由于上下文被释放,产品类别不能被延迟加载。ProductCategories
您可以通过在加载Product
列表时包含 来解决此问题。大概是在ProductService
你调用PrepareProductOverviewModels
方法之前的某个地方。为此,请将 添加Include(navigationPropertyPath)
到表中。这会强制直接加载属性。
示例ProductService
:
productRepository.Table.Include("ProductCategories")
或者,如果您不想在 中编辑默认方法,则使用自定义服务调用ProductService
替换product.ProductCategories.ToList()
ProductCategories = _productService.GetProductCategoriesForProduct(product);
在您的ProductService
中,您应该像这样创建此方法:
public virtual ProductCategories GetProductCategoriesForProduct(Product product)
{
var resultProduct = _productRepository.Table.Include("ProductCategories")
.FirstOrDefault(x => x.Id == product.Id);
if(resultProduct != null)
return resultProduct.ProductCategories.ToList();
else
return null; // Product not found, return null or empty list.
}
推荐阅读
- ruby-on-rails - 有没有办法在运行 bundle install 之前获取 gem 的版本(来自 Gemfile.lock)
- python - 如何在 Python Pandas 中用另一个字符串替换数据框列中的字符串?
- java - 就内存、处理器、时间而言,Java 中 BufferInputReader、LineNumberReader 和 Stream 之间的最佳文件阅读器是什么
- reactjs - 如何使用 reactjs 处理视图访问?
- ruby-on-rails - 了解 Model.find 返回的对象
- r - 在 group_by 之后在 id 之后的两列之间执行 bartlett.test
- c - 如何在 C 中获取无符号 8 位类型的最高有效位
- python - 此代码段中是否正确使用了 memoryview 对象?
- algorithm - 最大化2个玩家所选择的数字之和的差异
- java - 从 Java 资源文件中读取阿拉伯语数据