[CSHARP-3614] LINQ translation sometimes uses wrong element name in projection Created: 23/Apr/21 Updated: 28/Oct/23 Resolved: 29/Jun/22 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | Linq, LINQ3 |
| Affects Version/s: | 2.10.4 |
| Fix Version/s: | 2.17.0 |
| Type: | Bug | Priority: | Unknown |
| Reporter: | Анатолий Крыжановский | Assignee: | Robert Stam |
| Resolution: | Fixed | Votes: | 1 |
| Labels: | triaged | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Environment: |
Platform: .net core 3.1 |
||
| Issue Links: |
|
||||||||||||
| Epic Link: | CSHARP-3615 | ||||||||||||
| Description |
|
i have db model that contains all field of DTO for object. For example, if we have entity Foo and it can be shown in table FooTableItemDto
as card FooViewDto:
in editor FooEditorDto
then we have all fields from this DTOs in db:
now i have projector factory that build corresponding Expression. For example, if i want to get FooViewDto then factory generate me following expression:
and this works fine! but now, i have case then Material can be null (user can create draft of document without filling this field), but my code does not failed with NullReferenceException but create empty object (with null or default values of all fields)
but i need that Material also be null in this case, so i add IIF expression - check if Material is null in db and return null in such case, otherwise create projection:
but in this case i got exception during projection: Element 'Id' does not match any field or property of class FooMaterialDto
the exception was gone if i add explicit mapping for FooMaterialDto. But i don't want to do that, because i suggest that must have mapping only for my db entities, not for dto. and i don't have such things in case if i don't have IIF operation in my expression so my question is - what i do wrong or missed? |
| Comments |
| Comment by Robert Stam [ 29/Jun/22 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
This issue has been fixed in the new LINQ provider (known as LINQ3), which is included in the 2.14 release. Configure your MongoClientSettings to use LinqProvider.V3 if you want to use this functionality. To configure a client to use the LINQ3 provider use code like the following
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Githook User [ 29/Jun/22 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Author: {'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}Message: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Robert Stam [ 17/May/21 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
This looks like a bug in the LINQ translation of the query. The current implementation sends the following pipeline to the server:
The pipeline should be:
We are currently reimplementing our LINQ provider and we will ensure that this use case works in the new LINQ provider. I can offer two workarounds you could use in the meantime: 1. Annotate your BookDto and AuthorDto with the [BsonNoId] attribute (this disables the mapping of "Id" to "_id" as the element name) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Анатолий Крыжановский [ 14/May/21 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
any updates? | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Анатолий Крыжановский [ 28/Apr/21 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
so, i created sample which illustrate problem. we have two entity, Book and they Author (Author info is incapsulated in Book), also we have dto's for this entities. Then we try to select all books with author info. There are two query - simple, which project one-to-one. This query works but return empty Author info instead of null Second query contains IIF operation and thro expection
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Анатолий Крыжановский [ 27/Apr/21 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Ok, i try to create sample tommorow | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Mikalai Mazurenka (Inactive) [ 23/Apr/21 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Hello anatoly.kryzhanovsky@singularis-lab.com, Thank you for reporting this issue. This case looks like Note, that we are currently reimplementing our LINQ translation layer, and considering a fix for the linked issue. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Анатолий Крыжановский [ 23/Apr/21 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
and more info i found that there is different behavior for IntiMember Exrepssion and other type if we have simple InitMember then we create BsonMap without id member and all works fine in my case i have IIF expression and AutoClass was invoked, during autoclasss NamedIdMemberConvention executed
so as workaround i remove _defaults_ convention pack and add it with type filter (apply only for my db models) also i create new conventionpack which does not include NamedIdMemberConvention and apply them with type filter (apply for all other types) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Анатолий Крыжановский [ 23/Apr/21 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
additional info: i go deeper while debugging and found that there are difference between BsonTrie if i have IIF operation and without it for IIF expression case i have property "_id" (and i don't have such property in my DTO, and otherwise - in my Dto i have Id but there is no such property in BsonTrie) for non-IIF expression i have "Id" property |