2

I have made my custom model to use in my View, and when I try to use it I get the error

The entity or complex type 'Prep2Model.TechProfile' cannot be constructed in a LINQ to Entities query.

And I don't know how to resolve it.
Here's my relevant classes and controller

Custom View Model

namespace Prep2.Models
{
public class MyProjectViewModel
{
    public IEnumerable<Project> Project { get; set; }
    public IEnumerable<MyProjectCustomObject> Data { get; set; }
}
}

MyProjectCustomObject

namespace Prep2.Models
{
public class MyProjectCustomObject
{
    public int ProjectId { get; set; }
    public string ProjectCustomer { get; set; }
    public string ProjectName { get; set; }

    public IEnumerable<TechProfile> ProjectTechProfile { get; set; }

    public int MemberId { get; set; }
    public string MemberRole { get; set; }
    public short? MemberStart { get; set; }
    public short? MemberEnd { get; set; }
}
}

Controller

        var Projects = from a in db.Project
                      select a;

        var Data = from a in db.Member
                   where a.Person.PersonId.Equals(Id)
                   select new MyProjectCustomObject
                   {
                       ProjectId = a.Project.ProjectId,
                       ProjectCustomer = a.Project.Customer,
                       ProjectName = a.Project.Name,

                       ProjectTechProfile = a.Project.TechProfile.Select(x => new TechProfile()
                       {
                           TechProfileId = x.TechProfileId,
                           Name = x.Name,
                           Elements = x.Elements
                       }),

                       MemberId = a.MemberId,
                       MemberRole = a.Role,
                       MemberStart = a.Start,
                       MemberEnd = a.End
                   };

        var MyViewModel = new MyProjectViewModel();
        MyViewModel.Project = Projects;
        MyViewModel.Data = Data;

        //return ViewModel to View.
        return View(MyViewModel);
  • This is an error with Entity Framework rather than MVC. [**StackOverflow:** The entity cannot be constructed in a LINQ to Entities query][1] [1]: http://stackoverflow.com/questions/5325797/the-entity-cannot-be-constructed-in-a-linq-to-entities-query – Dan Apr 27 '15 at 12:48

1 Answers1

1

What it's saying is it can't create a new TechProfile record inside the select:

x => new TechProfile()
{
    TechProfileId = x.TechProfileId,
    Name = x.Name,
    Elements = x.Elements
}

I believe you're looking for this:

ProjectTechProfile = a.Project.TechProfile.First(x =>
{
    TechProfileId = x.TechProfileId,
    Name = x.Name,
    Elements = x.Elements
})

UPDATE You have two options that I can see then. First, you can create a different class that mimics the TechProfile class and do the following:

ProjectTechProfile = a.Project.TechProfile.Select(x => new MyTechProfile
{
    TechProfileId = x.TechProfileId,
    Name = x.Name,
    Elements = x.Elements
})

or you can send the results to a local collection first, thereby issuing the SQL statement, and then project them into the MyProjectCustomObject:

var Data = from a in db.Member
           where a.Person.PersonId.Equals(Id)
           select a;
var list = Data.ToList();
var projectedList = list.Select(a => new MyProjectCustomObject
{
    ProjectId = a.Project.ProjectId,
    ProjectCustomer = a.Project.Customer,
    ProjectName = a.Project.Name,
    ProjectTechProfile = a.Project.TechProfile.Select(x => new TechProfile()
    {
        TechProfileId = x.TechProfileId,
        Name = x.Name,
        Elements = x.Elements
    }),
    MemberId = a.MemberId,
    MemberRole = a.Role,
    MemberStart = a.Start,
    MemberEnd = a.End
});
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232