user2004
user2004

Reputation: 1973

MongoDB attribute

I'm using MongoDB for an application. I have a Collection in my db named Role with the following fields: Id, Role and I cannot change these names. In my VS code, I have a class named Role:

  public class Role
{
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; set; }
    public string ApplicationName { get; set; }
    [BsonElement]
    public string Name { get; set; }
}

I have an errOR that says the element Role doesn't match the fields from db. I wonder if there exists an attribiute I can use to keep the "Name" property and not change it to "Role" because I cannot have a property named like my class.

Upvotes: 0

Views: 634

Answers (2)

junkangli
junkangli

Reputation: 1202

According to mongo-csharp-driver reference, you specify an element name using attributes.

Have you tried specifying element name like this?

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

    public string ApplicationName { get; set; }

    [BsonElement("Role")]
    public string Name { get; set; }
}

Upvotes: 1

Houssem Romdhani
Houssem Romdhani

Reputation: 332

Try this for "Name" Property:

[BsonElement("Role")]
public string Name { get; set; }

Upvotes: 1

Related Questions