我遇到EF7反向属性的问题。有两个实体像这样连接。
public class Employee
{
public int Id { get; set; }
}
public class Review
{
public int Id { get; set; }
[Required]
public virtual Employee Employee { get; set; }
[Required]
public Employee Manager { get; set; }
}
我想在开始查询员工时访问评论列表,所以我尝试这样做:
public class Employee
{
public Employee()
{
Reviews = new List<Review>();
}
public int Id { get; set; }
[InverseProperty("Employee")]
public virtual ICollection<Review> Reviews { get; set; }
}
这样,查询效果不佳并返回此错误:
Invalid column name 'EmployeeId1'
。
这是查询的一部分,其中包含错误:
SELECT [ua].[Id], [r].[EmployeeId], [r].[EmployeeId1], [r1].[EmployeeId], [r1].[EmployeeId1]
FROM [UserAssessment] AS [ua]
LEFT JOIN [Review] AS [r] ON [ua].[ReviewId] = [r].[Id]
LEFT JOIN [Review] AS [r1] ON [ua].[ReviewId] = [r1].[Id]
任何人都知道我该怎么做?
更新
此语句正在生成查询:
return this.DbSet
.Include(ua => ua.Employee).ThenInclude(t => t.Role)
.Include(ua => ua.Review).ThenInclude(rt => rt.ReviewType)
.Include(ua => ua.Review).ThenInclude(rt => rt.Manager).ThenInclude(r => r.Role)
我必须使用相同的includes
访问,因为EF7上还没有延迟加载。
答案 0 :(得分:0)
您需要在员工和审核
上使用 InversePropertypublic class Review
{
public int Id { get; set; }
[Required]
[InverseProperty("Reviews")]
public Employee Employee { get; set; }
[Required]
public Employee Manager { get; set; }
}
public class Employee
{
public int Id { get; set; }
[InverseProperty("Employee")]
public ICollection<Review> Reviews { get; set; }
}
应该有效。我有一个类似的设置,它创建导航而不创建任何新的字段。如果这不起作用,请告诉我,我将启动一个测试项目。
另请注意,EF7当前忽略虚拟,这与EF6中的含义不同。