BsonDateTimeOptions 不设置本地日期时间
BsonDateTimeOptions doesn't set local datetime
虽然我用 BsonDateTimeOptions
装饰了我的 Datetime
属性 ,它仍然不起作用并且时间被插入到数据库中,比我当地时间晚 3 小时。(我认为它是 utc )
我的基础摘要 class
public abstract class MongoBaseModel
{
public ObjectId Id { get; set; }
[BsonElement]
[BsonDateTimeOptions(Kind = DateTimeKind.Local)]
public DateTime InsertedAt{ get; set; }
}
我的实体
public class RockSongs:MongoBaseModel
{
[BsonElement("Name")]
public string Name { get; set; }
[BsonElement("Singer")]
public string Singer { get; set; }
}
db版本v4.2.1
MongoDb.Driver 2.7.2
MongoDB stores times in UTC by default, and will convert any local time representations into this form. Applications that must operate or report on some unmodified local time value may store the time zone alongside the UTC timestamp, and compute the original local time in their application logic.
在序列化方面,DateTimeKind.Local
和DateTimeKind.Utc
没有区别。在这两种情况下,您的 .NET DateTime
都将存储为 UTC。这个属性派上用场的时刻是反序列化。 MongoDB 如果您应用 DateTimeKind.Local
.
,.NET 驱动程序将自动将您的 UTC 日期时间转换为本地计算机的时区
MongoDB 将日期存储为 UTC 的事实直接来自 BSON 规范。
虽然我用 BsonDateTimeOptions
装饰了我的 Datetime
属性 ,它仍然不起作用并且时间被插入到数据库中,比我当地时间晚 3 小时。(我认为它是 utc )
我的基础摘要 class
public abstract class MongoBaseModel
{
public ObjectId Id { get; set; }
[BsonElement]
[BsonDateTimeOptions(Kind = DateTimeKind.Local)]
public DateTime InsertedAt{ get; set; }
}
我的实体
public class RockSongs:MongoBaseModel
{
[BsonElement("Name")]
public string Name { get; set; }
[BsonElement("Singer")]
public string Singer { get; set; }
}
db版本v4.2.1
MongoDb.Driver 2.7.2
MongoDB stores times in UTC by default, and will convert any local time representations into this form. Applications that must operate or report on some unmodified local time value may store the time zone alongside the UTC timestamp, and compute the original local time in their application logic.
在序列化方面,DateTimeKind.Local
和DateTimeKind.Utc
没有区别。在这两种情况下,您的 .NET DateTime
都将存储为 UTC。这个属性派上用场的时刻是反序列化。 MongoDB 如果您应用 DateTimeKind.Local
.
MongoDB 将日期存储为 UTC 的事实直接来自 BSON 规范。