C# 无法从MongoDB文档中读取TimeZoneInfo

C# 无法从MongoDB文档中读取TimeZoneInfo,c#,mongodb,timezone,mongodb-query,C#,Mongodb,Timezone,Mongodb Query,我有一个C#对象,它包含一个TimeZoneInfo属性。我可以把它保存到MongoDB。但是当我把它取回的时候,它是空的。所有其他属性都已正确映射 我的DTO结构如下所示;还有其他一些东西,但我只提到了几个属性: public class TerminalDto { public string Code { get; set; } public GeographyDto Geography { get; set; } public GeoCoordinateDto Ge

我有一个C#对象,它包含一个
TimeZoneInfo
属性。我可以把它保存到MongoDB。但是当我把它取回的时候,它是空的。所有其他属性都已正确映射

我的
DTO
结构如下所示;还有其他一些东西,但我只提到了几个属性:

public class TerminalDto
{
    public string Code { get; set; }
    public GeographyDto Geography { get; set; }
    public GeoCoordinateDto GeoCoordinate { get; set; }
    public TimeZoneInfo TimeZone { get; set; }
}
我的mongo文档存储为:

{
    "_id": "5bc4601d5d46855e6c8a337b",
    "Code": "AK",
    "Geography": {
        "City": "Akron",
        "State": {
            "Name": "OHIO",
            "Code": "OH"
        }
    },
    "GeoCoordinate": {
        "Latitude": "40.97665",
        "Longitude": "-81.464607"
    },
    "TimeZone": {
        "_id": "Eastern"
    }
}
当我读回时,我的
DTO
属性被填充,除了
时区信息

{
    "_id": "5bc4601d5d46855e6c8a337b",
    "Code": "AK",
    "Geography": {
        "City": "Akron",
        "State": {
            "Name": "OHIO",
            "Code": "OH"
        }
    },
    "GeoCoordinate": {
        "Latitude": "40.97665",
        "Longitude": "-81.464607"
    },
    "TimeZone": {} // Empty Here
}
我的终端存储库是这样的

public class TerminalRepository
{
    public TerminalRepository(IMongoConnectionFactory mongoConnectionFactory)
    {
        this.collection = mongoConnectionFactory.GetCollection<TerminalDto>();
    }

    private readonly IMongoCollection<TerminalDto> collection;

    public async Task<IEnumerable<TerminalDto>> GetTerminals(int scenarioId)
    {
        var filter = Builders<TerminalDto>.Filter.Eq(t => t.ScenarioId, scenarioId);
        var dtos = (await this.collection.FindAsync(filter)).ToList();
    }
}
公共类终端存储库
{
公共终端存储库(IMongoConnectionFactory mongoConnectionFactory)
{
this.collection=mongoConnectionFactory.GetCollection();
}
私人只读IMongoCollection;
公共异步任务GetTerminals(int scenarioId)
{
var filter=Builders.filter.Eq(t=>t.ScenarioId,ScenarioId);
var dtos=(等待这个.collection.FindAsync(filter)).ToList();
}
}
我尝试搜索MongoDB官方文档,但找不到任何与存储
TimeZoneInfo
相关的信息

{
    "_id": "5bc4601d5d46855e6c8a337b",
    "Code": "AK",
    "Geography": {
        "City": "Akron",
        "State": {
            "Name": "OHIO",
            "Code": "OH"
        }
    },
    "GeoCoordinate": {
        "Latitude": "40.97665",
        "Longitude": "-81.464607"
    },
    "TimeZone": {} // Empty Here
}

如何计算?

您不应该将
TimeZoneInfo
类序列化到文档中,而应该只序列化
.Id
属性。有很多方法可以做到这一点,但其中一种方法是使用“好友属性”,例如:

public class TerminalDto
{
    // ... your other properties ...

    public string TimeZoneId { get; set; }

    [BsonIgnore]
    public TimeZoneInfo TimeZone
    {
        get
        {
            return TimeZoneInfo.FindSystemTimeZoneById(this.TimeZoneId);
        }

        set
        {
            this.TimeZoneId = value.Id;
        }
    }
}

@JohnnyHK更新了。请看一看。@JohnnyHK TimeZoneInfo是C#中的系统定义类。我指的是这个