Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble with mongoDB. The property '_id' of type ... cannot use element name '_id'

I use ASP .NET MVC4 WebAPI and i have some class

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

    [BsonElement]
    [Display(Name = "Title")]
    public string Title { get; set; }

    [BsonElement]
    [Display(Name = "Description")]
    public string Description { get; set; }

    [BsonElement]
    [Display(Name = "Cost")]
    public int Cost { get; set; }

    [BsonElement]
    [Display(Name = "CategoryId")]
    public string CategoryId { get; set; }

    [BsonElement]
    [Display(Name = "ProductList")]
    public List<string> ProductList { get; set; }

    [BsonConstructor]
    public Recipe(string title, string description, string type, int cost, List<string> productList)
    {
        Title = title;
        Description = description;
        CategoryId = type;
        Cost = cost;
        ProductList = productList;
    }
}

Than i take some documents in mongoDB

var collection = database.GetCollection<Recipe>("Recipes");
var doc = collection.FindAllAs<Recipe>(); // exception

Why i get this Exception

"The property '_id' of type 'CourseServer.Models.Recipe' cannot use element name '_id' because it is already being used by property '_id'."

I have class Person where i use same attributes and it works.

public class Person
{
    [BsonId]
    [BsonRepresentation(BsonType.ObjectId)]
    public string _id { get; set; } // and other some fields
like image 935
Vladyslav Zotov Avatar asked Jun 13 '14 14:06

Vladyslav Zotov


1 Answers

_id is reserved by MongoDb. Try using "Id" instead.

like image 189
Doug Johnson Avatar answered Oct 15 '22 13:10

Doug Johnson