Reputation: 44
I've a dotnet core web API that's using Repository Pattern and AutoMapper to mapping resources to models ..,
Here is the InMemoryDatabase
Implementation:
as shown below it's a ProducRepository
That use EntityFrameworkInclude()
method to include category Data
public async Task<IEnumerable<Products>>ListAsync(){
return await _context.Products.Include(p=>p.Categories).ToListAsync();
}
I implement category service for HttpGet
and prepare the method that get data in ProductController
here is the get method
[HttpGet]
public async Task<IEnumerable<ProductResource>>ListAsync(){
var products=await _ProductService.ListAsync();
var resource = _mapper.Map<IEnumerable<Products>,IEnumerable<ProductResource>>(products);
return resource;
}
in ProductResource
i passed CategoryResource
in this way
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; }
}
That means when I run the Product Get Request
the CategoryResource
must be executed beside ProductResourse
but it returns null
just like this:
Upvotes: 0
Views: 315
Reputation: 44
The main idea here that the product
and ProductResource
just they are mapping for a DescriptionAttribute
not for the include
// Mapping Description Attribut
CreateMap<Products,ProductResource>()
.ForMember(src=>src.UnitOfMeasurement,
opt=>opt.MapFrom(src=>src.UnitOfMeasurement.ToDescriptionString()));
// Mapping Categories Table
CreateMap<Products,ProductResource>()
.ForMember(dto=>dto.Categories,
conf=>conf.MapFrom(p=>p.Categories));
Upvotes: 1