none
The entity or complex type 'Categories' cannot be constructed in a LINQ to Entities query.

    Question

  • Hello guys,

    I am new to Entity framework Code first approach and at the beginning stage. I am working on ASP.NET MVC application. I create one class: 

    public class Category
    {
            [Key]
            public int CatId { get; set; }
            public string CategoryName { get; set; }

    }

    I want to include one more property that should not be created by database and also through LINQ i want to set some data. so i create another class as follow:

    public class CategoryWrapper : Category
        {
            public string LanguageName { get; set; }
        }

    So that by the reference of CategoryWrapper i can get Category properties as well, but i couldn't. It throws an exception "The entity or complex type CategoryWrapper cannot be constructed in a LINQ to Entities query."

    My LINQ query is:

    var myResult = (from data in db.Category
                                           join lang in db.Languages
                                           on data.LangId equals lang.Id
                                           select new CategoryWrapper
                                           {
                                               CatId = data.CatId,
                                               CategoryName = data.CategoryName,
                                               LanguageName = lang.LanguageName
                                           }).ToList();

    This gives me error "The entity or complex type CategoryWrapper cannot be constructed in a LINQ to Entities query."

    But if modify my CategoryWrapper as follow:

     public class CategoryWrapper 

        {
            public string LanguageName { get; set; }
            public int CatId { get; set; }
            public string CategoryName { get; set; }
        }

    By this it works fine.

    Can any one help me on this please that what is the issue in inheritance and getting all the properties ??

    I am not directly interacting with my Model so it should not throw an exception.

    Thanks

    Saturday, March 17, 2012 11:30 AM

Answers

  • Hi Jammy,

    This is by design, EF doesn't allow you to project the results of a query onto a mapped entity. You can either do what you've done and use a DTO which doesn't inherit from the mapped entity, or you could instantiate the CategoryWrapper in memory by first projecting to an anonymous type, then using LINQ to Objects to project to a CategoryWrapper:

    var myResult = (from data in db.Category
                                           join lang in db.Languages
                                           on data.LangId equals lang.Id
                                           select new 
                                           {
                                               CatId = data.CatId,
                                               CategoryName = data.CategoryName,
                                               LanguageName = lang.LanguageName
                                           }).ToList()
                                           .Select(c =>  new CategoryWrapper
                                           {
                                               CatId = c.CatId,
                                               CategoryName = c.CategoryName,
                                               LanguageName = c.LanguageName
                                           }).ToList();

    Regards,

    Tyler

    Saturday, March 17, 2012 12:20 PM

All replies

  • Hi Jammy,

    This is by design, EF doesn't allow you to project the results of a query onto a mapped entity. You can either do what you've done and use a DTO which doesn't inherit from the mapped entity, or you could instantiate the CategoryWrapper in memory by first projecting to an anonymous type, then using LINQ to Objects to project to a CategoryWrapper:

    var myResult = (from data in db.Category
                                           join lang in db.Languages
                                           on data.LangId equals lang.Id
                                           select new 
                                           {
                                               CatId = data.CatId,
                                               CategoryName = data.CategoryName,
                                               LanguageName = lang.LanguageName
                                           }).ToList()
                                           .Select(c =>  new CategoryWrapper
                                           {
                                               CatId = c.CatId,
                                               CategoryName = c.CategoryName,
                                               LanguageName = c.LanguageName
                                           }).ToList();

    Regards,

    Tyler

    Saturday, March 17, 2012 12:20 PM
  • Take a look on this post:

    The entity or complex type cannot be constructed in a LINQ to Entities query

    or change code to:

    var myResult = (from data in db.Category.AsEnumerable()
                                            join lang in db.Languages.AsEnumerable()
                                            on data.LangId equals lang.Id
                                            select new CategoryWrapper()
                                            {
                                                CatId = data.CatId,
                                                CategoryName = data.CategoryName,
                                                LanguageName = lang.LanguageName
                                            }).ToList();

    Saturday, August 03, 2013 6:46 PM