Entity Framework 6 Recipes 2nd Edition (12-3), entityrecipes

Source: Internet
Author: User

Entity Framework 6 Recipes 2nd Edition (12-3), entityrecipes

12-3. database connection logs

Problem

You want to record logs for each connection and disconnection from the database

Solution

EF exposes a StateChange event for the DbContext connection. We need to handle this event and record logs for each connection and disconnection with the database.

Suppose our model is shown in Figure 12-3. in the Listing 12-3 code, we create some Donation instances and save them to the database. the code here implements the override SaveChanges () method to provide an entry point for our StateChange event.

 

Figure 12-3.The model with the Donation entity

 

Listing 12-3.Code to Implement Logging of Open and Close of a Database Connection

Class Program

{

Static void Main (string [] args)

{

RunExample ();

}

Static void RunExample ()

{

Using (var context = new EFRecipesEntities ())

{

Context. Donations. Add (new Donation

{

DonorName = "Robert Byrd ",

Amount = 350 M

});

Context. Donations. Add (new Donation

{

DonorName = "Nancy McVoid ",

Amount = 250 M

});

Context. Donations. Add (new Donation

{

DonorName = "Kim Kerns ",

Amount = 750 M

});

Console. WriteLine ("About to SaveChanges ()");

Context. SaveChanges ();

}

Using (var context = new EFRecipesEntities ())

{

Var list = context. Donations. Where (o => o. Amount> 300 M );

Console. WriteLine ("Donations over $300 ");

Foreach (var donor in list)

{

Console. WriteLine ("{0} gave {1}", donor. DonorName,

Donor. Amount. ToString ("C "));

}

}

Console. WriteLine ("Press any key to close ...");

Console. ReadLine ();

}

}

Public partial class EFRecipesEntities

{

Public override int SaveChanges ()

{

This. Database. Connection. StateChange + = (s, e) =>

{

Var conn = (DbConnection) s;

Console. WriteLine ("{0}: Database: {1}, State: {2}, was: {3 }",

DateTime. Now. tow.timestring (), conn. Database,

E. CurrentState, e. OriginalState );

};

Return base. SaveChanges ();

}

}

The above Listing 12-3 code output is as follows:

About to SaveChanges ()

AM: Database: EFRecipes, State: Open, was: Closed

09: 56: Database: EFRecipes, State: Closed, was: Open

Donations over $300

Robert Byrd gave $350.00

Kim Kerns gave $750.00

Principle

We implement the override SaveChanges () method to provide an entry point for our StateChange event. This event handler receives two parameters: the event sender and StateChangeEventArgs. the second parameter provides the current and original status of the access connection. We will log the two database-related statuses. if you pay attention to the log Content sequence, you will notice that in the second using block, the connection to the database is restarted when foreach executes the query, rather than when writing the query statement. this demonstration illustrates an important concept: query is only performed when necessary. in our example, the query is executed only during traversal.

 

Appendix: script file of the database used in the Creation example

 

 

Related Article

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.