RavenDB: Returned Objects with Zero Property Values

I am using Raven in ASP.NET MVC as follows:

[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); } 

It is strange that the model object has one value (as I would expect, because I know that I have one Workflow document in my database), but it has both the WorkflowId and WorkfloName properties of null . Why is this? Is there a problem with my projection?

I tried moving the call of ToArray() to to Select() , and this works fine:

 [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); } 
+4
source share
1 answer

If you change the names of your properties in the view model in the same way as the Workflow class, it will work.

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

But this is just a workaround to what seems like a mistake or limitation.

+1
source

Source: https://habr.com/ru/post/1380223/


All Articles