You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using Automapper with .ProjectTo<DTO>, with DTO that has ignored mapping properties (or unmapped), if those properties are at the top of the DTO class then DataSourceLoader.Load(..) breaks with System.InvalidOperationException: 'The LINQ expression [...] could not be translated.
We use views in the database and Automapper to map them to a DTO, and then DataSourceLoader.Load to load them from the DB with grouping/paging/sorting. In some cases it works fine, in others breaks.
System.InvalidOperationException: 'The LINQ expression 'DbSet<VBlog>
.Where(v => v.Url == "http://www.SomeBlog.com")
.OrderBy(v => v.Url)
.ThenBy(v => new VBlogDTO{
BlogId = v.BlogId,
Url = v.Url
}
.IdUrl)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.'
The only difference is that VBlogDTO1 has mapped properties first, and ignored properties last, while VBlogDTO2 has ignored properties first.
Problem also appears when using DataSourceLoader.LoadAsync()
I tried applying workarounds mentioned in #367 but those, far as I understand, were designed to work with tables with keys, while we work with views that don't have keys.
We have few cases when two views are mapped to one DTO, and both supply different set of data/properties , so working around the problem with changing property order is not possible, in some cases.
The error occurs because DataSourceLoader fails to correctly identify a property to use for stable SQL sorting. It falls back to the first sortable property that happens to be ignored.
Use the following configuration to resolve the error.
If your DTO has a property that can serve as a logical key:
Thank you for a quick reply and sorry for late response now.
I've tested the proposed fix and it does seem to work for the provided unit test.
Unfortunately the proper production code, which is a bit more complicated, still has the same issues, even after providing either/both PrimaryKey and DefaultSort.
I wasn't yet able to track that issue down, due to the task prioritization at work, and fact I was able to provide a workaround with duplicated models, which had properties set in order to not trigger the exception; but I hope to be back at it by end of this or next week, to hopefully provide a more proper fix.
When using Automapper with
.ProjectTo<DTO>
, with DTO that has ignored mapping properties (or unmapped), if those properties are at the top of the DTO class thenDataSourceLoader.Load(..)
breaks withSystem.InvalidOperationException: 'The LINQ expression [...] could not be translated.
We use views in the database and Automapper to map them to a DTO, and then DataSourceLoader.Load to load them from the DB with grouping/paging/sorting. In some cases it works fine, in others breaks.
XUnit test that shows the problem:
The call:
Results in a proper generated SQL:
The call:
Results in an exception when generating the SQL:
The only difference is that
VBlogDTO1
has mapped properties first, and ignored properties last, whileVBlogDTO2
has ignored properties first.Problem also appears when using
DataSourceLoader.LoadAsync()
I tried applying workarounds mentioned in #367 but those, far as I understand, were designed to work with tables with keys, while we work with views that don't have keys.
We have few cases when two views are mapped to one DTO, and both supply different set of data/properties , so working around the problem with changing property order is not possible, in some cases.
Tested using
Entity Framework Core 3.1
DevExtreme.AspNet.Core v20.1.7
DevExtreme.AspNet.Data v2.7.1
Automapper v10.0.0
The text was updated successfully, but these errors were encountered: