2016-07-27 101 views
1

我正在使用实体框架核心。我正在尝试创建一个产品列表,并包含相关评论和评论作者数据。实体框架:包含多个属性级别时出现InvalidOperationException

我有3个实体:

public class Product 
{ 
    [Key] 
    public int ID { get; set; } 
    public ICollection<Review> Reviews {get; set;} 
} 

public class Review 
{ 
     [Key] 
     public int ID { get; set; } 
     public Product Product {get; set;} 
     public Customer Author { get; set; } 
} 

public class Customer 
{ 
     [Key] 
     public int ID { get; set; } 
     public ICollection<Review> Reviews { get; set; } 
} 

时,指的this answer,我要求的产品评论和作者列表:

context.Products.Include(p=> p.Reviews.Select(r => r.Author)).ToList(); 

以下错误被抛出:

System.InvalidOperationException
Message =属性表达式'p => {来自[p]中的评论r。评论select [r] .Author}'无效。该表达式应该代表属性访问:'t => t.MyProperty'

任何意见将不胜感激。

+0

你有任何流利的映射? –

+0

我没有流利的映射。 –

回答

1

这似乎是一些还没有完成或丢失的,但无论如何,你可以做到这样的:

context.Products.Include(p => p.Reviews).ThenInclude(x=>x.Author).ToList(); 
+0

谢谢@Bassam :)但是,在您的示例中,'x'的类型是ICollection ,而不是Review。所以我不认为这有效。 –

+0

@StevePaul这是像你的查询exaclty,它正在工作!试试吧^^ –

+1

我的apoligies - 你是对的,我错了!出于某种原因,这是抛出编译错误。我重新编译了我的模型类,并且错误消失了!我很困惑。但是,谢谢你的时间:) –