简体   繁体   English

InverseProperty实体框架7

[英]InverseProperty Entity Framework 7

I am facing a problem with EF7 inverse property. 我面临EF7逆属性的问题。 There are two entities that are connected like this. 有两个这样连接的实体。

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; }
 }

I want to access a list of the reviews when I start to query my employees, so I tried to do this: 当我开始查询员工时,我想访问评论列表,因此我尝试执行以下操作:

public class Employee
    {
        public Employee()
        {
            Reviews = new List<Review>();
        }
        public int Id { get; set; }

        [InverseProperty("Employee")]
        public virtual ICollection<Review> Reviews { get; set; }
    }

With this, the query is not well made and return this error: 这样,查询就做得不好,并返回此错误:

Invalid column name 'EmployeeId1' . Invalid column name 'EmployeeId1'

This is the part of the query where is the error: 这是查询的一部分,其中错误是:

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]

Anyone know what can I do? 有人知道我该怎么办?

UPDATE 更新

This statement is generating the query: 该语句生成查询:

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)

I have to access with those same includes because lazy loading is not available on EF7 yet. 我必须访问那些includes因为在EF7上尚无法进行延迟加载。

You need the InverseProperty on both the Employee and Review 您需要在Employee和Review上都具有InverseProperty

public 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; }
    }

Should work. 应该管用。 I have a similar setup where it creates the navigation without creating any new fields. 我有一个类似的设置,它在不创建任何新字段的情况下创建导航。 If this doesn't work let me know and I'll spin up a test project. 如果这不起作用,请告诉我,然后我将启动一个测试项目。

Also note, that EF7 currently ignores virtual and this does not have meaning like it did in EF6. 还要注意,EF7当前忽略虚拟 ,并且没有像EF6那样的含义。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM