Option to not load the navigation property
See original GitHub issueDo 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:
- Created 3 years ago
- Comments:8 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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 sinceInclude
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.
Ah, I see