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
_id is reserved by MongoDb. Try using "Id" instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With