Pergunta

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.

Foi útil?

Solução

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);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top