0
votes

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.

1
Are you having any table called TaskViewModel in your database? - Jenish Rabadiya
No, it's not part of the database. However, I was able to successfully use a ProjectViewModel class w/o adding it to the database. - user1147941
Then why have you applied line builder.EntitySet<ViewModels.TaskViewModel>("TaskViewModel"); to your configuration? - Jenish Rabadiya
Everything I had read stated that had to be in there. If I take it out, I receive the error: "The given model does not contain the type 'MyApplication.ViewModels.TaskViewModel'. - user1147941

1 Answers

1
votes

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.