Increased logging for database operations in Entity Framework 6

Source: Internet
Author: User

Starting from EntityFramework6,  Because the database provider no longer inherits from System.Data.Common.DbProviderServices and inherits from System.Data.Entity.Core.Common.DbProviderServices, it cannot be passed as before The Dbproviderfactories.getfactoryclasses () method obtains the function of logging by getting the provider's DataTable and then using reflection to modify the provider that it implements for itself, but the EF 6 dbcontext class provides us with a Database property that returns a System.Data.Entity.Database object that contains a Log property that is defined as follows:

Public action<string> Log
{
    get
    {return
        this._internalcontext.log
    }
    Set
    {
        this._internalcontext.log = value;
    }
}

Therefore, we can achieve the purpose of logging the SQL action log by specifying a logging method for this property:

public class Mydbcontext:dbcontext
{public
    mydbcontext (): Base ("ConnectionString")
    {
        Database.setinitializer<mydbcontext> (null);
        Base. Database.Log = Logger;
    }

Then you can define your own logger method, when you perform a database operation, EF6 calls the method several times, with only a few records of a newline character (\ r \ n), the remaining SQL statements executed, execution start time, execution results, query parameters generated (if any), You need to deal with them separately.

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.