Question

For example:

 var emp = db.Employees
           .Include("CostCenter")
           .Single(e => e.Id == 123);

I do not like the idea of using magic strings to include navigation properties in my query, is there a more neat way of doing this? I am asking this because recently I changed a navigation property's name and I had to change this manually, it is just doesn't look nice.

Was it helpful?

Solution

Yes, you can try to use lambda expressions. First of all include appropriate extensions in your project:

System.Data.Entity.DbExtensions 

Then add appropriate namespace:

using System.Data.Entity; 

Now you can start using this:

var emp = db.Employees
           .Include(x => x.CostCenter)
           .Single(e => e.Id == 123);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top