Explicit construction of entity type


  • I have a DataModel that contains a Hour table and a HourService that contains the pregenerated CRUD methods.

    I have modified the GetHours() to this:

    public IQueryable GetHours()
    return from hours in this.Context.Hours
    select new Hour()
    HourID = hours.HourID

    The reason why I construct a new Hour and set the properties manually is that I have some custom properties defined in a shared.cs file that I'm going to set manually.

    This code compiles fine but in runtime I get the following error:

    "Explicit construction of entity type 'DataModel.Hour' in query is not allowed."

    Any help is appreciated!

    Sunday, October 18, 2009 11:00 AM


All replies

  • I have tried to instantiate and return an other type of entity that is not part of the DataModel - that works just fine.

    Could it be a Linq to Sql problem?

    Sunday, October 18, 2009 11:17 AM
  • Hi,

    There are discusstion about exception like that at post.

    Sunday, October 18, 2009 11:18 AM
  • Try using func<T,TResult>:

    Func<Hours, Hours> make =

    h => new Hours { HourID= h.HourID };

    return from h in this.Context.Hours

    select make(h);

     Hope this can help


    Friday, March 12, 2010 8:59 AM
  • hi

    jcottino Thank you Worked like a Charm.. WinkLaughing




    Friday, February 24, 2012 9:38 AM