How to load data from tables with foreign keys in entity framework

asp.net dynamic-data entity-framework linq-to-entities

Question

I created an entity framework (.edmx) application in version 4.0 and used it to access all the data from my querying table and any tables that it had foreign key references to. However, when I switch my project to 3.5, I am unable to access the data from tables with foreign key references. Please assist me...

1
2
5/3/2010 5:46:44 AM

Accepted Answer

Lazy loading is present and enabled by default in EF4.

Not so in earlier iterations: You may have to add an. Call Include() to automatically load additional material (eager loading) or Load() to load references (manually).

If the reference table was say "Details" you would do ...

var featuredOffers = context.Hosters_FeaturedOffer.Include("Details").ToList();

look at http://msdn.microsoft.com/en-us/library/bb896272.aspx

By the way, if you search for "strongly typed Include," you may find extension methods that allow you to replace the magic text with a lambda expression that has been verified at compilation time.

5
2/25/2014 11:13:23 AM

Popular Answer

Using a more recent version of EF for future responses;

 var o = db.Order.Include(i => i.User).Include(i => i.OrderItem).FirstOrDefault(x=>x.OrderId == orderId);


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow