[CSHARP-4690] InvalidCastException on converting underlying type to enum Created: 26/Jun/23  Updated: 28/Oct/23  Resolved: 25/Jul/23

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 2.19.0, 2.19.1, 2.19.2, 2.20.0
Fix Version/s: 2.21.0

Type: Bug Priority: Blocker - P1
Reporter: Ganeshraj K Assignee: Oleksandr Poliakov
Resolution: Fixed Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible
Documentation Changes: Not Needed
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

Summary

Unable to cast object of type 'MongoDB.Bson.Serialization.Serializers.Int32Serializer' to type 'MongoDB.Driver.Linq.Linq3Implementation.Serializers.IEnumUnderlyingTypeSerializer

Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

Please refer the following code where I receive error while using an ENUM value (VehicleType)vehicle.Type with mongodb c# driver version 2.19 & above

How to Reproduce

cehcileInfo = await (from vehicle in _mongodb.Vehicles.AsQueryable()

                                                                                                                                                where vehicle.Id == vehicleId

                                                                                                                                                select new VehicleMetaData()

                                                                                                                                               

{                                                                                                                                                                 Id = vehicle.Id,                                                                                                                                                                 CustomerId = vehicle.CustomerId,                                                                                                                                                                 Name = vehicle.Name,                                                                                                                                                                 Type = (VehicleType)vehicle.Type,                                                                                                                                                 }

).FirstOrDefaultAsync();

Additional Background

Please provide any additional background information that may be helpful in diagnosing the bug.



 Comments   
Comment by Githook User [ 19/Jul/23 ]

Author:

{'name': 'Oleksandr Poliakov', 'email': '31327136+sanych-sun@users.noreply.github.com', 'username': 'sanych-sun'}

Message: CSHARP-4690: InvalidCastException on converting underlying type to enum (#1121)
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/4f9b7ade09ca1a77e262e27e88ffe9506ce989a0

Comment by PM Bot [ 26/Jun/23 ]

Hi ganeshmukeshraj1999@gmail.com, thank you for reporting this issue! The team will look into it and get back to you soon.

Generated at Wed Feb 07 21:49:03 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.