You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

添加商品到购物车时触发Product Not Found异常的排查求助

商品添加购物车触发未处理异常排查求助

异常信息

An unhandled exception occurred while processing the request.
Exception: Product not found!
Services.Abstracts.ProductService.GetOneProduct(int id, bool trackChanges) in ProductService.cs, line 48

我跟着教程开发应用时,在添加商品到购物车环节触发了上述未处理异常。已核对Service层、仓储层代码,也和教程GitHub项目做了对比,但仍找不到问题根源,求可行的排查方案。


相关代码

IProductService 接口

public interface IProductService
{
    IEnumerable<Product> GetAllProducts(bool trackChanges);
    Product? GetOneProduct(int id,bool trackChanges);
    void CreateProduct(ProductDtoForInsertion productDto);
    void UpdateOneProduct(ProductDtoForUpdate productDto);
    void DeleteOneProduct(int id);
    ProductDtoForUpdate GetOneProductForUpdate(int id, bool trackChanges);
}

ProductService 实现

public Product? GetOneProduct(int id, bool trackChanges)
{
    var product = _manager.Product.GetOneProduct(id, trackChanges);
    if (product is null)
        throw new Exception("Product not found!");
    return product;
}

IProductRepository 接口

public interface IProductRepository :IRepositoryBase<Product>
{
    IQueryable<Product> GetAllProducts(bool trackChanges);
    Product? GetOneProduct(int id ,bool  trackChanges);
    void CreateOneProduct(Product product);
    void DeleteOneProduct(Product product);
    void UpdateOneProduct(Product entity);
}

ProductRepository 实现

public Product? GetOneProduct(int id, bool trackChanges)
{
    return FindByCondition(p=>p.Id.Equals(id),trackChanges);
}

Cart.cshtml.cs 页面模型

public IActionResult OnPost(int productId,string returnUrl)
{
    Product ? product =_manager
    .ProductService
    .GetOneProduct(productId,false);

    if (product is not null)
    {
        Cart.AddItem(product,1);
    }
     return Page();
}

仓储基类代码

public interface IRepositoryBase<T>
{
    IQueryable<T> FindAll(bool trackChanges);
    T? FindByCondition(Expression<Func<T, bool>> expression, bool trackChanges);
    void Create(T entity);
    void Remove(T entity);
    void Update(T entity);
}

public T? FindByCondition(Expression<Func<T, bool>> expression, bool trackChanges)
{
    return trackChanges
        ? _context.Set<T>().Where(expression).SingleOrDefault()
        :_context.Set<T>().Where(expression).AsNoTracking().SingleOrDefault();
}

排查方案

  • 验证请求参数正确性:在Cart.cshtml.cs的OnPost方法开头加断点或日志,查看实际传入的productId值,确认是否传了无效值(如0、负数、数据库中不存在的ID)。
  • 检查数据库数据:直接查询数据库Product表,确认是否存在与传入productId匹配的记录。注意核对ID的数据类型是否与代码一致(比如数据库用bigint但代码用int,会导致查询不到)。
  • 测试仓储层查询逻辑:手动调用ProductRepository.GetOneProduct方法,传入已知存在的ID,看是否能返回正确结果。也可尝试将FindByCondition中的SingleOrDefault改为FirstOrDefault(虽主键查询不会有多个结果,但可能因EF Core查询解析问题产生差异)。
  • 核对依赖注入配置:确认ProductRepository是否正确注入到ProductService,_manager.Product是否指向正确的仓储实例,避免因实例不匹配导致查询失败。
  • 查看EF Core生成的SQL:开启EF Core日志功能,查看GetOneProduct方法实际执行的SQL语句,将该SQL直接在数据库中执行,看是否能返回结果。
  • 检查软删除逻辑:若教程涉及软删除(如Product表有IsDeleted字段),确认仓储层查询是否过滤了已删除记录,避免因软删除标记导致查询不到。
  • 验证DbContext生命周期:检查DbContext的注入生命周期是否正确(如是否用了单例模式导致数据未更新),确保每次查询都能获取最新数据库数据。

内容的提问来源于stack exchange,提问作者Mehmet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 15:20:05