Using Entity Framework one often writes queries such as
var orders = from o in context.Orders.Include("Customer")
where o.OrderDate.HasValue && o.OrderDate.Value.Year == 1997
orderby o.Freight
select o;
What really makes my stomach churn is the "Customer"
string argument. I have a hard time believing that EF does not generate table names as constants somewhere. Does anyone know a better approach than to using a string? for the Include
fetch option?
EF 4.1 has strongly typed version of Include usable for IQueryable, ObjectQuery and DbQuery. Once you add reference to EntityFramework.dll (EF 4.1) you can add using System.Data.Entity and use eager loading with lambda expressions
// get Orders with related Customers
var orders = from o in context.Orders.Include(o => o.Customer) ...
Edit:
If you don't want to use EF 4.1 check this article. I already used in my project and I'm happy with it.
IMO GetType might help you other than .edmx file where all the definitions is stored,
context.Orders.Include(CustomerEntity.GetType.Name or full name )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With