c# - 如何从依赖于实体框架核心中另一个表的表中获取记录?
问题描述
我想从依赖于三个表的数据库中获取一些记录。三张表是:
1.Company(Id,Name)
2. Car(Id,CompanyId,Name)
3. Showroom(Id,CarId,Name)
现在一家公司包含许多汽车,许多陈列室中可能存在许多汽车。我想从公司'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;}
}
陈列室:
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();
推荐阅读
- html - “如何在 HTML 和 CSS 中修复‘css 编辑后按钮不移动’”
- css - 如何使网格区域始终为页面的 100% 宽度?
- python - 使用 Python 在 Azure SQL 上启用 TDE
- hive - 在 Oracle 中是否有与转储函数等效的 Hive?
- c - 无法在 If 语句中检索或存储二维数组位置
- css - 如何在 Vuetify 中更改文本字段的宽度和高度?
- c# - Unity:使用两个摄像头对单个图层进行灰度化也会对天空盒进行灰度化?
- c - 字节顺序和位移问题
- javascript - 在一个 td 中显示数组项目,每个项目在新行上
- tensorflow - 函数 model.fit() 中的错误不兼容形状