一个枚举值存储的问题
枚举是编程中常见的类型,经常在代码中使用。枚举的好处就不说了,需要注意的一点是在编译时会变成静态int。这个地方需要注意一下,不然会出现问题。
如下代码定义枚举,并把数据存储到数据库中。
using System;
using System.Linq;
using Wisdombud.Mongo;
namespace MongoTest
{
public class MongoEnumTest
{
private MongoDbBaseDao<MyEnum> dao = new MongoDbBaseDao<MyEnum>("mongodb://192.168.163.36:27017/chzhaotest");
public void Test()
{
MyEnum me1 = new MyEnum();
me1.Name = "company1";
me1.WorkDay = Days.Mon;
MyEnum me2 = new MyEnum();
me2.Name = "company2";
me2.WorkDay = Days.Fri;
this.dao.Save(me1);
this.dao.Save(me2);
MyEnum me3 = this.dao.GetAll().ToList().First();
Console.WriteLine(me3.WorkDay + me3.Name);
}
}
public enum Days { Mon, Tue, Wed, Thu, Fri };
public class MyEnum : Wisdombud.Mongo.MongoDbEntity
{
public string Name { get; set; }
public Days WorkDay { get; set; }
}
}
数据存储到数据库中是这样的[mongodb]。
/* 0 */
{
"_id" : ObjectId("551d0ce1b15ae72abc8b42e9"),
"Name" : "company1",
"WorkDay" : 0
}
/* 1 */
{
"_id" : ObjectId("551d0ce2b15ae72abc8b42ea"),
"Name" : "company2",
"WorkDay" : 4
}
如上代码运行结果的是
Moncompany1
可是如果我要增加枚举的类型,改成如下代码。
using System;
using System.Linq;
using Wisdombud.Mongo;
namespace MongoTest
{
public class MongoEnumTest
{
private MongoDbBaseDao<MyEnum> dao = new MongoDbBaseDao<MyEnum>("mongodb://192.168.163.36:27017/chzhaotest");
public void Test()
{
MyEnum me1 = new MyEnum();
me1.Name = "company1";
me1.WorkDay = Days.Mon;
MyEnum me2 = new MyEnum();
me2.Name = "company2";
me2.WorkDay = Days.Fri;
this.dao.Save(me1);
this.dao.Save(me2);
MyEnum me3 = this.dao.GetAll().ToList().First();
Console.WriteLine(me3.WorkDay + me3.Name);
}
}
public enum Days { Sta, Sun, Mon, Tue, Wed, Thu, Fri };
public class MyEnum : Wisdombud.Mongo.MongoDbEntity
{
public string Name { get; set; }
public Days WorkDay { get; set; }
}
}
注意,我在Mon前面增加了Sta和Sun。在数据库中存储的是
/* 0 */
{
"_id" : ObjectId("551d0ce1b15ae72abc8b42e9"),
"Name" : "company1",
"WorkDay" : 0
}
/* 1 */
{
"_id" : ObjectId("551d0ce2b15ae72abc8b42ea"),
"Name" : "company2",
"WorkDay" : 4
}
/* 2 */
{
"_id" : ObjectId("551d0dc1b15ae722cce4af5f"),
"Name" : "company1",
"WorkDay" : 2
}
/* 3 */
{
"_id" : ObjectId("551d0dc2b15ae722cce4af60"),
"Name" : "company2",
"WorkDay" : 6
}
两条新的数据变成了2和6,而不是0和4,也就说,当我在前面增加了新的类型之后,所有的枚举都乱了,而且数据库中存储的也错误了。一切原因都是在编译时会变成静态int*
解决这个问题的办法也很简单,在定义枚举时给枚举赋一个默认值,但要从0开始,如下:
using System;
using System.Linq;
using Wisdombud.Mongo;
namespace MongoTest
{
public class MongoEnumTest
{
private MongoDbBaseDao<MyEnum> dao = new MongoDbBaseDao<MyEnum>("mongodb://192.168.163.36:27017/chzhaotest");
public void Test()
{
MyEnum me1 = new MyEnum();
me1.Name = "company1";
me1.WorkDay = Days.Mon;
MyEnum me2 = new MyEnum();
me2.Name = "company2";
me2.WorkDay = Days.Fri;
this.dao.Save(me1);
this.dao.Save(me2);
MyEnum me3 = this.dao.GetAll().ToList().First();
Console.WriteLine(me3.WorkDay + me3.Name);
}
}
public enum Days { Sta = 5, Sun = 6, Mon = 0, Tue = 1, Wed = 2, Thu = 3, Fri = 4 };
public class MyEnum : Wisdombud.Mongo.MongoDbEntity
{
public string Name { get; set; }
public Days WorkDay { get; set; }
}
}