Efcore: The given expression ... does not contain the searched expression '[x]' in a nested NewExpression with member assignments or a MemberBindingExpression.

Created on 16 Mar 2016  路  7Comments  路  Source: dotnet/efcore

I have the following entities:

public class Message {    
  public Int32 Id { get; set; }
  public Int32 AuthorId { get; set; }
  public virtual User Author { get; set; }
}

public class User : IdentityUser<Int32> {
  public String Name { get; set; }
  public virtual ICollection<Message> Messages { get; set; } = new List<Message>();
}

And the configuration is:

public class Context : IdentityDbContext<User, Role, Int32> {
  protected override void OnModelCreating(ModelBuilder builder) {
    base.OnModelCreating(builder);
    builder.Entity<Message>(b => {
      b.ToTable("Messages");
      b.HasKey(x => x.Id);
      b.Property(x => x.Content);
      b.HasOne(x => x.Author).WithMany(x => x.Messages).HasForeignKey(x => x.AuthorId);
    }
  }
}

When I execute the following query with projection:

  var result = await _context.Messages
    .Include(x => x.Author)
    .Select(x =>
      new MessageModel {
        Author = new UserModel {
          Id = x.Author.Id,
          Name = x.Author.Name
        }
      })
     .ToListAsync();

I get the following error:

The given expression 'new MessageModel() {Author = new UserModel() {Id = Property([x], "AuthorId"), Name = [x.Author].Name}}' does not contain the searched expression '[x]' in a nested NewExpression with member assignments or a MemberBindingExpression.
Parameter name: fullExpression

The previous query works if I use:

 Id = x.AuthorId   (so using the FK in Message entity)
type-bug

Most helpful comment

I had the same error on version 1.1.0, using :
_context.Entities.Select(x => new MyViewModel(x)).ToListAsync()

I got it working using :
_context.Entities.AsNoTracking().Select(x => new MyViewModel(x)).ToListAsync()

MyViewModel being a simple POCO object with properties assignation.

All 7 comments

This works in the current bits, producing the following sql:

SELECT [x].[AuthorId], [x.Author].[Name]
FROM [Messages] AS [x]
INNER JOIN [Users] AS [x.Author] ON [x].[AuthorId] = [x.Author].[Id]

I had the same error on version 1.1.0, using :
_context.Entities.Select(x => new MyViewModel(x)).ToListAsync()

I got it working using :
_context.Entities.AsNoTracking().Select(x => new MyViewModel(x)).ToListAsync()

MyViewModel being a simple POCO object with properties assignation.

@adrien-constant can you file a new issue on this? (please provide query and the model that you used). This error shows up every time there is a bug in our tracking logic (that's why AsNoTracking makes it go away)

@maumar Same issue as @adrien-constant . Using "Microsoft.EntityFrameworkCore": "1.1.0",

AsNoTracking() prevents Exception.

 records = _context
                    .Footprints
                        .Include(x => x.Enrollment)
                        .Where(x =>
                            x.CourseId == courseId
                            && x.Actor == forUser
                            && x.Enrollment.PortalId == portalId
                            && x.Enrollment.Disposition != (int)FootprintDisposition.Completed
                            && x.Enrollment.Disposition != (int)FootprintDisposition.Dropped
                            )
                        .OrderByDescending(x => x.LastUpdate)
                        .AsNoTracking() // EF bug.  This query hangs without AsNoTracking
                        .Select(x => new DTO_FootprintMasterRecord(x))
                        .ToList();

BY adding "AsNoTracking()", the query can be seen as

 SELECT [x].[ID], [x].[Actor], [x].[CourseId], [x].[CourseVersion], [x].[Disposition], [x].[EnrollmentId], [x].[ExecutionPlan], [x].[Footprints], [x].[GlobalDat], [x].[LastSceneDisplayed], [x].[LastUpdate], [x].[RecordFinalized], [x.Enrollment].[ID], [x.Enrollment].[AuthorizedBy], [x.Enrollment].[CourseId], [x.Enrollment].[Disposition], [x.Enrollment].[EnrolledOn], [x.Enrollment].[PortalId], [x.Enrollment].[Reason], [x.Enrollment].[StudentId], [x.Enrollment].[TransactionId], [i].[ID], [i].[AuthorizedBy], [i].[CourseId], [i].[Disposition], [i].[EnrolledOn], [i].[PortalId], [i].[Reason], [i].[StudentId], [i].[TransactionId]
 FROM [Footprints] AS [x]

LEFT JOIN [EnrollmentPreviews] AS [x.Enrollment] ON [x].[EnrollmentId] = [x.Enrollment].[ID]

 LEFT JOIN [EnrollmentPreviews] AS [i] ON [x].[EnrollmentId] = [i].[ID]

 WHERE (((([x].[CourseId] = @__courseId_0) AND ([x].[Actor] = @__forUser_1)) AND (([x.Enrollment].[PortalId] = @__portalId_2) AND [x.Enrollment].[PortalId] IS NOT NULL)) AND (([x.Enrollment].[Disposition] <> 2) OR [x.Enrollment].[Disposition] IS NULL)) AND (([x.Enrollment].[Disposition] <> 3) OR [x.Enrollment].[Disposition] IS NULL)
 ORDER BY [x].[LastUpdate] DESC, [x].[EnrollmentId]

Model looks as such:

  [Table("Footprints")]
public partial class Footprints
{
    [Column("ID")]
    public long Id { get; set; }
    public long CourseId { get; set; }
    [Required]
    [MaxLength(450)]
    public string Actor { get; set; }
    public Guid CourseVersion { get; set; }
    public string Footprints { get; set; }
    public bool RecordFinalized { get; set; }
    [Column(TypeName = "datetime")]
    public DateTime LastUpdate { get; set; }
    public Guid? LastSceneDisplayed { get; set; }
    public long? EnrollmentId { get; set; }
    public int? Disposition { get; set; }

    [ForeignKey("Actor")]
    [InverseProperty("Footprints")]
    public virtual ApplicationUser ActorNavigation { get; set; }
    [ForeignKey("CourseId")]
    [InverseProperty("Footprints")]
    public virtual Courses Course { get; set; }

    public string ExecutionPlan { get; set; }

    public string GlobalDat { get; set; }

    [ForeignKey("EnrollmentId")]
    [InverseProperty("Footprints")]
    public virtual Enrollment Enrollment { get; set; }
}

The Exception thrown is

 InnerException = {"The given expression 'new DTO_FootprintMasterRecord([x])' does not contain the searched expression '[x]' in a nested NewExpression with member assignments or a MemberBindingExpression.\r\nParameter name: fullExpression"}

I have the same issue. is here an update to that beside using AsNoTracking?

@NPadrutt Can you please create a new issue for this, including a full code listing or project that reproduces what you are seeing?

@ajcvickers sure, if it helps. Although I can't provide more than @SimonOrdo already did.

Was this page helpful?
0 / 5 - 0 ratings