Entity cannot be constructed in a LINQ to Entities query.

Mark Kopple 0 Reputation points
2023-02-20T16:52:47.98+00:00
using (var ctx = new MainWebsiteEntities())
                {
                    model = (IEnumerable<BoardMember>)ctx.BoardMembers.Where(s => s.id == id).Select(e => new BoardMember
                    {
                        BoardMemberName = e.BoardMemberName,
                        CompanyName = e.CompanyName,
                        Group = e.Group,
                        id = (int)e.id

                    }).FirstOrDefault();
}
I get the following error: cannot be constructed in a LINQ to Entities query.

Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 25,296 Reputation points
    2023-02-21T02:39:26.58+00:00

    @Mark Kopple , Welcome to Microsoft Q&A, based on my test, I reproduced your problem.

    The error cannot be constructed in a LINQ to Entities query means that you could not (and should not be able to) project onto a mapped entity.

    If you want to get the model of BoardMember, you could simplify your code into the following code:

    var model = context.BoardMembers.Where(s => s.Id == 1).FirstOrDefault();
    

    Best Regards,

    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.