請幫忙。我無法解決問題 - 類別名稱為空。非常感謝。模型 public class Product { [Key] public int ProductID { get; 放; } 公共字串 ProductName { 獲取;放; } 公共字串描述 { 獲取;放; } 公共雙價格 { 得到; 放; }
public int CategoryID { get; set; }
[ForeignKey("CategoryID")]
public virtual Category Category { get; set; }
}
public class Category
{
[Key]
public int CategoryID { get; set; }
public string CategoryName { get; set; }
}
` 產品控制器
public async Task<IEnumerable<ProductDto>> GetProductsAsync()
{
List<Product> products = await _db.Products
.Include(u => u.Category)
.ToListAsync();
return _mapper.Map<List<ProductDto>>(products);
}
public class MappingProfiles : Profile
{
public MappingProfiles()
{
CreateMap<ProductDto, Product>().ReverseMap();
}
}
public class ProductDto
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public string Description { get; set; }
public double Price { get; set; }
public string CategoryName { get; set; }
}
如何解決空欄位?
uj5u.com熱心網友回復:
在下面更改您的MappingProfiles
喜歡:
public class MappingProfiles : Profile
{
public MappingProfiles()
{
CreateMap<Product, ProductDto>()
.ForMember(d => d.CategoryName, a => a.MapFrom(s => s.Category.CategoryName))
.ReverseMap()
.ForPath(b => b.Category, o => o.MapFrom(dto => (Category)null));
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/526548.html
標籤:实体框架asp.net 核心