【发布时间】:2020-08-11 13:31:54
【问题描述】:
我有一个dotnet core web api,它使用Repository Pattern 和AutoMapper 将资源映射到模型......,
这是InMemoryDatabase 实现
如下所示,它是一个ProducRepository,它使用EntityFrameworkInclude()方法来包含类别数据
public async Task<IEnumerable<Products>>ListAsync(){
return await _context.Products.Include(p=>p.Categories).ToListAsync();
}
我为HttpGet实现了分类服务,并准备了在ProductController获取数据的方法,这里是get方法
[HttpGet]
public async Task<IEnumerable<ProductResource>>ListAsync(){
var products=await _ProductService.ListAsync();
var resource = _mapper.Map<IEnumerable<Products>,IEnumerable<ProductResource>>(products);
return resource;
}
在ProductResource我就这样通过了CategoryResource
public class ProductResource
{
public int Id { get; set; }
public string Name { get; set; }
public int QuantityInPackage { get; set; }
public string UnitOfMeasurement { get; set; }
public CategoryResource categoryResource { get; set; }
}
这意味着当我运行产品时Get RequestCategoryResource必须在ProductResourse旁边执行,但它返回null就像这样
【问题讨论】: