2

I have the following object in my models:

[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }

[BsonElement("Name")]
[BsonRepresentation(BsonType.String)]
public string Name { get; set; }

[BsonElement("Price")]
[BsonRepresentation(BsonType.Double)]
public double Price { get; set; }

[BsonElement("Type")]
[BsonRepresentation(BsonType.Array)]
public string[] Type { get; set; }

when I try to insert a new object to the database using =>

MongoCollection.InsertOne(object);

the following error occurs

System.TypeInitializationException: The type initializer for 'OrdisTT.Libs.MongoLib' threw an exception.
 ---> System.ArgumentException: GenericArguments[0], 'System.Array', on 'MongoDB.Bson.Serialization.Serializers.EnumerableInterfaceImplementerSerializer`1[TValue]' violates the constraint of type 'TValue'.
 ---> System.TypeLoadException: GenericArguments[0], 'System.Array', on 'MongoDB.Bson.Serialization.Serializers.EnumerableInterfaceImplementerSerializer`1[TValue]' violates the constraint of type parameter 'TValue'.
   at System.RuntimeTypeHandle.Instantiate(Type[] inst)
   at System.RuntimeType.MakeGenericType(Type[] instantiation)
   --- End of inner exception stack trace ---

How can I map an array property to a mongodb array?

2
  • 1
    Can you check if the .NET side type is List<string> if it's working? Commented Nov 12, 2019 at 9:27
  • 2
    github.com/mongodb/mongo-csharp-driver/blob/… looks like arrays are represented as strings in the tests ([BsonRepresentation(BsonType.String)]) Commented Nov 12, 2019 at 9:53

1 Answer 1

3

If I just make the field or property like this:

public List<string> SomeList = new List<string>();

it will map, no need for representation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.