Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
129 views
in Technique[技术] by (71.8m points)

c# - Entity Framework 4.1 - EFTracingProvider

Is there a way to make the EFTracing provider work with EF 4.1?

EFTracing seems to need an objectcontext and I use dbcontext.

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, you can. I'm using the community version with both database-first DbContexts and code-first DbContexts. This answer is based on a discussion thread on the project site.

For database-first/designer DbContexts (via ADO.NET DbContext Generator templates), you can simply add the following constructor:

public abstract class MyDbContext : DbContext
{
    protected MyDbContext(string nameOrConnectionString)
        : base(EFTracingProviderUtils.CreateTracedEntityConnection(nameOrConnectionString), true)
    {
        // enable sql tracing
        ((IObjectContextAdapter) this).ObjectContext.EnableTracing();
    }
}

For code first DbContexts its a bit more complicated since the EFTracingProvider wants a full entity connection string. You have to create an instance of EFTracingConnection manually. The following example will work for both database first and code first contexts.

public abstract class MyDbContext : DbContext
{
    protected MyDbContext(string nameOrConnectionString)
        : base(CreateTracingConnection(nameOrConnectionString), true)
    {
        // enable sql tracing
        ((IObjectContextAdapter) this).ObjectContext.EnableTracing();
    }

    private static DbConnection CreateTracingConnection(string nameOrConnectionString)
    {
        try
        {
            // this only supports entity connection strings http://msdn.microsoft.com/en-us/library/cc716756.aspx
            return EFTracingProviderUtils.CreateTracedEntityConnection(nameOrConnectionString);
        }
        catch (ArgumentException)
        {
            // an invalid entity connection string is assumed to be a normal connection string name or connection string (Code First)

            ConnectionStringSettings connectionStringSetting =
                ConfigurationManager.ConnectionStrings[nameOrConnectionString];
            string connectionString;
            string providerName;

            if (connectionStringSetting != null)
            {
                connectionString = connectionStringSetting.ConnectionString;
                providerName = connectionStringSetting.ProviderName;
            }
            else
            {
                providerName = "System.Data.SqlClient";
                connectionString = nameOrConnectionString;
            }

            return CreateTracingConnection(connectionString, providerName);
        }
    }

    private static EFTracingConnection CreateTracingConnection(string connectionString, string providerInvariantName)
    {
        // based on the example at http://jkowalski.com/2010/04/23/logging-sql-statements-in-entity-frameworkcode-first/
        string wrapperConnectionString =
            String.Format(@"wrappedProvider={0};{1}", providerInvariantName, connectionString);

        EFTracingConnection connection =
            new EFTracingConnection
                {
                    ConnectionString = wrapperConnectionString
                };

        return connection;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...