开发者

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 开发者_Go百科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 )


How about

Include(context.Customers.EntitySet.Name)

?


You can create a Text Template which will allow you to generate the code in addition to EF's default code. You can do this by right click and clicking "Add Code Generation Item".

In this text template, you can create your constants as you need in "CustomerProperties" and create constant name for each navigation property.

http://msdn.microsoft.com/en-us/data/gg558520

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜