Ich habe Probleme, mein Modell die Id
-Eigenschaft einer Entität als Zeichenfolge darzustellen, aber es automatisch erstellt und intern von MongoDb als ein natives dargestellt.MongoDB C# -Treiber - Wie wird _id als ObjectId gespeichert, sondern der String-ID-Eigenschaft zugeordnet?
class Account
{
public string Id { get; set; }
...
}
class AccountStore
{
static AccountStore()
{
BsonClassMap.RegisterClassMap<Account>(cm =>
{
cm.AutoMap();
cm.SetIgnoreExtraElements(true);
// map Id property here
});
}
public void Save(Account account)
{
_accounts.Save(account);
}
}
Für die Linie // map Id property here
in dem obigen Code, habe ich zahlreiche verschiedene Möglichkeiten zur Konfiguration des ID-Mapping und keine gearbeitet hat versucht. Die Art und Weise habe ich versucht, und die damit verbundenen Ausnahmen, die ausgelöst werden, wenn ich die Save
Methode aufrufen, sind:
// Exception: No IdGenerator found.
cm.IdMemberMap
.SetRepresentation(BsonType.ObjectId);
// Exception: No IdGenerator found.
cm.IdMemberMap
.SetRepresentation(BsonType.String);
// Exception: Unable to cast object of type 'MongoDB.Bson.ObjectId' to type 'System.String'.
cm.IdMemberMap
.SetRepresentation(BsonType.ObjectId)
.SetIdGenerator(ObjectIdGenerator.Instance);
// Exception: Unable to cast object of type 'MongoDB.Bson.ObjectId' to type 'System.String'.
cm.IdMemberMap
.SetRepresentation(BsonType.String)
.SetIdGenerator(ObjectIdGenerator.Instance);
// Exception: Unable to cast object of type 'MongoDB.Bson.ObjectId' to type 'System.String'.
cm.IdMemberMap
.SetIdGenerator(ObjectIdGenerator.Instance);
Was mache ich falsch? Ich dachte, das wäre ein Standard-Anwendungsfall für das ID-Handling?
Mögliche Duplikat [Wie BsonRepresentation Attribut durch Konvention anzuwenden, wenn MongoDB mit] (https://stackoverflow.com/questions/45043266/how-to- anwenden-bsonpräsentieren ation-attribute-by-convention-wenn-mit-mongodb) – dnickless