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

Source: Internet
Author: User

Objective

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 refer to using entityframework.extensions;//both ways, context is your EF context object. context.LogData.Delete (A = A.entitykey = = " AA "); context. Logdata.where (A = A.entitykey = = "AA"). Delete ();

Batch update:

Data is the modified number of rows int   data =context. Logdata.where (a=>a.entitykey== "AA"). Update (b=> new Logdata {entityname = "ss"});//The second way, this is for Dbset, has been labeled obsolete var data = context. Logdata.update (A = A.entitykey = = "AA",b=> new Logdata {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:

                    var count =context. Logdata.futurecount ();                    var data = context. Logdata.futurefirstordefault ();                     var DataList = context. Logdata.future ();                    In the same code context, count,data,datalist these three objects                    //Any one, the first time tolist or. Value, the database will be connected once                    //At the same time to query the three data, and not three times the query                    var datalists = DataList. ToList ();                    int countdata = 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 void Application_Start ()        {            arearegistration.registerallareas ();            Filterconfig.registerglobalfilters (globalfilters.filters);            Routeconfig.registerroutes (routetable.routes);            Bundleconfig.registerbundles (bundletable.bundles);            Here is the configuration of the audit log            var auditconfiguration = 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 = new Studentinfoentities ())                {                    //open log                    var logaudit = us. Beginaudit ();                   Perform the increase and deletion operation                    us. Logdata.add (New Logdata () {EntityKey = "AA", EntityName = "ASD", Name = "ASD"});                    var data = us. Logdata.where (A = a.ID = = 15). FirstOrDefault ();                    Data. Name = "Test";                    var delete = us. Logdata.where (A = a.ID = = 9). FirstOrDefault ();                    us. Logdata.remove (delete);                    Save                    US normally. SaveChanges ();                   Get audit log                    var log = Logaudit. Lastlog;                    Convert the log to an XML string, or to the XML file,                    string dda = 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    . Where (t = = T.completedate = null)    . Fromcache ();//Query Result Cache 300 sec var tasks = db. Tasks    . Where (t = = T.assignedid = = myUserID && T.completedate = = null)    . Fromcache (Cachepolicy.withdurationexpiration (Timespan.fromseconds (300)));

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.