如何从依赖于实体框架核心中另一个表的表中获取记录?

时间:2019-03-07 07:03:50

标签: c# entity-framework entity-framework-core

我想从数据库中获取一些依赖于三个表的记录。 三个表是:

window.location.assign

现在,一家公司拥有许多汽车,许多展厅中可能存在许多汽车。 我想从陈列有公司“ 2”辆汽车和汽车的展厅表中获取记录。是否可以在实体框架核心中做到这一点?

1 个答案:

答案 0 :(得分:2)

我认为您的实体会像:

公司

 public class Company
 {
    public int Id{get; set;}
    public string Name {get; set;}

    public ICollection<Car> Cars {get; set;}
  }

汽车:

public class Car
   {
        public int Id{get; set;}
        public string Name {get; set;}
        public int CompanyId{get; set;}
        public Company Company {get; set;}

      }

ShowRoom:

public class ShowRoom
     {
        public int Id{get; set;}
        public string Name {get; set;}
        public int CarId{get; set;}
        public Car Car{get; set;}

      }

使用您的方法:

var context = new SomeContext();



 var showRooms= context.ShowRooms
                    .Include(x=> x.Car)
                    .ThenInclude(x=> x.Company)
                    .Where(x=> x.Car.Company.Id== 2)
                    .ToList();