1

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();
        }
    }
1
  • 1
    You need to include navigation property (KundeNavigation), not base one. Commented Nov 11, 2020 at 11:45

1 Answer 1

1

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();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Well... You´re the king of the day

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.