[CSHARP-3235] FormatException when using AutoMapper.ProjectTo Created: 23/Oct/20 Updated: 06/May/21 Resolved: 28/Oct/20 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | Linq |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | Mohamad Javad Ebrahimi | Assignee: | James Kovacs |
| Resolution: | Duplicate | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||||||
| Description |
|
|
| Comments |
| Comment by James Kovacs [ 28/Oct/20 ] | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Thank you for the additional information. Digging in further, the problem is specifically with CategoryDto. If you ignore the Category to CategoryDto mapping (via .ForMember(x => x.Category, opt => opt.Ignore())), your code works correctly. Turning our attention to the equivalent Select that you provided offers more insight into the problem. The Category portion of the provided LINQ query translates into:
If we remove the ternary operator from the LINQ query, the following also runs successfully:
Our current LINQ provider does not correctly or fully support LINQ's IIF conditional as noted in Thank you again for reporting this bug. Please monitor | ||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Mohamad Javad Ebrahimi [ 28/Oct/20 ] | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Thanks for replying. No, it is not with AutoMapper. The equivalent query that AutoMapper makes is:
And equivalent to the previous query using json string is:
But there is the same error with this.
Maybe this will help that a workaround for this is annotating CategoryDto with [BsonNoId] attribute.
| ||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by James Kovacs [ 28/Oct/20 ] | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Thank you for reporting the problem and providing the linked repro. We understand that you are attempting to use AutoMapper to convert POCOs (retrieved from MongoDB) to and from DTOs. Let's focus on the query itself:
If we remove AutoMapper's ProjectTo<PostDto>, result contains two correctly deserialized Post objects as expected as your DatabaseFixture pre-populates the collection with those post documents. With AutoMapper's ProjectTo<PostDto>, we observe the exception noted in your description. This appears to be an issue with AutoMapper and not with the MongoDB .NET Driver's LINQ provider. We recommend contacting the maintainers of AutoMapper to investigate further. | ||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Mohamad Javad Ebrahimi [ 23/Oct/20 ] | ||||||||||||||||||||||||||||||||||||||||||||||||