Share via


The entity cannot be constructed in a LINQ to Entities query

Question

Monday, June 11, 2018 8:36 AM

It is a simple left outer join.
Why is the following query an error?
Does it need a DTO?

var test = __dbContext.Parents
           .GroupJoin(__dbContext.Children,
               p => p.Id,
               c => c.ParentId,
               (p, c) => new Parent
               {
                   Id = p.Id,
                   Name = p.Name,
                   Children = c.ToList()
               }
           }).ToList();

The code below throws the following error :
The entity or complex type 'PrimalDoctor.DbContext.Parent' cannot be constructed in a LINQ to Entities query.

public class MyDbContext : DbContext
{
    public DbSet<Parent> Parents { get; set; }

    public DbSet<Child> Children { get; set; }
}

public class Parent
{
    public int Id { get; set; }

    public string Name { get; set; }

    public virtual ICollection<Child> Children { get; set; }
}

public class Child
{
    public int Id { get; set; }

    public int ParentId { get; set; }

    public string Name { get; set; }

    public virtual Parent Parent { get; set; }
}

All replies (4)

Monday, June 11, 2018 11:11 AM âś…Answered | 1 vote

You can keep all your code, but at some point, you has to set the state of the parent in order for EF to know what to do.

The example code shows a parent with its child collection being persisted to the database. 

I think you should follow the concepts in the link in persisting a parent and its child collection.

http://www.entityframeworktutorial.net/EntityFramework5/attach-disconnected-entity-graph.aspx


Monday, June 11, 2018 8:55 AM | 1 vote

From what I read about this,  EF doesn't allow you to project the results of a query onto a mapped entity.
You can use a custom object like a DTO that doesn't inherit from the mapped entity.

EF issues can be discussed at the EF forum.

https://social.msdn.microsoft.com/Forums/en-US/home?forum=adodotnetentityframework


Monday, June 11, 2018 10:25 AM

Thank you for your early reply.
For example, will it be this following source if I try to update the data which I acquired?
generally to say, do we acquire "1. DTO parse loop" with LINQ?
I'm afraid of asking this elementary question, but I 'm so appreciate that you will tell me about it in advance.

var test = __dbContext.Parents
           .GroupJoin(__dbContext.Children,
               p => p.Id,
               c => c.ParentId,
               (p, c) => new ParentDTO
               {
                   Id = p.Id,
                   Name = p.Name,
                   ChildrenDTO = c
               }
           }).ToList();

List<Parent> parents = new List<Parent>();

// 1. DTO parse loop
foreach (var parent in test)
{
    Parent p = new Parent({
        Id = parent.Id,
        Name = parent.Name
    });

    List<Child> children = new List<Child>();

    foreach (var child in parent.ChildrenDTO)
    {
        children.Add(new Child({
            Id = child.Id,
            ParentId = child.ParentId,
            Name = child.Name
        }));
    }

    p.Children = children;

    parents.Add(p);
}

__dbContext.Parents.Attach(parents);

// 2. Data update
foreach (var parent in parents)
{
    parent.Name = "Patient A";

    foreach (var child in parent.Children)
    {
        child.Name = "Child A";
    }
}

__dbContext.SaveChanges();
public class Parent
{
    public int Id { get; set; }

    public string Name { get; set; }

    public virtual ICollection<Child> Children { get; set; }
}

public class Child
{
    public int Id { get; set; }

    public int ParentId { get; set; }

    public string Name { get; set; }

    public virtual Parent Parent { get; set; }
}

public class ParentDTO
{
    public int Id { get; set; }

    public string Name { get; set; }

    public virtual IEnumerable<Child> Children { get; set; }
}

public class ChildDTO
{
    public int Id { get; set; }

    public int ParentId { get; set; }

    public string Name { get; set; }

    public virtual Parent Parent { get; set; }
}

Monday, June 11, 2018 11:30 AM

>http://www.entityframeworktutorial.net/EntityFramework5/attach-disconnected-entity-graph.aspx

I will check it again.
Thank you for your help.

Arigatou gozaimasu.