简体   繁体   中英

The expression 'x.Taken' is invalid inside an 'Include' operation, since it does not represent a property access: 't => t.MyProperty'

class Taak:

   
        [Key]    
        public int TaakId { get; set; }
        public int Pid { get; private set; }
        [Required]
        [Range(0,23.5)]
        public double Uur { get; private set; }
        [Required]
        public Functie Functie { get; private set; }
        public List<Taak> Taken { get; private set; }
        [NotMapped]
        public ICollection<WerknemerTaak> Werknemers { get; set; }

class WerknemerTaak:

  [Required]
        public Werknemer Werknemer { get; set; } 
        [Required]
        public Taak Taak { get; set; }
        
        public Afdelingen Afdeling { get; set; }
        public string Taakbeschrijving { get; set; }

ReadAllTakenWthWerknemers:

  public List<Werknemer> ReadAllWerknemersWithTaken()
        {
            var taken = ctx.Werknemers
                .Include(x => x.Taken)
                .ThenInclude(x => x.Taak)
                .ToList();
            
          
            return taken;
        }

Program.cs

_mgr.GetAllWerknemersWithTaken().ForEach(x =>
                        {
                            Console.WriteLine(x.ToString());
                            Console.WriteLine("Waarvan de taken zijn:");
                            x.Taken.ToList().ForEach(t =>
                            {
                                Console.WriteLine("\t"+t.Taak.ToString());
                            });
                        });

The error that I am getting is as follows:

The expression 'x.Taken' is invalid inside an 'Include' operation, since it does not represent a property access: 't => t.MyProperty'.

I tried adding Where(t => t.Werknemer.Pid.Equals(t.Taak.Pid))), with no luck at all. I searched for others with same problem, But didn't find an answer for me. And I need to use eager loading.

Chetan is correct, you are getting your LINQ expression mixed up, and your entities are a bit intertwined as well that you should look to straighten out. Renamed to make it more readable you can see the issue:

var taken = ctx.Werknemers  // <- Query over Werknemers...
    .Include(werknemer => werknemer.Taken) // werknemer has no Taken collection...
    .ThenInclude(taak => taak.Taak)
    .ToList();

Changing the "x" term doesn't change the type, it just makes it more readable. When you expand x. with intellisense it will outline the properties of the type it is working with.

This expression is against Werknemer entities, not Taaks. Wernemers does not include the "Taken" property, Taak does.

This would work:

var taken = ctx.Taaks 
    .Include(x => x.Taken)

Or from Werknemers, this should work:

var taken = ctx.Werknemers  // <- Query over Werknemers...
    .Include(x => x.Taak)
    .ThenInclude(x => x.Taken) // <- ThenInclude dives into Taak

To make it a bit more readable as to what entity the expression is using:

var taken = ctx.Werknemers  // <- Query over Werknemers...
    .Include(werknemer => werknemer.Taak)
    .ThenInclude(taak => taak.Taken) 

This query would still be selecting Werknemers, where it implies you want the Taken Taaks. To get the Taaks, you would use SelectMany .

var taken = ctx.Werknemers  
    // Assuming you would have a `Where` clause to filter the Taken taaks from specific Werk/Taaks...
    .SelectMany(werknemer => werknemer.Taak.Taken);

To eager load properties of the Taak you would include Include etc. after the SelectMany based on the Taak navigation properties.

The next issue you will face is the self-referencing loop inside Taak with Taken. Taak will need something like a null-able ParentTaakId or such to facilitate this relationship. For the Taak that will be in the Taken collection, there will need to be a FK that points back to the parent Taak record. EF will need to be configured with that relationship.

[Key]    
public int TaakId { get; set; }
// ...
public int? ParentTaakId { get; set; }
public List<Taak> Taken { get; private set; }

Then when you set up the mapping either with modelBuilder or an EntityTypeConfiguration :

modelBuilder.Entity<Taak>()
    .HasMany(x => x.Taken)
    .WithOptional()
    .HasForeignKey(x => x.ParentTaakId);

Lastly, why is your Werknemers collection in Taak marked as [NotMapped] ? Since a Werknemer has a Taak reference, this mapping should be fine to configure within the entity. [NotMapped] values should be used sparingly, and generally avoided because the temptation will be to use them in a LINQ expression which would result in a runtime error.

So I got this error when converting from some Framework code to some .NET Core (EF Core). Turns out the code compiles fine with the old way:

myQueryable.Include(x => x.SubProp.SelectMany(y => y.SubSubProp));

So watch out for that, and instead convert it (as gleaned from other answers) to the new EF Core ThenInclude

myQueryable.Include(x => x.SubProp).ThenInclude(x => x.SubSubProp);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM