简体   繁体   English

实体框架:如何从相关表中获取数据(很多)

[英]Entity Framework: How to get data from related table (many to many)

I have two tables 我有两张桌子

LOCATIONS:
Id
Name
OwnerId (FK to Owners table)

PARAMTERS:
Id
LocationId
TypeId
Name
Value

The data is something like: 数据类似于:

LOCATIONS
1,'Park',100
2,'Shop',200
PARAMETERS
1,1,'Length',5,200
2,1,'Width',5,100
3,2,'Area',6,100

I want to retrieve the data for all locations that will include all parameters for each location. 我想检索所有位置的数据,这些数据将包括每个位置的所有参数。

I have a query that doesn't work. 我有一个行不通的查询。

_locationsRepository.GetAll()
      .Include(x => x.Owner)
      .Include(x => x.Parameters)
      .ToList();

Running this without including Parameters works fine. 在不包含参数的情况下运行此程序效果很好。 There is FK(to Locations table) in Parameters table. 参数表中有FK(到位置表)。

Am I missing something? 我想念什么吗? Would be glad if anyone could help. 如果有人可以帮助,我们将非常高兴。

You might want to try and add something like: 您可能想要尝试添加以下内容:

[InverseProperty("Location")]
public virtual ICollection<Parameters> Parameters { get; set; }

to your Locations table and add 到“位置”表并添加

[ForeignKey("LocationId")]
public virtual <Location> Location { get; set; }

to the Parameters table to allow for reverse lookup and help EF generate the foreign key correctly (assuming that you are using CodeFirst of course). 到Parameters表以允许反向查找并帮助EF正确生成外键(当然,假设您使用的是CodeFirst)。

Both of the tags live inside 这两个标签都位于内部

System.ComponentModel.DataAnnotations;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架多对多 - 无法获取数据 - Entity Framework Many to Many - Cannot get data 如何使用Entity Framework从具有一对多关系的两个表中选择所有相关数据? - How to select all related data from two tables having one-to-many relationship using Entity Framework? 使用Entity Framework Core查询多对多相关数据 - Query many-to-many related data using Entity Framework Core 实体框架 ASP.NET MVC:在多对多场景中从相关表中查找最后一条记录 - Entity Framework ASP.NET MVC : find last record from related table in a many to many scenario 如何从 Entity Framework Core 构建的多对多关系中获取数据? - How to get data from many-to-many relationship builded by Entity Framework Core? Entity Framework 6,从具有许多指向其他表的链接的大表中获取过滤数据的最佳策略 - Entity Framework 6, best strategy to get filtered data from big table with many links to other tables 如何从表中添加实体多对多 - How add entity from table many to many Linq实体框架 - 让所有客户看到ID不在很多表中 - Linq Entity Framework - get all customers that Ids are not in many to many table 实体框架EF6将数据添加到多对多关系表 - Entity Framework EF6 Add data to Many to Many relationship Table 实体框架多对多关系将数据留在联接表中 - Entity Framework Many to Many relationship leaves data in joining table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM