Dzefo
Dzefo

Reputation: 113

Lambda expression used inside Include is not valid. Include not working

I am trying to include two relationships with the Include() method.

using (var db = new DBContext()) {
        var result = db.ProduktKundeArtNummer
        .Include(p => p.Kunde)
        .Include(d => d.Produkt)
        .ToList();
        }
    }

When I execute this snippet I get the following error message:

Lambda expression used inside Include is not valid.

I created my context by scaffolding my mysql database.

This is the class it is referring to:

  public partial class ProduktKundeArtNummer {
    [Key]
    public int Produkt { get; set; }
    [Key]
    public int Kunde { get; set; }
    [Required]
    [StringLength(255)]
    public string Artikelnummer { get; set; }

    [ForeignKey(nameof(Kunde))]
    [InverseProperty("ProduktKundeArtNummer")]
    public virtual Kunde KundeNavigation { get; set; }
    [ForeignKey(nameof(Produkt))]
    [InverseProperty("ProduktKundeArtNummer")]
    public virtual Produkt ProduktNavigation { get; set; }
  }

I already googled a lot and as far as I know my snippet should work. Does anyone know where I messed up?

Edit: Using a string navigator works fine, but I would rather use the lambda expressions.

using (var db = new DBContext()) {
        var result = db.ProduktKundeArtNummer
        .Include("KundeNavigation")
        .Include("ProduktNavigation")
        .ToList();
        }
    }

Upvotes: 1

Views: 641

Answers (1)

Svyatoslav Danyliv
Svyatoslav Danyliv

Reputation: 27282

You are trying to Include integer properties which are not a Navigation properties. Include is a declaration which navigation properties should be loaded with entity.

Probably you need this:

using (var db = new DBContext()) {
   var result = db.ProduktKundeArtNummer
      .Include(p => p.KundeNavigation)
      .Include(d => d.ProduktNavigation)
      .ToList();
}

Upvotes: 1

Related Questions