I have created a C# class like this:
public class Employee
{
[BsonRepresentation(BsonType.ObjectId)]
public string Name { get; set; }
public int Age { get; set; }
public List<string> Address { get; set; }
}
When I try to save this information (using MongoDB) like this:
var e = new Employee();
e.Address = new List<string>();
e.Address.Add("Address 1");
e.Address.Add("Address 2");
e.Age = 333;
e.Name = "Some Name";
context.Employees.Insert(e);
I am getting following error:
An unhandled exception of type 'System.FormatException' occurred in MongoDB.Bson.dll
Additional information: 'Some Name' is not a valid 24 digit hex string.
How can I make a string field to act as ObjectID
in MongoDB?