I had the same Exception but none of the answers above gave me a solution.
I figured out the error by defining a trace in web.config :
I ran the website locally, fired a few failing calls and the opened the tracelog selecting the failed entry, inside that entry I saw this exception :
The entity or complex type 'Person' cannot be constructed in a LINQ to Entities query.
Constructing the person outside of the query fixed the issue so instead of :
var persons = (from p in db.Persons
select new Person
{
Id = p.Id,
Name = p.Name,
LastName = p.LastName,
Email = p.Email
}
).AsEnumerable();
return persons;
I had to do :
var persons = (from p in db.Persons
select new
{
Id = p.Id,
Name = p.Name,
LastName = p.LastName,
Email = p.Email
}
).AsEnumerable()
//construct the complex type outside of DB (to prevent the exception that model cannot be constructed in linq to entities)
.Select(item =>
new Person
{
Id = item.Id,
Name = item.Name,
LastName = item.LastName,
Email = item.Email
}).ToList();
return persons;
I could have saved alot of time if the exception message was more comprehensive, judging by all the different causes outlined in this topic I guess people will agree with me.
Anyway, this is how I figured out the issue, hopefully this will help someone else to