0

I have two functions that look exactly the same except they create lists of two different objects. The two different objects look very much alike, but when I try to run one of the functions on one of the objects, I get the error message, "The entity or complex type cannot be constructed in a LINQ to Entities query.". Can someone explain to me what is happening in very simple terms? Also, can you tell me how to change my code so that it works? Thanks, Allan.

Function 1 (works):

    public static List<ChartApp> ListChartApplications()
    {
        using (var db = new LatencyDBContext())
        {
            var appNames = db.LoginApplications.Select(item => new ChartApp()
            {
                LoginApplicationID = item.LoginApplicationID,
                LoginAppName = item.LoginAppName,
            }).OrderBy(item => item.LoginAppName);
            return appNames.ToList();
        }
    }

Function 2 (throws error on "return appNames.ToList();"):

    public static List<LoginApplication> ListApplications()
    {
        using (var db = new LatencyDBContext())
        {
            var appNames = db.LoginApplications.Select(item => new LoginApplication()
            {
                LoginApplicationID = item.LoginApplicationID,
                LoginAppName = item.LoginAppName,
            }).OrderBy(item => item.LoginAppName);
            return appNames.ToList();
        }
    }

Classes:

public class ChartApp
{
    public ChartApp()
    {
        this.LoginHistories = new List<ChartHist>();
    }

    public int? LoginApplicationID { get; set; }
    public string LoginAppName { get; set; }
    public virtual ICollection<ChartHist> LoginHistories { get; set; }
    public int Step { get; set; }
}

public class LoginApplication
{
    public LoginApplication()
    {
        this.LoginHistories = new List<LoginHistory>();
    }

    public int LoginApplicationID { get; set; }
    public string LoginAppName { get; set; }
    public virtual ICollection<LoginHistory> LoginHistories { get; set; }
}

Edit: Could the difference possibly be that one of the objects are mapped to the database?

public class LoginApplicationMap : EntityTypeConfiguration<LoginApplication>
{
    public LoginApplicationMap()
    {
        // Primary Key
        this.HasKey(t => t.LoginApplicationID);

        // Properties
        this.Property(t => t.LoginAppName)
            .HasMaxLength(500);

        // Table & Column Mappings
        this.ToTable("LoginApplication");
        this.Property(t => t.LoginApplicationID).HasColumnName("LoginApplicationID");
        this.Property(t => t.LoginAppName).HasColumnName("LoginAppName");
    }
}
ADH
  • 2,971
  • 6
  • 34
  • 53
  • Is one of your classes a mapped entity, while the other is not? Take a look at this question: http://stackoverflow.com/questions/5325797/the-entity-cannot-be-constructed-in-a-linq-to-entities-query/5325861#5325861 – Yakimych Feb 15 '13 at 20:08
  • Yes, one is mapped and the other is not. I will take a look at your link. – ADH Feb 15 '13 at 20:45
  • Okay, that explains the error. So, what is the best way to fix the error? How can I change my function to not throw the error anymore? – ADH Feb 15 '13 at 20:50
  • As in the referenced answer - don't project onto a mapped entity, but rather onto a different object (anonymous class or DTO). – Yakimych Feb 15 '13 at 20:52
  • @Yakimych : please move from comment to answer so it can be marked accordingly . You are right, You shouldnt project onto a type used in DbSet in Context. – phil soady Feb 17 '13 at 15:02
  • @soadyp - if the problem is solved, the OP should mark the question as a duplicate, comment accordingly and close it. Copy-pasting the answer from the referenced question does not make much sense (and is certainly not encouraged by the SO community). – Yakimych Feb 19 '13 at 07:35
  • @Yakimych. Fair comment. And it sounds like we both want questions closed in an orderly manner so as to better serve the community. Are you going to mark it as duplicate? Will you Enter an answer with one line explanation and reference another post? Or will your comment remain a comment? Had it been marked as dup or solved I wouldnt have been here. You answered the question. I agree with your comment...Your move... – phil soady Feb 19 '13 at 11:29

1 Answers1

1

My solution in this case was to just delete the non-working function and use the working one in all places. For, similar functions that are mapped, I use the following function to return values.

    public static List<LoginEnvironment> ListEnvironments(bool allSelection)
    {
        using (var db = new LatencyDBContext())
        {
            //GET ALL THE ENVIRONMENT NAMES              
            var envNames = from e in db.LoginEnvironments
                           orderby e.LoginEnvName
                           select e;

            //PUT ALL THE ENVIRONMENTS INTO A LOCAL LIST
            var listEnv = new List<LoginEnvironment>();

            if (allSelection)
            {
                var defaultAll = new LoginEnvironment();
                defaultAll.LoginEnvironmentID = 0;
                defaultAll.LoginEnvName = "All";
                listEnv.Add(defaultAll);
            }

            foreach (var item in envNames)
            {
                var localEnv = new LoginEnvironment();
                localEnv.LoginEnvironmentID = item.LoginEnvironmentID;
                localEnv.LoginEnvName = item.LoginEnvName;
                listEnv.Add(localEnv);
            }

            return listEnv;
        }
    }
ADH
  • 2,971
  • 6
  • 34
  • 53