我曾在我看來,使用個性化的模型,當我嘗試使用它,我得到的錯誤錯誤在查看自定義模型
實體或複雜類型「Prep2Model.TechProfile」不能在LINQ to Entities查詢中構造。
而且我不知道如何解決它。
這裏是我的相關類和控制器
自定義視圖模式
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; }
}
}
控制器
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);
這是實體框架而不是MVC的錯誤。 [**的StackOverflow:**實體不能在LINQ被構造成實體查詢] [1] [1]:http://stackoverflow.com/questions/5325797/the- entity-can-be-constructed-in-a-linq-to-entities-query – Dan