Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6049797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:37:02+00:00 2026-05-23T07:37:02+00:00

Following on from this question , I’m using a simple ViewModel class to test

  • 0

Following on from this question, I’m using a simple ViewModel class to test passing a strongly-typed model to a view. Whilst Darin’s answer to my original question has solved the problem I had, it’s now left me scratching my head as to why I can’t do the same thing with an EF generated class. Here’s what I have:

public ActionResult Test()
{
    using (MyEntities db = new MyEntities())
    {
        var model = from c in db.Categories
                    select new CategoryViewModel { CategoryID = c.CategoryID, Name = c.Name };

        return View(model.ToList());
    }
}

This is CategoryViewModel:

public class CategoryViewModel
{
    public int CategoryID { get; set; }
    public string Name { get; set; }
}

In my view I’m simply foreaching over the model. This produces the desired results. However, one of the classes that was generated for me by EF, is Category. I was initially trying to use it to build the strongly-typed model and pass that to the view instead, like so:

public ActionResult Test()
{
    using (MyEntities db = new MyEntities())
    {
        var model = from c in db.Categories
                    select new Category { CategoryID = c.CategoryID, Name = c.Name };

        return View(model.ToList());
    }
}

This was giving me the following error:

“The entity or complex type ‘MyProjectModel.Category’ cannot be constructed in a LINQ to Entities query.”

So my question is, what is the difference between CategoryViewModel and Category other than Category being generated for me? What is preventing me from using Category as my model type in this scenario? I’ve been looking through other questions (and answers) on SO today but I’ve been unable to find something that discusses this. I’ve also seen a few other new terms mentioned in those posts (buddy classes, automappers, POCO) but it seems premature to look into what they are without understanding the foundation of my problem.

I’d really appreciate some insight.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:37:02+00:00Added an answer on May 23, 2026 at 7:37 am

    Though I don’t know exactly why I bet that the entity framework doesn’t like selecting new objects of the same type in the property you are writing your query off of. If my assumption is correct the following code should be equivalent to what you are trying to do:

    public ActionResult Test()
    {
        using (MyEntities db = new MyEntities())
        {
            var model = from c in db.Categories;
    
            return View(model.ToList());
        }
    }
    

    You would only need to select new objects when you are mapping the properties of your context’s classes to another class (IE not one that is generated by the EF).

    I did a little more research into the issue, including duplicating it myself, according to the stack trace this is where the exception is thrown: System.Data.Objects.ELinq.ExpressionConverter.CheckInitializerType(Type type)
    Here is the source code for CheckInitializerType (found here):

     // Determines whether the given type is supported for materialization
        private void CheckInitializerType(Type type)
        {
            // nominal types are not supported
            TypeUsage typeUsage;
            if (_funcletizer.RootContext.Perspective.TryGetType(type, out typeUsage))
            {
                  BuiltInTypeKind typeKind = typeUsage.EdmType.BuiltInTypeKind;
                  if (BuiltInTypeKind.EntityType == typeKind ||
                      BuiltInTypeKind.ComplexType == typeKind)
                  {
                      throw EntityUtil.NotSupported(System.Data.Entity.Strings.ELinq_UnsupportedNominalType(
                                typeUsage.EdmType.FullName));
                  }
            }
    
            // types implementing IEnumerable are not supported
            if (TypeSystem.IsSequenceType(type))
            {
                 throw EntityUtil.NotSupported(System.Data.Entity.Strings.ELinq_UnsupportedEnumerableType(
                            DescribeClrType(type)));
            }
       }
    

    For reasons I haven’t determined yet, if the object you are trying to project properties onto has the BuiltInTypeKind of EntityType or ComplexType, then projection is not supported. With my testing I discovered that I can project properties onto an entity that was generated with another edmx file, so the relationship with a given ObjectContext seems to boil down to the System.Data.Objects.DataClasses.EdmEntityTypeAttribute that decorates the generated Entity class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the following code that I originally found from this question: Prevent
Following on from this question what would be the best way to write a
Following up from this question: How can I unlock a file that is locked
Following on from this question , I now want to know how to stop
Following on from this question I now have code that can attach to a
Following on from this question, what would be the best way to represent a
Following on from this question: Run WCF ServiceHost with multiple contracts Are there any
This question is overflow from the following question: How do I programmatically convert mp3
This question comes from my experience with the following question: https://stackoverflow.com/questions/492748/new-responses-icon-on-so-crashes-ie7-closed In that question,
Following on from this question, I still seem to be battling at the frontiers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.