Considering a table [Person]
which has two foreign keys ([Phone_Numbers]
, [Business_Information]
). When using EF Core, we can simply find a Person
using the dbContext.Find
method like var person = await db.Context.FindAsync<Person>(1)
; however, the Find
method only looks for the entity inside the tracking cache and it does not handle relational properties. In order to solve this, we can call the Entry
method to attach those properties like dbContext.Entry<Person>(person).Reference(x=> x.Business_Information)
. Considering the provided example, we have to call the Entry
method twice in this case:
dbContext.Entry<Person>(person).Reference(x=> x.Business_Information).Load();
dbContext.Entry<Person>(person).Collection(x=> x.Phone_Numbers).Load();
An alternative solution is to use the Include
method:
var person = await dbContext.Set<Person>().Include("Business_Information").Include("Phone_Numbers").FirstOrDefaultAsync(x=> x.id == id);
The first solution sends two request to the Db (I think the Find
method does not send a request if the entity is being tracked); however, I'm not sure how the second one works and accordingly I'm also unsure if it has any performance advantages. I've been thinking the first solution could be faster and more efficient. I'd appreciate if someone clarifies this for me.