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

时间:2014-02-12 18:19:15

标签: c# mongodb

我有这个枚举:

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

我有以下课程:

[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();

我得到一个例外:Element 'eventType' does not match any field or property of class LogAggregation.PublicLibrary.Models.Event.

我做错了什么?

1 个答案:

答案 0 :(得分:3)

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

 [BsonElement("eventType")]

而不是

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