I used the ADO.NET C# POCO Entity Generator Visual Studio add-in to generate POCO classes for my entities.
When I try to use the class in a Linq to Entities query such as the one below:
var q = from w in entities.Widgets
select new Widget
{
Id = w.Id,
WidgetName = w.WidgetName,
WidgetDescription = w.WidgetDescription
};
return q.ToList();
I get the following exception:
"The entity or complex type MyNamespace.Widget' cannot be constructed in a LINQ to Entities query".
The only way around this is to use an anonymous type and then another LINQ query:
var q = from w in entities.Widgets
select new
{
Id = w.Id,
WidgetName = w.WidgetName,
WidgetDescription = w.WidgetDescription
};
var r = from e in q.AsEnumerable()
select new Widget
{
Id = e.Id,
WidgetName = e.WidgetName,
WidgetDescription = e.WidgetDescription
};
return r.ToList();
This works but is pretty redundant. I understand why I'm getting the exception, but is there a more elegant way around this?
The fact that the POCO classes are generated by the ADO.NET C# POCO Entity Generator doesn't seem related to the issue; I tried using my own POCO classes and saw the same exception.
Many thanks.
EDIT: Added link to walkthrough for using the ADO.NET C# POCO Entity Generator Visual Studio add-in - http://blogs.msdn.com/b/adonet/archive/2010/01/25/walkthrough-poco-template-for-the-entity-framework.aspx