【问题标题】:EF 3.x. eager loading many to many InvalidOperationException MVC英孚 3.x。急切加载多对多 InvalidOperationException MVC
【发布时间】:2021-02-16 21:25:47
【问题描述】:

我在 EF 5 中需要帮助。急切加载多对多 奇怪的是它曾在以前的版本中工作

投掷

InvalidOperationException:无法应用“包含”操作 论点'g => g.Attendees'。要么源不是可查询的 在“选择”方法之后应用了已知实体类型或“包含” 它通过导航投射不同的实体类型。考虑 在“选择”方法调用之前应用“包含”。

当然没有预先加载它会给出正常的 nullreferenceexception 并且编译没有错误

this used to work on previous EF less than 3.x

var userid = User.FindFirstValue(ClaimTypes.NameIdentifier);

var GigsIamAttending = _context.Attendances.Where(a => a.FollowerId == userid)

.Select(g => g.Gig)

.Include(g => g.Attendees)

.Include(g => g.Genre)

.ToList();

var viewmodel = new UpcommingGigsViewModel()

{

IsAuthorized = User.Identity.IsAuthenticated,

UpComingGigs = GigsIamAttending

};

return View( viewmodel);

【问题讨论】:

    标签: c# .net model-view-controller


    【解决方案1】:

    奇怪这对我有用,我不知道这是否会得到冗余连接!

     var GigsIamAttending = _context.Attendances.Where(a => a.FollowerId == userid)
                    .Include(a => a.Gig)
                    .ThenInclude(gig => gig.Artist)
                    .Include(a => a.Gig)
                    .ThenInclude(gig => gig.Genre)
                    .Select(a =>a.Gig);
    
                var viewmodel = new UpcommingGigsViewModel()
                {
                    IsAuthorized = User.Identity.IsAuthenticated,
                    UpComingGigs = GigsIamAttending
                };
                return View( viewmodel);
    

    【讨论】:

      猜你喜欢
      • 2015-05-13
      • 1970-01-01
      • 2013-04-13
      • 1970-01-01
      • 1970-01-01
      • 2021-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多