如何在MongoDB C#Driver中全局设置属性

时间:2016-01-06 09:28:05

标签: 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将它设置为所有日期时间字段的全局?

1 个答案:

答案 0 :(得分:2)

BsonSerializer.RegisterSerializer(typeof(DateTime),
         new DateTimeSerializer(DateTimeKind.Local));

已解决我的问题