鉴于此文档类:
public class Tea
{
public String Id { get; set; }
public String Name { get; set; }
public TeaType Type { get; set; }
public Double WaterTemp { get; set; }
public Int32 SleepTime { get; set; }
}
public enum TeaType
{
Black,
Green,
Yellow,
Oolong
}
我使用以下代码存储新茶:
using (var ds = new DocumentStore { Url = "http://localhost:8080/" }.Initialize())
using (var session = ds.OpenSession("RavenDBFirstSteps"))
{
Tea tea = new Tea() { Name = "Earl Grey", Type = TeaType.Black, WaterTemp = 99d, SleepTime = 3 };
session.Store(tea);
session.SaveChanges();
Console.WriteLine(tea.Id);
}
茶会成功保存,但当我尝试用linq查询所有红茶时,我没有结果:
using (var ds = new DocumentStore { Url = "http://localhost:8080/" }.Initialize())
using (var session = ds.OpenSession("RavenDBFirstSteps"))
{
var dbTeas = from teas in session.Query<Tea>()
where teas.Type == TeaType.Black
select teas;
foreach (var dbTea in dbTeas)
{
Console.WriteLine(dbTea.Id + ": " + dbTea.Name);
}
}
我还尝试使用以下命令将Enum保存为整数:
ds.Conventions.SaveEnumsAsIntegers = true;
但结果是一样的。当我使用Name或WaterTemp时,一切正常。 RavenDB是否以这种方式支持Enums,或者我完全错了?
答案 0 :(得分:1)
似乎我得到了答案。始终不建议使用名称类型为Type的属性,该属性可以是保留关键字。
我重命名了Type,一切正常,所以答案是:
public class Tea
{
public String Id { get; set; }
public String Name { get; set; }
public TeaType TeaType { get; set; }
public Double WaterTemp { get; set; }
public Int32 SleepTime { get; set; }
}