RavenDB:返回属性为null值的对象

时间:2011-11-08 16:04:46

标签: c# .net linq ravendb

我在ASP.NET MVC中使用Raven如下:

[HttpGet]
public ActionResult Index()
{
    ViewBag.Title = Strings.Workflows;
    ViewBag.AddNewText = Strings.Add_new;

    IEnumerable<WorkflowIndexViewModel> model;
    using (var session = DocumentStore.OpenSession())
    {
        model = session.Query<Workflow>()
            .Select(w => new WorkflowIndexViewModel { WorkflowId = w.Id, WorkflowName = w.Name })
            .ToArray();
    }

    return View(model);
}

奇怪的是model对象中有一个值(正如我所期望的那样,因为我知道我的数据库中有一个Workflow文档),但WorkflowIdWorkfloName上的null属性为ToArray()。为什么是这样?我的预测有问题吗?

我已尝试将Select()调用调用之前的[HttpGet] public ActionResult Index() { ViewBag.Title = Strings.Workflows; ViewBag.AddNewText = Strings.Add_new; IEnumerable<WorkflowIndexViewModel> model; using (var session = DocumentStore.OpenSession()) { model = session.Query<Workflow>() .ToArray() .Select(w => new WorkflowIndexViewModel { WorkflowId = w.Id, WorkflowName = w.Name }); } return View(model); } ,这样就可以了:

{{1}}

1 个答案:

答案 0 :(得分:1)

如果您将视图模型中属性的名称更改为与Workflow类相同,则它将起作用。

model = session.Query<Workflow>()
        .Select(w => new WorkflowIndexViewModel { Id = w.Id, Name = w.Name })
        .ToArray();

但这只是一个似乎是错误或限制的解决方法。