AutoMapper InternalDbSet`1 -> IQueryable`1 异常
AutoMapper InternalDbSet`1 -> IQueryable`1 Exception
我收到以下异常:
Mapping types:
InternalDbSet'1 -> IQueryable'1
Error mapping types.
我的 AssertConfigurationIsValid() returns 有效,但是一旦我 运行 我的传输层中的以下函数,就会抛出上述异常。
public IQueryable<CategoryDTO> Categories
{
get
{
return _mapper.Map<IQueryable<CategoryDTO>>(_context.Categories);
}
}
这是从上述函数调用的数据访问层函数,其中context.Categories类型为DbSet:
public IQueryable<Category> Categories => context.Categories.AsQueryable();
类别DTO:
[AutoMap(typeof(Category), ReverseMap = true)]
public class CategoryDTO
{
public CategoryDTO()
{
}
public int Id { get; set; }
[Required]
public string Description { get; set; }
public int ParentCategoryId { get; set; }
public string ImageFileName { get; set; }
}
类别实体:
public class Category
{
private ILazyLoader LazyLoader { get; set; }
private ICollection<Category> _subcategories;
private ICollection<Product> _products;
private Category _parentCategory;
public Category()
{
}
public Category(ILazyLoader lazyLoader)
{
LazyLoader = lazyLoader;
}
[Key]
public int Id { get; set; }
[Required]
public string Description { get; set; }
[ForeignKey("ParentCategory")]
public int? ParentCategoryId { get; set; }
public Category ParentCategory
{
get => LazyLoader.Load(this, ref _parentCategory);
set => _parentCategory = value;
}
public ICollection<Category> Subcategories
{
get => LazyLoader.Load(this, ref _subcategories);
set => _subcategories = value;
}
public ICollection<Product> Products
{
get => LazyLoader.Load(this, ref _products);
set => _products = value;
}
public string ImageFileName { get; set; }
}
知道这个问题出在哪里吗?我的断言函数没有错误让我迷路了。
正如 Lucian 评论的那样,可查询扩展解决了我的问题。
修改后的代码如下:
public IQueryable<CategoryDTO> Categories
{
get
{
//return _mapper.Map<IQueryable<CategoryDTO>>(_context.Categories);
return _context.Categories.ProjectTo<CategoryDTO>(_mapper.ConfigurationProvider).AsQueryable();
}
}
我收到以下异常:
Mapping types: InternalDbSet'1 -> IQueryable'1 Error mapping types.
我的 AssertConfigurationIsValid() returns 有效,但是一旦我 运行 我的传输层中的以下函数,就会抛出上述异常。
public IQueryable<CategoryDTO> Categories
{
get
{
return _mapper.Map<IQueryable<CategoryDTO>>(_context.Categories);
}
}
这是从上述函数调用的数据访问层函数,其中context.Categories类型为DbSet
public IQueryable<Category> Categories => context.Categories.AsQueryable();
类别DTO:
[AutoMap(typeof(Category), ReverseMap = true)]
public class CategoryDTO
{
public CategoryDTO()
{
}
public int Id { get; set; }
[Required]
public string Description { get; set; }
public int ParentCategoryId { get; set; }
public string ImageFileName { get; set; }
}
类别实体:
public class Category
{
private ILazyLoader LazyLoader { get; set; }
private ICollection<Category> _subcategories;
private ICollection<Product> _products;
private Category _parentCategory;
public Category()
{
}
public Category(ILazyLoader lazyLoader)
{
LazyLoader = lazyLoader;
}
[Key]
public int Id { get; set; }
[Required]
public string Description { get; set; }
[ForeignKey("ParentCategory")]
public int? ParentCategoryId { get; set; }
public Category ParentCategory
{
get => LazyLoader.Load(this, ref _parentCategory);
set => _parentCategory = value;
}
public ICollection<Category> Subcategories
{
get => LazyLoader.Load(this, ref _subcategories);
set => _subcategories = value;
}
public ICollection<Product> Products
{
get => LazyLoader.Load(this, ref _products);
set => _products = value;
}
public string ImageFileName { get; set; }
}
知道这个问题出在哪里吗?我的断言函数没有错误让我迷路了。
正如 Lucian 评论的那样,可查询扩展解决了我的问题。
修改后的代码如下:
public IQueryable<CategoryDTO> Categories
{
get
{
//return _mapper.Map<IQueryable<CategoryDTO>>(_context.Categories);
return _context.Categories.ProjectTo<CategoryDTO>(_mapper.ConfigurationProvider).AsQueryable();
}
}