Suppose we have classes
public class Forest { public int Id { get; set; } public virtual ICollection<TreeType> TreeTypes { get; set; } = new List<TreeType>(); } public class TreeType { public int Id { get; set; } public string Name { get; set; } public virtual SomeLinkedData SomeLinkedData { get; set; } public int ForestId { get; set; } public virtual Forest Forest { get; set; } } public class ApplicationDbContext : IdentityDbContext { public DbSet<TreeType> TreeTypes { get; set; } public DbSet<Forest> Forests { get; set; } }
And I need to optimize the query. I can do this in db.TreeTypes.Where(x=>x.Forest.Id == forest.Id).Count()
in this query, I have only one query to the database and it will immediately return a number
And I can do so forest.TreeTypes.Count()
And I cannot understand how it will work in the second case, whether it will receive all types of trees first and then calculate their number, as a result, the load on the base will be greater than the first request and there will be data that can not be processed at all. handle. Or, in the second case, it also optimizes the query. As I understand the optimization in the second case will not. And if it does not, then why, it's so much extra resources to spend.
And in the continuation of the issue. I can do db.Forests.Include("TreeTypes.SomeLinkedData")
and how this can be done through forest.TreeTypes
there is no such function Include there is some kind of trimmed functionality in the navigation properties.
At the moment I am doing all such requests through db.
but this is not particularly convenient, and the db
object is not available in any context, I have to pass it, which I also do not particularly like when it would be possible to simply refer to the navigation property of the object.
public virtual ICollection<TreeType> { get; set; } = new List<TreeType>();
and unless that should not be a property name? - Bald