gpt4 book ai didi

c# - Mongo 无法使用枚举反序列化类

转载 作者:可可西里 更新时间:2023-11-01 10:04:30 24 4
gpt4 key购买 nike

我有这个枚举:

public enum EventType
{
Regular = 1,
Error = 2,
AccessDenied = 3,
Warning = 4,
Maintenance = 5,
CustomMade = 6
}

我有以下类(class):

[DataContract]
public class Event : IEvent
{
[DataMember(Name = "eventType", IsRequired = true)]
public EventType EventType { get; set; }

[DataMember(Name = "occuringDate", IsRequired = true)]
public DateTime OccuringDate { get; set; }

[DataMember(Name = "physicalServerId", IsRequired = true)]
public string PhysicalServerId { get; set; }

[DataMember(Name = "text", IsRequired = true)]
public string Text { get; set; }

[DataMember(Name = "systemIds", IsRequired = true)]
public ICollection<string> SystemIds { get; set; }

[DataMember(Name = "_id", IsRequired = true)]
public string Id { get; set; }
}

当我将它插入我的 mongo 集合时,我得到以下对象(在 mongo 中):

{ "_id" : "1", "eventType" : 1, "occuringDate" : "2014-02-12T20:04:20.4328247+02
:00", "physicalServerId" : "10", "text" : "User has logged in successfully.", "s
ystemIds" : [ "1", "3" ], "details" : "userId: 2" }

现在,当我尝试像这样读取对象时:

MongoClient mongoClient = new MongoClient(connectionString);
MongoServer mongoServer = mongoClient.GetServer();
this.db = mongoServer.GetDatabase("eventsLog");
eventsCollection = this.db.GetCollection<Event>("eventsLog");
eventsCollection.Exists();
this.eventsCollection.FindAll().SetSkip((int)(page * perPage)).SetLimit((int)perPage).ToList();

我遇到异常:元素“eventType”与类 LogAggregation.PublicLibrary.Models.Event 的任何字段或属性都不匹配。

我做错了什么?

最佳答案

尝试使用 BsonAttributes 来控制序列化/反序列化覆盖

 [BsonElement("eventType")]

代替

[DataMember(Name = "eventType", IsRequired = true)]

关于c# - Mongo 无法使用枚举反序列化类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21736317/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com