question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Option to not load the navigation property

See original GitHub issue

Do we already have this feature?

If no, I think this is necessery to make sure that Include statement is called or not in my repository or query. The sample scenario:

public class Lesson {
  public int Id {get; set;}
}

public class Student {
  public int Id {get; set;}

  public List<Lesson> Lessons   { get; set; }
}

var student1 = new Student(){
  Id = 1,
  Lessons = new List<Lesson>(){
    new Lesson(){
      Id = 1,
    }
  }
}

var students = new List<Student>(){
  student1
};

var dataSetMock = students.AsQueryable().BuildMockDbSet();

var dbContextMock = new Mock<IDbContextInterface>();
dbContextMock.SetupGet(e => e.Students)
    .Returns(dataSetMock.Object);

var repository = new StudentsRepository(dbContextMock.Object);

var studentResult = repository.GetById("12312312");

studentResult.Lessons.Should().NotBeNull();

But this will causes studentResult == student1 returns false because we need to instansiate new object.

My suggestion for this, we can add new param in BuildMockDbSet

public static IQueryable<TEntity> BuildMock<TEntity>(this IQueryable<TEntity> data, bool loadNavigation= true) where TEntity : class {}

Thanks 😃

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
romantitovcommented, Feb 4, 2021

@leopripos, thank you for more detailed explanation. I agree, that would be a very useful feature. I even did some research and I think technically it is possible (with some changes) to see whether an Include executed or not. But since Include is an extension it is not so easy to verify an execution of the extension with frameworks like Moq. Unfortunately the feature is not something what can be easily done and will take some time.

I usually work with the project in my free time and now I’m quite busy with my commercial projects for now.

I’ll keep the issue opened and who knows, maybe one day me or someone else will add the feature to the project. But I cannot promise that it will be soon.

1reaction
thomaslevesquecommented, Feb 5, 2021

Ah, I see

Read more comments on GitHub >

github_iconTop Results From Across the Web

Entity Framework Core 5 turn off loading ...
1 Answer. Default behaviour of EF is to not load related entities. In order to load related entities you could use: eager loading....
Read more >
How do you manage non-loaded navigation properties in ...
I don't think this is a good idea as a default option. 6. Write a getter for every property. Throw better exception if...
Read more >
Option to turn off automatic setting of navigation properties
I would like an option to turn this property off so that if I choose not to load a navigation property it is...
Read more >
Loading Related Entities - EF6
Turning off lazy loading for specific navigation properties. Lazy loading of the Posts collection can be turned off by making the Posts property ......
Read more >
Eager Load Navigation Properties By Default In EF Core
Normally when loading navigation properties in EF Core, you're forced to use the “Include” method to specify which navigational properties to pull back...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found