c# - 如何在 MongoDB C# 驱动程序中为字段全局设置属性

标签 c# mongodb

这是在 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/

相关文章:

c# - 从 url 读取数据返回空值

c# - DataGrid 宽度上的 WPF 全宽 DataGrid 列

c# - 如何根据按钮点击设置Label的内容

javascript - Python、 Bokeh 、Javascript : use Mongodb database query in callback

mongodb - 更改 MongoDB 集合的主键

python - 如何在 Python 中将十六进制字符串转换为 ObjectId

linux - 自定义端口上的 MongoDB 服务未运行

java - MongoDB——Java | $near 的使用

c# - 在后台运行程序 Wix

c# - Documentformat.openxml - 使用默认样式创建 word 文档