这是在 mongoDb 中实现映射的代码
public static void MapEntity<TEntity>() where TEntity : IEntity
{
BsonClassMap.RegisterClassMap<TEntity>(e =>
{
e.AutoMap();
e.MapIdProperty(u => u.Id)
.SetIdGenerator(StringObjectIdGenerator.Instance)
.SetSerializer(new StringSerializer(BsonType.ObjectId));
});
}
我在实体类中有一个这样的字段
[BsonDateTimeOptions(Kind = DateTimeKind.Local)]
public DateTime Received { get; set; }
我在许多实体中都有日期时间字段,我需要将 [BsonDateTimeOptions(Kind = DateTimeKind.Local)]
应用于所有实体。有没有一种方法可以使用 BsonClassMap 将其设置为所有日期时间字段的全局变量?
最佳答案
BsonSerializer.RegisterSerializer(typeof(DateTime),
new DateTimeSerializer(DateTimeKind.Local));
已解决我的问题
关于c# - 如何在 MongoDB C# 驱动程序中为字段全局设置属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34629583/