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.