Problem with Eager Loading Nested Navigation Based on Abstract Entity (EF CTP5)

前端 未结 1 1949
星月不相逢
星月不相逢 2020-12-15 14:03

I a portion of my EF model that looks like this:

\"enter

Summary:

1条回答
  •  情歌与酒
    2020-12-15 14:37

    The new Query method might help you:

    var location = context.Locations.SingleOrDefault();
    
    context.Entry(location)
           .Collection(l => l.Posts)
           .Query()
           .OfType()
           .Load();
    


    Repository Implementation:

    We can add a new LoadProperty generic method to the Repository class that leverages this new QUery method:

    public void LoadProperty(T entity, 
            Expression>> navigationProperty,
            Expression> predicate) where TElement : class
    {
        _context.Set().Attach(entity);
    
        _context.Entry(entity)         
                .Collection(navigationProperty)
                .Query()
                .Where(predicate)
                .Load();
    }
    

    Using the LoadProperty method:

    Location location = _locationRepository.Find(1);
    _locationRepository.LoadProperty(location, l => l.Posts, p => p is Discussion);
    

    0 讨论(0)
提交回复
热议问题