Answer the question
In order to leave comments, you need to log in
How to optimize an Entity Framework query?
Good afternoon, I work with Entity Framework 6 and Asp.net MVC5, there are three entities: company, branch and user.
public class Company
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Branch> Branches { get; set; }
}
public class Branch
{
public int Id { get; set; }
public string Name { get; set; }
public int CompanyId { get; set; }
public virtual Company Company { get; set; }
public virtual ICollection<User> Users { get; set; }
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public int BranchId { get; set; }
public virtual Branch Branch { get; set; }
}
var user = UserManage.Users.Include(b=>b.Branch.Company).FirstOrDefault(u=>u.Id == id);
@Model.Branch.Company.Name
var user = UserManage.Users.Include(b=>b.Branch.Company).FirstOrDefault(u=>u.Id == id);
Answer the question
In order to leave comments, you need to log in
"Next, I pass the user to the view. And every time I access information about the branch or company in the view, for example:
@Model.Branch.Company.Name
, a query is made to the database. "
var user = UserManage.Users.Include(b= >b.Branch.Company).FirstOrDefault(u=>u.Id == id).ToList();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question