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.

Était-ce utile?

La 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);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top