public class Company { public int Id { get; set; } public string CompanyName { get; set; } public List Admins { get; set; } public List Employees { get; set; } public List Courses { get; set; } public string GetFullName() { return CompanyName; } } public class Employee { public int Id { get; set; } public string Forename { get; set; } public string Surname { get; set; } public int CompanyId { get; set; } [ForeignKey("CompanyId")] public Company company { get; set; } public ICollection Employeecourses { get; set; } } public class Admin { public int Id { get; set; } public string Forename { get; set; } public string Surname { get; set; } public int CompanyId { get; set; } [ForeignKey("CompanyId")] public Company Company { get; set; } }
[HttpGet] public dynamic Get() { var dbContext = new ApplicationContext(); var result = dbContext.Companies .Select(e => new { e.CompanyName, e.Id, e.Employees, e.Admins }) .ToList(); return result; }
EF Core无法进行延迟加载。 请参考这里 。
或者,您可以使用预先加载。
阅读这篇文章
下面是我为实现预先加载而创建的扩展方法。
扩展方法:
public static IQueryable IncludeMultiple( this IQueryable source, List>> navigationPropertyPath) where TEntity : class { foreach (var navExpression in navigationPropertyPath) { source= source.Include(navExpression); } return source.AsQueryable(); }