Iii. query classes
Because the database is completely abstract to EDM, you can ignore the underlying database structure to query classes. EF converts the ing information to SQL. The workflow is as follows:
The abstraction between classes and databases is not hard to understand. If an attribute does not match the name of a data table column, you can still use the attribute name in your query-this is very obvious in class query. When using LINQ to entities, you are unlikely to enter the wrong name, because the intelligent prompt of vs prompts you with the correct name. More importantly, if you type an incorrect attribute name, the compiler throws an exception.
In other cases, the database and object model may be different. Customers and suppliers have their own classes, but only one table stores their data. Again, you query these entity classes instead of the company table.
In addition, we have merged address information groups into a complex type. When querying, they are accessed as part of the type, but they are clearly listed in the database.
At first, these differences may blind you, especially when you are familiar with relational models. However, after some practices, you will find that this method of query is more intuitive and effective than SQL query. Class indicates that the business scenario is more meaningful than the relational model, and the result is that the class query is more business-oriented than the SQL query. More importantly, EF processes the intricate relationships between classes, and you do not need to care about the physical associations of data.
4. retrieve data from the database
When you execute a query, LINQ provides resolution queries for managed object sets and creates a command tree. Because you are writing a query for the class, the command tree represents a conceptual query. Object Service sends the command tree to entity client. With the help of ing and storage files, the command tree is converted to a local SQL command. Entity client provided by ADO. netProgram(Specified in the provider section of the connectionstring attribute) start SQL queries on the database and obtain the results. Then, the query results form a table to reflect the structure of the object model. Finally, the data is returned to the object service layer. This process is described as follows:
After adding the scalar, complex, and reference attributes, the context triggers the objectmaterialized event before processing the set attributes. This event accepts the entity being embodied and allows you to execute some logic before the object is returned to the program. The followingCodeShows you how to use this event.
PublicOrderitentities () {CTX. objectmaterialized + =NewObjectmaterializedeventhandler(Ctx_objectmaterialized );}VoidCtx_objectmaterialized (ObjectSender,ObjectmaterializedeventargsE ){VaRO = entityAsOrder;// Any logic}
5. Capture generated SQL
Although the Entity Framework generates SQL code, you cannot assume that the code is good. You must always ensure that the generated code does not require too many database resources. Sometimes, the generated query is too complex and complex. You can use the stored procedure or manually write SQL statements to achieve better performance.
The easiest way to check SQL statements is to use the profiler tool. With this tool, you can monitor all statements executed on the database and analyze the statements that need to be refined or completely replaced. Usually, such monitoring requires advanced database permissions, and sometimes you do not have such permissions. Fortunately, the OSS layer uses the totracestring method of the objectset <t> class to help you. How to use it
VaR result = CTX. Orders. Where (O => O. Date. Year =Datetime. Now. year); var SQL = (ResultAsObjectquery). Totracestring ();
6. When will the query be executed?
In general, the code of the program is executed when the code of the program accesses the data. Each time the LINQ to entities query, EF accesses the database and retrieves data. When data is converted from a record to an object, LINQ to entities scans the memory to check whether the object exists. If yes, it returns the object in the memory. If no, it converts the record to an object, put in memory and return.
Let's look at an example. Suppose you have to traverse the two query results as follows:
VaR result = linqtoentitiesquery;Foreach(VAR oInResult ){...}Foreach(VAR oInResult ){...}
Each foreach execution triggers a round-trip operation on the database. The context does not care whether you have executed the same query.
If it is not a simple query of all orders, but a more complex query, two executions indicate serious performance problems. The simple solution is to execute the command for the first time and then download the object to the memory. Then traverse the set in the memory so that you do not need to access the database twice. The following code is used:
VaR result = linqtoentitiesquery. tolist ();Foreach(VAR oInResult ){...}Foreach(VAR oInResult ){...}
This method not only applies to the returned Object List, but also to returning a single object. EF makes the query return an object and a set no difference.
7. manage databases from context
The objectcontext class allows you to operate on the database structure using four interesting methods:
Createdatabase-use the connection string and SSDL information to create a database
Createdatabasescript-use SSDL information to generate database scripts
Databaseexists-verify whether the database with the specified connection string exists
Deletedatabase-delete a database using the specified connection string
These methods are useful when creating an application without creating an installation package. When the program starts, you can use databaseexists and createdatabase to create a database if the database does not exist.