What is the difference between
ObjectQuery<SalesOrderHeader> query =
context.Contacts.Include("SalesOrderHeaders").Include("SalesOrderDetails");
and
Contact contact =
context.Contacts.Include("SalesOrderHeader开发者_如何学JAVAs.SalesOrderDetails").FirstOrDefault();
Any advantage of using one over the other?
My main confusion is for the using 2 Include
in first one and using 2 tables in second Include
.
Thanks in advance.
On entity framework you have something called navigation properties (Named on your .edmx model) so you can access related entities based on them, in other words, you can use your navigation properties to eager load (include) related data..
I'm guessing you have this relationship: One Contact can have many SalesOrderHeaders and one SalesOrderHeader can have many SalesOrderDetails, so if you start loading from contacts entity and want to have both relations loaded you should do:
Contact contact = context.Contacts.Include("SalesOrderHeaders").Include("SalesOrderHeaders.SalesOrderDetails").FirstOrDefault();
as you can see, we're using first include to bring SalesOrderHeaders from DB and the secound one to bring SalesOrderDetails through SalesOrderHeaders navigation property as we're starting from Contacts
精彩评论