Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity framework autogenerated table names

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?

like image 468
Carlo V. Dango Avatar asked Mar 19 '11 16:03

Carlo V. Dango


2 Answers

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.

like image 105
Ladislav Mrnka Avatar answered Oct 03 '22 16:10

Ladislav Mrnka


IMO GetType might help you other than .edmx file where all the definitions is stored,

 context.Orders.Include(CustomerEntity.GetType.Name or full name )
like image 27
indiPy Avatar answered Oct 03 '22 14:10

indiPy