Showing posts with label entity cannot be constructed. Show all posts
Showing posts with label entity cannot be constructed. Show all posts

Wednesday, April 19, 2017

The entity cannot be constructed in a LINQ to Entities query

Issue:


I had a problem casting an anonymous type to a type of a ModelView class that I had defined.
The query worked but failed on my conversion to the type PayScheduleCurrencyModelView.

Incorrect:

 var currencyDetail = (from hn in db.Host_Nation
                                      join cur in db.Currencies on hn.Currency_ID equals cur.Currency_ID
                                      where hn.Host_Nation_ID.Equals(country)
                                      select new PayScheduleCurrencyModelView
                                      {
                                          Country_ID = hn.Host_Nation_ID,
                                          Country = hn.Host_Nation_Name,
                                          Currency_Code = cur.Code,
                                          Currency_Description = cur.Description,
                                          Exchange_Rate = cur.Rate
                                      }).AsEnumerable().Select(x => new PayScheduleCurrencyModelView
                                      {
                                          Country_ID = x.Country_ID,
                                          Country = x.Country,
                                          Currency_Code = x.Currency_Code,
                                          Currency_Description = x.Currency_Description,
                                          Exchange_Rate = x.Exchange_Rate
                                      }).FirstOrDefault();


Correct:


 var currencyDetail = (from hn in db.Host_Nation
                                      join cur in db.Currencies on hn.Currency_ID equals cur.Currency_ID
                                      where hn.Host_Nation_ID.Equals(country)
                                      select new 
                                      {
                                          Country_ID = hn.Host_Nation_ID,
                                          Country = hn.Host_Nation_Name,
                                          Currency_Code = cur.Code,
                                          Currency_Description = cur.Description,
                                          Exchange_Rate = cur.Rate
                                      }).AsEnumerable().Select(x => new PayScheduleCurrencyModelView
                                      {
                                          Country_ID = x.Country_ID,
                                          Country = x.Country,
                                          Currency_Code = x.Currency_Code,
                                          Currency_Description = x.Currency_Description,
                                          Exchange_Rate = x.Exchange_Rate
                                      }).FirstOrDefault();


The fix was to drop the implied cast in the linq to sql query.  This cast was causing the EF error.


Source:


  1. http://stackoverflow.com/questions/12916080/the-entity-or-complex-type-cannot-be-constructed-in-a-linq-to-entities-query