Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load Foreign key referenced tables data also in entity framework

I developed and entity framework (.edmx) application in 4.0 in that i got all the data of my querying table and its foreign key referenced tables data also. but when i change my project to 3.5 i am unable to get the data of foreign key referenced tables data. Please help me out...


2 Answers

In EF4 lazy loading is included and is on by default.

No such luck in prior versions: You may need to add an .Include() to fetch the other data automatically (eager loading) or call Load() on the references to load them (manually).

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

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

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

BTW: Do a search for "strongly typed Include" too - there are some extension methods people have written to remove the magic string and replace it with a compile time checked lambda expression.

like image 146
3 revs, 2 users 92%Ian Mercer Avatar answered Oct 25 '25 22:10

3 revs, 2 users 92%Ian Mercer


For the future answers if you are using newer version of EF;

 var o = db.Order.Include(i => i.User).Include(i => i.OrderItem).FirstOrDefault(x=>x.OrderId == orderId);
like image 20
Project Mayhem Avatar answered Oct 25 '25 21:10

Project Mayhem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!