Learning notes for ADO. Net object framework 4

Source: Internet
Author: User

Here I will summarize some things that need attention during my learning process:

1. BecauseObjectqueryIqueryable and ienumerableObjectqueryThe implemented query generator method is combined with a specific standard query operator (such as first or count) of LINQ. Unlike the query generator method, the LINQ operator does not returnObjectquery

2. Try to use the static create method of the object when creating the object.

3. When modifying a referenced object of an object, do not directly modify the primary key of the referenced object. If you have to modify the primary key, you can directly assign a new value to the referenced object.

4. By default,ObjectqueryReturns zero or more entity objects of a specific type. Call subsequent query methods (for exampleWhereAndOrderby) Will affect the originalObjectqueryThe collection of returned objects. Some methods (suchSelectAndGroupby) Returns the projection of the data as dbdatarecord, rather than the projection of the object type.

5. Try to use it when you know the key value of an object.GetobjectbykeyTo find objects. Do not use LINQ to find objects becauseGetobjectbykeyIs directly in the data source cache to query objects, faster

6. If you only search for data and do not need to update the data, you can useMergeoption.NotrackingCancel change tracking. In this way, objectstatemanager will not be used to reduce the query execution time. All returned entities will be in the detached state ). Notracking is a good choice for displaying data in read-only mode in ASP. NET web applications or in winforms/WPF grids controls. When using the LINQ to entities or createquery <t> factory method used together with Entity SQL, you cannot directly pass in the notracking option. You need to set the mergeoption option for the entire entityset.

ExampleCodeAs follows:

VaR categories = from C in this. Context. Category

Where C. parentcategory. Name! = NULL

Orderby C. Name

Select C;

Context. Category. mergeoption = mergeoption. notracking;

Return categories. tolist ();

That is to say, it is not possible to change mergeoption after the query is complete, and an invalidoperationexception will occur.

7. use compiledquery. compile () pre-compiled query. You do not need to concatenate query strings every time. You can directly obtain query statements from the cache after splicing for the first time, which improves performance.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.