使用带有EF 4.0和Ninject的mvc-mini-profiler

时间:2011-06-09 16:52:25

标签: asp.net-mvc-3 entity-framework-4 ninject mvc-mini-profiler

我正在尝试将新的mvc-mini-profiler与我的基于EF4的应用程序一起使用,但我不知道如何正确获取与目标数据源的连接。

据我所知,这是。

Func<IMyContainer> createContainer = () =>
{
    var profiler = MiniProfiler.Current;

    if (profiler != null)
    {
        var rootConn = // ????
        var conn = ProfiledDbConnection.Get(rootConn);
        return ObjectContextUtils.CreateObjectContext<MyContainer>(conn);
    }
    else
    {
        return new MyContainer();
    }
};

kernel.Bind<IMyContainer>().ToMethod(ctx => createContainer()).InRequestScope();

如果没有contianer本身,如何连接到EF容器?我只是新建一个SqlConnection,除了连接字符串包装在所有EF垃圾中。

3 个答案:

答案 0 :(得分:4)

稍微不那么狡猾的方式:

private static SqlConnection GetConnection()
{
    var connStr = ConfigurationManager.ConnectionStrings["ModelContainer"].ConnectionString;
    var entityConnStr = new EntityConnectionStringBuilder(connStr);
    return new SqlConnection(entityConnStr.ProviderConnectionString);
}

<小时/> John Gietzen的修正案:

所有答案的这种组合应该适用于Entity Framework支持的任何后备存储。

public static DbConnection GetStoreConnection<T>() where T : System.Data.Objects.ObjectContext
{
    return GetStoreConnection("name=" + typeof(T).Name);
}

public static DbConnection GetStoreConnection(string entityConnectionString)
{
    // Build the initial connection string.
    var builder = new EntityConnectionStringBuilder(entityConnectionString);

    // If the initial connection string refers to an entry in the configuration, load that as the builder.
    object configName;
    if (builder.TryGetValue("name", out configName))
    {
        var configEntry = WebConfigurationManager.ConnectionStrings[configName.ToString()];
        builder = new EntityConnectionStringBuilder(configEntry.ConnectionString);
    }

    // Find the proper factory for the underlying connection.
    var factory = DbProviderFactories.GetFactory(builder.Provider);

    // Build the new connection.
    DbConnection tempConnection = null;
    try
    {
        tempConnection = factory.CreateConnection();
        tempConnection.ConnectionString = builder.ProviderConnectionString;

        var connection = tempConnection;
        tempConnection = null;
        return connection;
    }
    finally
    {
        // If creating of the connection failed, dispose the connection.
        if (tempConnection != null)
        {
            tempConnection.Dispose();
        }
    }
}

答案 1 :(得分:2)

这是一个稍微好一点,但稍微有点讨厌的解决方案来获得商店连接。

    public static DbConnection GetStoreConnection<T>() where T : System.Data.Objects.ObjectContext
    {
        return GetStoreConnection("name=" + typeof(T).Name);
    }

    public static DbConnection GetStoreConnection(string entityConnectionString)
    {
        DbConnection storeConnection;

        // Let entity framework do the heavy-lifting to create the connection.
        using (var connection = new EntityConnection(entityConnectionString))
        {
            // Steal the connection that EF created.
            storeConnection = connection.StoreConnection;

            // Make EF forget about the connection that we stole (HACK!)
            connection.GetType().GetField("_storeConnection",
                BindingFlags.NonPublic | BindingFlags.Instance).SetValue(connection, null);

            // Return our shiny, new connection.
            return storeConnection;
        }
    }

答案 2 :(得分:1)

您必须直接初始化连接,如下:

var rootConn = new System.Data.SqlClient.SqlConnection(your_connection_string_minus_your_ef_junk);