In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
It is believed that many inexperienced people don't know what to do with the example analysis of asynchronous query and working principle and annotation tag in EFCORE. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Asynchronous query
When executing queries in a remote database, synchronous queries may block the current thread, asynchronous queries can avoid blocking threads, which helps avoid freezing the client interface, asynchronous operations can also increase the throughput of Web applications, and you can release threads to handle other requests before the database operation is completed.
EF Core does not support running multiple parallel operations on the same context instance. You should always wait for the operation to complete before starting the next operation. This is usually done by using the await keyword on each asynchronous operation.
Entity Framework Core provides a set of asynchronous extension methods that can be used to execute asynchronous queries. For example:
ToListAsync (), ToArrayAsync (), SingleAsync (), if you see the end of Async, it's usually an asynchronous method, which is also Microsoft's naming convention, and I hope you follow it.
For some LINQ operators (such as Where (...), OrderBy (...)) There is no corresponding asynchronous version, because these methods are only used to build the LINQ expression tree, not send the query to the database for execution, and only go to the database query when the result is used. This is an inherent feature of IQueryable, which we talked about before.
Public async Task GetBlogsAsync ()
{
Using (var context = new BloggingContext ())
{
Return await context.Blogs.ToListAsync ()
}
}
Asynchronous programming
How the query works
The resulting expressions written by the query are processed by EF Core and the SQL statements equivalent to LINQ are generated according to different database providers.
A: the queried data is cached so that performance is consumed each time the same data query is executed.
The query expression is passed to the database provider
A: the provider identifies which parts of the query can be evaluated in the database, which can only be done on the client, and which cannot be done at all.
B: for the operations that can query the database, generate the corresponding SQL statement.
C: one or more queries are sent to the database and the result set is returned. What is returned here is not a collection of entity objects, but a dataset.
For each item in the result set
A: in the case of a trace query (the default trace), EF checks that the data item already exists in the tracker. If so, return the existing entity, if not, create the entity and add the entity to the tracker.
B: if the query is an untracked query, EF checks to see if the data represents an existing entity in the query result set, if so, returns the existing entity, if not, creates a new entity and returns the new entity.
When the LINQ operator is called, only the in-memory representation of the query is built. The query is sent to the database only when the results are used.
The most common actions that cause queries to be sent to the database are as follows:
Iterate through the results in a for or foreach loop
Use ToList, ToArray, Single, Count, and so on
When binding query result data to the UI interface.
Query comment tag
Use the TagWith () method to annotate the SQL statement generated by LINQ for easy reading.
one
Context.Friends.TagWith ("This is my spatial query!")
After reading the above, have you mastered the asynchronous query and how it works in EFCORE, as well as the method of sample analysis of annotation tags? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.