开发者

difference between 2 code segments

开发者 https://www.devze.com 2023-04-04 02:22 出处:网络
What is the difference between ObjectQuery<SalesOrderHeader> query = context.Contacts.Include(\"SalesOrderHeaders\").Include(\"SalesOrderDetails\");

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

0

精彩评论

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