Extending EF with entityframework.extended (Entity Framework Extended series 2)

Source: Internet
Author: User

Preface

Entity Framework Extension Series Catalog

Today we're talking about entityframework.extended.

First of all, the entityframework.extended is what this is, as follows:

This is a class library that extends the Entity Framework.

Fully supports EF 5.0/6.0+,

GitHub address https://github.com/loresoft/EntityFramework.Extended,

The last update was in 2015/07/10

This library supports batch update, delete. Query results cache and audit logs.

This extension generates only one SQL statement per batch operation, and does not produce n-SQL statements as bulk n data as the native method of the entityframework supply

the environment and technology used in this paper

System: WIN7

Database: SQL Server2008

Related technologies: mvc5+ef6.1.3+entityframework.extended6.0

Chapter One: bulk Operations database

Bulk Delete:

// Remember to quote using entityframework.extensions; // Both of these can be written, and the context is your EF contextual object. " AA "  "aa"). Delete ();

Batch update:

//the number of rows modified by dataintData =context. Logdata.where (a=>a.entitykey=="AA"). Update (b=>Newlogdata {entityname ="SS" });//The second way, this is for Dbset, has been marked out of date varData = context. Logdata.update (A = A.entitykey = ="AA",b=>Newlogdata {entityname ="SS"});

Batch Increase:

// this has nothing to do with extended. The EF itself comes with a simple example of a novice, and the great God ignores it . int data= context. Logdata.addrange (new list<logdata> ());

Chapter II: Bulk Query Database

Bulk query:

                    varCount =context.                    Logdata.futurecount (); vardata =context.                     Logdata.futurefirstordefault (); varDataList =context.                    Logdata.future (); //in the same code context, count,data,datalist these three objects//either one, the first time tolist or. Value, the database is connected once//query These three data at the same time, without having to query three times                    varDataLists =DataList.                    ToList (); intCountdata = count. Value;

The principle is this:

The original text of E:

The future queries is creating the appropriate Ifuturequery object that keeps the iquerable. The Ifuturequery object is and stored in ifuturecontext.futurequeries list. Then, when one of the Ifuturequery objects are enumerated, it calls back to Ifuturecontext.executefuturequeries () via the L Oadaction delegate. Executefuturequeries builds a batch query from all the stored Ifuturequery objects. Finally, all the Ifuturequery objects is updated with the results from the query.

This cock of spicy chicken loafers translation:

The future creates his own custom Ifuturequery object based on Iquerable, and then joins him to the Ifuturecontext.futurequeries query queue, When an object in the queue calls Loadaction then the Ifuturecontext.executefuturequeries () method is executed, executefuturequeries constructs a batch query Ifuturequery , and finally, all Ifuturequery query results are updated (that is, queries).

Chapter III: Data audit logs for EF

Data audit log:

First of all, the concept of auditing is to monitor the operation of all entities (increase, delete, change).

Let's look at the effect first.

This is the information he traced, we can easily put this information in the database or in your log store (text, XML, cache) are all right with you.

Let's start by explaining usage:

First we can configure the application in the portal (Application_Start) (I'm going to use MVC's Web App for example):

Here the configuration can not add, with the default also line, of course, there is a need to refer to

Https://github.com/loresoft/EntityFramework.Extended/wiki/Audit-Log

Here is a more detailed description of the configuration

  protected voidApplication_Start () {arearegistration.registerallareas ();            Filterconfig.registerglobalfilters (globalfilters.filters);            Routeconfig.registerroutes (routetable.routes);            Bundleconfig.registerbundles (Bundletable.bundles); //here is the configuration of the audit log            varAuditconfiguration =Auditconfiguration.default; Auditconfiguration.includerelationships=true; Auditconfiguration.loadrelationships=false; Auditconfiguration.defaultauditable=true; }

Let's start with the direct use,

Give me a chestnut:

                using(Studentinfoentities US =Newstudentinfoentities ()) {                    //Open Log                    varLogaudit =us.                   Beginaudit (); //to perform an increase and deletion operationus. Logdata.add (NewLogdata () {EntityKey ="AA", EntityName ="ASD", Name ="ASD" }); vardata = us. Logdata.where (A = a.ID = = the).                    FirstOrDefault (); Data. Name="Test"; varDelete = us. Logdata.where (A = a.ID = =9).                    FirstOrDefault (); us.                    Logdata.remove (delete); //Normal Saveus.                   SaveChanges (); //Get audit Logs                    varLog =Logaudit.                    Lastlog; //Convert the log to an XML string, or to an XML file                    stringDDA =log.                TOXML (); }

The final result is as follows:

We can see that we are conveniently monitoring the three operations we have just done.

(Important note: Here, the audit log can only monitor the general wording of the deletion and modification, for the above batch additions and deletions are very strange is not monitored , as the expansion of their own library, but they do not monitor--this has to say it is very egg pain.)

Fourth Chapter: Query Caching

The code is as follows:

// Use the default cache time var tasks = db. Tasks    null)    . Fromcache (); // Query Results cache 300 seconds var tasks = db. Tasks    null)    . Fromcache (cachepolicy.withdurationexpiration (timespan.fromseconds));

I do not use a lot of this, it is not detailed to repeat, interested friends can see for themselves

Https://github.com/loresoft/EntityFramework.Extended/wiki/Query-Result-Cache

written in the last

This is the end of the article, the process of writing the article is also self-aware of the new process. Write bad hope that the great God more correct, I very much hope to play the effect, the previous article has been a lot of good advice, I hope we do not Sik Sik Yuen Enlighten.

Extending EF with entityframework.extended (Entity Framework Extended series 2)

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.