Entity Framework 4.1 learning records

Source: Internet
Author: User
ArticleDirectory
    • 2. Entity Framework power tool
    • 4. implement on-demand update of Entity Framework
    • 5. No trace query for Entity Framework
    • 6. Other Tips

Overview:

Entity Framework 4.1 code first has been used for some time. This article mainly summarizes the learning process and practical skills. If someone else has written this article, I will directly post the link. Finally, I will have some practical skills I have summarized.

Body:

Learning Resources

1. mvc3 + ef4.1 Learning Series
2. Entity Framework 4.1
3. Summary of Entity Framework Object Relationships
4. Building an Entity Framework 4.0 model on views: Practical Tips

These are all tutorials carefully summarized by netizens. We recommend that you take a look at them. In particular, the first one is based on Microsoft's official example, and the author's own thoughts are added to write the article, which is very well written!

Practical Tips 1. Use SQL profiler

You can use SQL profiler to monitor SQL statements generated by EF, including update, delete, add, and select statements.CodeFinally, what kind of SQL statement is translated by. Net for further optimization. This tool is available in the Enterprise Edition of SQL, and is there no certainty in the dev version. We recommend that you install this component to analyze and use EF.

2. Entity Framework power tool

A very easy-to-use and cool tool can generate the corresponding code first based on your database, greatly facilitating the compilation of code first.

This plug-in can be downloaded from the Extension Manager of vs2010. Enter the Entity Framework power tool to search for the plug-in. For usage instructions, refer to the following article:

Http://www.cnblogs.com/LingzhiSun/archive/2011/05/24/EFPowerTool_1.html

3. Remove unnecessary contracts

// Prevents black-screen transactions. Otherwise, the corresponding data inventory of the context will be queried first.

 
PublicTestcontext ():Base(Dbconnmanager. Sqlconnection,True){Database. Setinitializer <Testcontext> (Null);}

// Prevents black screen transactions. Otherwise, you must access the edmmetadata table every time.

Protected override voidOnmodelcreating (DbmodelbuilderModelbuilder) {modelbuilder. Conventions. Remove <Includemetadataconvention> ();Base. Onmodelcreating (modelbuilder );}

If these two contracts are unnecessary, you must remove them. Otherwise, you can use SQL profiler to monitor and perform a simple query. EF can execute n SQL statements.

4. implement on-demand update of Entity Framework

Here, we will emphasize how the Entity Framework implements on-demand updates. First, an object before modification is given, and then the attribute value of the current object is modified. EF can track the changes and update them to the database when saving the changes!

However, you cannot mark this object through context. Entry (entitytoupdate). State = entitystate. Modified; otherwise, EF updates the entire object!

Example:

Using(VaRDB =NewTestcontext()){VaRTest = dB. Tests. firstordefault (ROW => row. ID = 1); test. Name ="Newname"; Test. lastname ="Oldvalue"; // This is the original value of this field dB. savechanges ();}

Only the name field is updated in the last generated SQL statement in the preceding example. It can be seen that if the entityThe field is not re-assigned, or the value is still the original value.That is, when there is no update, EF does not generate SQL statements that update these fields.

In fact, we can use the following code to query the Update Status of an object or attribute:

  pile  pile = _ context. piles. firstordefault (ROW => row. pileid = 1);  // the status of an object   entitystate  state = _ context. entry (pile ). state;  // the status of each field in the object   bool  isupdate = _ context. entry  pile  (pile ). property (P => P. pilename ). ismodified;  // whether to modify   string  now = _ context. entry  pile  (pile ). property (P => P. pilename ). currentvalue;  // current value   string  before = _ context. entry  pile  (pile ). property (P => P. pilename ). originalvalue;  // previous value  

Entity Framework uses these statuses to track whether an object has been updated and generate corresponding SQL statements at the end.

5. No trace query for Entity Framework

Although the tracking status mentioned above is very useful when updating, if the data is only provided to the UI for display, you do not need these traces. you can disable the tracking in the following ways:

1. dbset. sqlquery query with tracking status

2. dbdatabase. sqlquery no trace status query
3. dbdatabase. sqlcommand directly executes SQL statements, which are generally used for batch addition, deletion, and modification.

4. Add asnotracking () to the query to achieve no-trace query.

6. Other Tips

1. It is better to use the firstordefault method to find a piece of data. The generated SQL statement is simpler than the find statement;

2. Public String fullname {get {return lastname + "," + firstmidname ;}}

Fullname is not created in the database because it is only obtained, that is, a get. In this way, a fullname is not created in the database table, you can add computing columns in the domainmodel;

3. pessimistic concurrency is controlled by "locking". Optimistic Concurrency can be controlled by adding timestamp to the database;

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.