MongoCollectionSettings.GuidRepresentation 已过时,有什么替代方案?
MongoCollectionSettings.GuidRepresentation is obsolete, what's the alternative?
我正在使用 MongoDB.Driver 2.11.0 和 .Net Standard 2.1。为了确保数据库存在并且集合存在,我有以下代码:
IMongoClient client = ...; // inject a Mongo client
MongoDatabaseSettings dbSettings = new MongoDatabaseSettings();
IMongoDatabase db = client.GetDatabase("MyDatabase", dbSettings);
MongoCollectionSettings collectionSettings = new MongoCollectionSettings()
{
GuidRepresentation = GuidRepresentation.Standard,
};
IMongoCollection<MyClass> collection = db.GetCollection<MyClass>("MyClasses", collectionSettings);
在 MongoDB.Driver 的早期版本中,此代码将在没有任何警告的情况下编译。在 v2.11.0 中,我现在收到一条警告“MongoCollectionSettings.GuidRepresentation 已过时:改为配置序列化器”,但我无法找到任何说明设置 Guid 序列化格式的新方法的示例。有谁知道为集合设置序列化程序的其他方法吗?
在最新版本中已更改,请在此处查看详细信息:https://mongodb.github.io/mongo-csharp-driver/2.11/reference/bson/guidserialization/
如果你想为特定的 属性 定义 GuidRepresentation,你可以在注册 class 地图时进行,如下所示:
BsonClassMap.RegisterClassMap<MyClass>(m =>
{
m.AutoMap();
m.MapIdMember(d => d.Id).SetSerializer(new GuidSerializer(GuidRepresentation.Standard));
});
如果你想在全球范围内进行:
BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
我正在使用 MongoDB.Driver 2.11.0 和 .Net Standard 2.1。为了确保数据库存在并且集合存在,我有以下代码:
IMongoClient client = ...; // inject a Mongo client
MongoDatabaseSettings dbSettings = new MongoDatabaseSettings();
IMongoDatabase db = client.GetDatabase("MyDatabase", dbSettings);
MongoCollectionSettings collectionSettings = new MongoCollectionSettings()
{
GuidRepresentation = GuidRepresentation.Standard,
};
IMongoCollection<MyClass> collection = db.GetCollection<MyClass>("MyClasses", collectionSettings);
在 MongoDB.Driver 的早期版本中,此代码将在没有任何警告的情况下编译。在 v2.11.0 中,我现在收到一条警告“MongoCollectionSettings.GuidRepresentation 已过时:改为配置序列化器”,但我无法找到任何说明设置 Guid 序列化格式的新方法的示例。有谁知道为集合设置序列化程序的其他方法吗?
在最新版本中已更改,请在此处查看详细信息:https://mongodb.github.io/mongo-csharp-driver/2.11/reference/bson/guidserialization/
如果你想为特定的 属性 定义 GuidRepresentation,你可以在注册 class 地图时进行,如下所示:
BsonClassMap.RegisterClassMap<MyClass>(m =>
{
m.AutoMap();
m.MapIdMember(d => d.Id).SetSerializer(new GuidSerializer(GuidRepresentation.Standard));
});
如果你想在全球范围内进行:
BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));