[CSHARP-4024] System.InvalidOperationException: This constructor can only be used when BsonDefaults.GuidRepresentationMode is V2. Created: 28/Jan/22  Updated: 28/Jan/22  Resolved: 28/Jan/22

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 2.12.0, 2.12.1, 2.12.2, 2.12.3, 2.12.4, 2.12.5, 2.13.0, 2.13.1, 2.13.2, 2.13.3, 2.14.0
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Tissa Siriwardana Assignee: James Kovacs
Resolution: Duplicate Votes: 0
Labels: Bug
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates CSHARP-3691 GuidRepresentationMode.V3 filter in p... Closed

 Description   

Summary

System.InvalidOperationException: This constructor can only be used when BsonDefaults.GuidRepresentationMode is V2.

System.InvalidOperationException
This constructor can only be used when BsonDefaults.GuidRepresentationMode is V2.
at MongoDB.Bson.BsonBinaryData..ctor(Guid guid)
at MongoDB.Bson.BsonTypeMapper.Convert(Object value, Conversion conversion)
at MongoDB.Bson.BsonTypeMapper.TryMapToBsonValue(Object value, BsonValue& bsonValue)
at MongoDB.Bson.BsonTypeMapper.MapToBsonValue(Object value)
at MongoDB.Bson.BsonValue.Create(Object value)
at MongoDB.Driver.Linq.Translators.AggregateLanguageTranslator.TranslateConstant(Expression node)
at MongoDB.Driver.Linq.Translators.AggregateLanguageTranslator.TranslateValue(Expression node)
at MongoDB.Driver.Linq.Translators.AggregateLanguageTranslator.TranslateOperation(BinaryExpression node, String op, Boolean canBeFlattened)
at MongoDB.Driver.Linq.Translators.AggregateLanguageTranslator.TranslateValue(Expression node)
at MongoDB.Driver.Linq.Translators.AggregateLanguageTranslator.TranslateWhere(WhereExpression node)
at MongoDB.Driver.Linq.Translators.AggregateLanguageTranslator.TranslateValue(Expression node)
at MongoDB.Driver.Linq.Translators.AggregateLanguageTranslator.TranslatePipeline(PipelineExpression node)
at MongoDB.Driver.Linq.Translators.AggregateLanguageTranslator.TranslateValue(Expression node)
at MongoDB.Driver.Linq.Translators.AggregateLanguageTranslator.TranslateMapping(ProjectionMapping mapping)
at MongoDB.Driver.Linq.Translators.AggregateLanguageTranslator.TranslateMemberInit(MemberInitExpression node)
at MongoDB.Driver.Linq.Translators.AggregateLanguageTranslator.TranslateValue(Expression node)
at MongoDB.Driver.Linq.Translators.AggregateLanguageTranslator.Translate(Expression node, ExpressionTranslationOptions translationOptions)
at MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslateProjectValue(Expression selector)
at MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslateSelect(SelectExpression node)
at MongoDB.Driver.Linq.Translators.QueryableTranslator.Translate(Expression node)
at MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslatePipeline(PipelineExpression node)
at MongoDB.Driver.Linq.Translators.QueryableTranslator.Translate(Expression node)
at MongoDB.Driver.Linq.Translators.QueryableTranslator.Translate(Expression node, IBsonSerializerRegistry serializerRegistry, ExpressionTranslationOptions translationOptions)
at MongoDB.Driver.Linq.MongoQueryProviderImpl`1.Translate(Expression expression)
at MongoDB.Driver.Linq.MongoQueryProviderImpl`1.Execute(Expression expression)
at MongoDB.Driver.Linq.MongoQueryableImpl`2.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

When executing the following LINQ query the above exception is thrown.

var queryable = departmentCollection.AsQueryable()
.Where(x => x.Id == department.Id)
.Select(x => new DepartmentDTO()

Unknown macro: { Employees = x.Employees.Where(x => x.Id == employee.Id) }

).ToList();

Driver version: 2.12.0

MongoDB server version: 4.4.12
Topology: replica set

 



 Comments   
Comment by James Kovacs [ 28/Jan/22 ]

Thank you for confirming. Closing as a duplicate of CSHARP-3691.

Comment by Tissa Siriwardana [ 28/Jan/22 ]

Hi James,

Thanks for checking this. It works in v2.14.0 with the linq provider set to v3.

Cheers,

Viraj

Comment by James Kovacs [ 28/Jan/22 ]

Hi, viraj.s@kidsxap.com.au,

Thank you for reaching out to MongoDB with your issue. Initial indications are that this is a duplicate of CSHARP-3691, which is fixed in MongoDB .NET/C# Driver 2.14.0. Please try upgrading your driver to 2.14.0 and running your query with our new LINQ3 provider, which can be configured as follows:

var connectionString = "mongodb://localhost";
var clientSettings = MongoClientSettings.FromConnectionString(connectionString);
clientSettings.LinqProvider = LinqProvider.V3;
var client = new MongoClient(clientSettings);

If this does not resolve the issue, please provide a self-contained repro of the problem so that we can analyze the issue further.

Sincerely,
James

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