微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 查询的结果类型既不是EntityType,也不是具有实体元素类型的CollectionType

var myQuery = from product in _repository.Query()
                      join prodLocalization in _repoProductLocalization.Query()
                      on product.Id equals prodLocalization.ProductId
                      select new { Product = product,Localization = prodLocalization };
myQuery = myQuery.Include(x => x.Product.Customer);
var prods = myQuery.ToList();

最后一行抛出:

An exception of type ‘system.invalidOperationException’ occurred in
EntityFramework.sqlServer.dll but was not handled in user code

Additional @R_430_4045@ion: The result type of the query is neither an
EntityType nor a CollectionType with an entity element type. An
Include path can only be specified for a query with one of these
result types.

我已经设法找到很少甚至没有解释为什么会发生这种情况.有帮助吗?

解决方法

您的课程在产品和本地化之间是否存在物理关系?如果他们这样做,你不应该使用join.此外,您必须在选择之前调用include.

试试这个:

var myQuery = from product in _repository.Query()
                  .Include(x => x.Product.Customer)
                  .Include(x => x.Product.Localization)
              select new 
              { 
                 Product = product,Localization = product.Localization 
              };

var prods = myQuery.ToList();

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐