Consider the following
Models:
public class Blog
{
public Guid ID {get; set;}
public string Name {get; set;}
// more fields...
// Navigation Property
public IList<Post> Posts {get; set;}
}
public class Posts
{
public Guid ID {get; set;}
public Guid BlogID {get; set;}
public string Author {get; set;}
// more fields...
// Navigation Property
public Blog Blog {get; set;}
}
In DbContext.OnModelCreating :
// Primary Keys
modelBuilder.Entity<Blog>()
.HasKey(c => c.ID);
modelBuilder.Entity<Post>()
.HasKey(c => c.ID);
modelBuilder.Entity<Blog>()
.HasMany(c => c.Posts)
.WithOne(c => c.Blog)
.HasForeignKey(c => c.BlogID)
.HasPrincipalKey(c => c.ID);
modelBuilder.Entity<Post>()
.WithOne(c => c.Blog)
.HasMany(c => c.Posts)
.HasForeignKey(c => c.BlogID)
.HasPrincipalKey(c => c.ID);
This all works, the problem is then I fetch a Blog it contains a Post which contains the Blog which contains a list of Posts which each contain the Blog and so on.
How can I limit this to a specific level of nesting ? I checked the docs and could not find a solution.
I don't think you can limit that at all. But also I don't think that can cause any issues.
EntityFramework is dealing with navigation properties
by replacing (overriding) their functionality with actual query to the database. This approach is called LazyLoading
.
That means, everytime you access the property, it just do a call to the database and brings the data for you, and it can do this forever, as you requesting it to.
I hope that explains it?
Further explanation can be found here .
Regards
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.