[英]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.