Storing Enums as strings in MongoDB

后端 未结 9 1246
盖世英雄少女心
盖世英雄少女心 2020-12-08 03:50

Is there a way to store Enums as string names rather than ordinal values?

Example:

Imagine I\'ve got this enum:

public enum Gender
{
    Fema         


        
9条回答
  •  感情败类
    2020-12-08 04:35

    using MongoDB.Bson;
    using MongoDB.Bson.Serialization.Attributes;
    
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;
    
    public class Person
    {
        [JsonConverter(typeof(StringEnumConverter))]  // JSON.Net
        [BsonRepresentation(BsonType.String)]         // Mongo
        public Gender Gender { get; set; }
    }
    

提交回复
热议问题