IQueryable Extensions Exception
See original GitHub issueI’ve been trying to implement a project with automapper for the first time, and I keep running into the same issue when writing queries and using the IQueriable extensions.
This is a simplified Dto model.
public class WidgetDto
{
public int Id { get; set; }
public string WidgetName { get; set; }
public string Description { get; set; }
public List<WidgetCategoryDto> WidgetCategories { get; set; }
}
public class CategoryDto
{
public int Id { get; set; }
public string CategoryName { get; set; }
public List<WidgetCategoryDto> WidgetCategories { get; set; }
}
public class WidgetCategoryDto
{
public int Id { get; set; }
public int WidgetId { get; set; }
public int CategoryId { get; set; }
public CategoryDto Category { get; set; }
public WidgetDto Widget { get; set; }
}
With a very simple mapping configuration.
Mapper.CreateMap<Widget, WidgetDto>.MaxDepth(3);
Mapper.CreateMap<WidgetCategory, WidgetCategoryDto>.MaxDepth(3);
Mapper.CreateMap<Category, CategoryDto>.MaxDepth(3);
If I write a very simple query then the exception occurs.
var db = new DbContext();
var query = db.Widgets
.Project()
.To<WidgetDto>()
.ToList();
Additional information: The type ‘Widget.WidgetCategoryDto’ appears in two structurally incompatible initializations within a single LINQ to Entities query. A type can be initialized in two places in the same query, but only if the same properties are set in both places and those properties are set in the same order.
From what I’ve read, the expected behaviour is to eager load related mapped types to recursive depth of 3.
However, I can’t get the query to work without it throwing the above exception.
I would actually like to optionally expand sub types using a mapping configuration like so…
Mapper.CreateMap<Widget, WidgetDto>()
.MaxDepth(3)
.ForMember(dto => dto.WidgetCategories, opt => opt.ExplicitExpansion());
Mapper.CreateMap<WidgetCategory, WidgetCategoryDto>()
.MaxDepth(3)
.ForMember(dto => dto.Category, opt => opt.ExplicitExpansion())
.ForMember(dto => dto.Widget, opt => opt.ExplicitExpansion());
Mapper.CreateMap<Category, CategoryDto>()
.MaxDepth(3)
.ForMember(dto => dto.WidgetCategories, opt => opt.ExplicitExpansion());
However, the following query also throws the same exception
var db = new DbContext();
var query = db.Widgets
.Project()
.To<WidgetDto>(null,
dto => dto.WidgetCategories,
dto => dto.WidgetCategories.Select(x => x.Category))
.ToList();
Am I missing something?
Issue Analytics
- State:
- Created 8 years ago
- Comments:11 (5 by maintainers)
Top GitHub Comments
If you make your Dto objects like so
It should work. The problem is commented out properties will cause an infinite loop when trying to Project. Project makes a select statement, and DON’T use Include(), and you would have an infinitely long select statement.
IF you need those properties you need to make them separate Dto class for one level down and so on and so forth until you want to stop recursively going down the list, where you wouldn’t have the child property.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.