Reputation: 121
I'm attempting to return a custom view model from an ODataController. Here is the view model:
public class TaskViewModel
{
public int TaskID { get; set; }
public string Name { get; set;}
public string Details { get; set; }
public DateTime? DueDate { get; set; }
public virtual Project Project { get; set; }
}
My controller looks like this:
public IEnumerable<TaskViewModel> Get(ODataQueryOptions<TaskViewModel> options)
{
IEnumerable<Task> tasks = db.Tasks.AsEnumerable();
...
var vm = from t in tasks
select new TaskViewModel
{
TaskID = t.TaskID,
Name = t.Name,
Details = t.Details,
DueDate = t.DueDate,
Project = t.Project
}
return vm.AsEnumerable();
}
The Task entity looks like this (generated from database-first EDMX):
public partial class Task
{
public int TaskID { get; set; }
public string Name { get; set; }
public string Details { get; set; }
public DateTime? DueDate { get; set; }
... //other properties I don't want exposed
public virtual Project Project { get; set; }
}
When I attempt to perform a GET, I receive the following message:
"No NavigationLink factory was found for the navigation property 'Project' from entity type 'MyApplication.ViewModels.TaskViewModel' on entity set 'Tasks'. Try calling HasNavigationPropertyLink on the EntitySetConfiguration."
Here is my WebApiConfig.cs:
builder.EntitySet<Project>("Projects").EntityType.HasKey(p => p.ProjectID);
builder.EntitySet<Task>("Tasks").EntityType.HasKey(t => t.TaskID);
...
builder.EntitySet<ViewModels.TaskViewModel>("TaskViewModel");
I've successfully done this with another entity in the application (Project), so I don't know why this one isn't working. The only difference is the Task entity references the Project entity.
Any help/insight is appreciated.
Upvotes: 0
Views: 1720
Reputation: 121
I was able to get it working. All I did was make the TaskViewModel class a derived class of Task.
From:
public class TaskViewModel
{
public int TaskID { get; set; }
...
public string CurrentStatus { get; set; }
}
To:
public class TaskViewModel : Task
{
public string CurrentStatus { get; set; }
}
It's also worth noting that this worked with or without the entry for TaskViewModel in WebApiConfig.cs.
Upvotes: 1