简体   繁体   中英

Mongo failing to deserialize class with enums

I have this enum:

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

I have the following 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; }
    }

And when I insert it into my mongo collection I get the following object (in 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" }

Now, when I try to read the the object like this:

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

I get an exception: Element 'eventType' does not match any field or property of class LogAggregation.PublicLibrary.Models.Event.

What am I doing wrong?

Try using the BsonAttributes to control serialization/deserialization overrides

 [BsonElement("eventType")]

Instead of

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM