首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将mvc-mini-profiler与EF4.0和Ninject一起使用

将mvc-mini-profiler与EF4.0和Ninject一起使用
EN

Stack Overflow用户
提问于 2011-06-10 00:52:25
回答 3查看 1.6K关注 0票数 11

我正尝试在基于EF4的应用程序中使用新的mvc-mini-profiler,但我不知道如何正确地连接到我的目标数据源。

这就是我所能得到的。

代码语言:javascript
复制
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垃圾文件中。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-06-27 19:12:37

稍微不那么老套的方式:

代码语言:javascript
复制
private static SqlConnection GetConnection()
{
    var connStr = ConfigurationManager.ConnectionStrings["ModelContainer"].ConnectionString;
    var entityConnStr = new EntityConnectionStringBuilder(connStr);
    return new SqlConnection(entityConnStr.ProviderConnectionString);
}

John Gietzen提出的修正案:

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

代码语言:javascript
复制
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();
        }
    }
}
票数 4
EN

Stack Overflow用户

发布于 2011-06-10 05:37:57

这是一个性能稍好,但略显老练的解决方案,以获得商店连接。

代码语言:javascript
复制
    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
EN

Stack Overflow用户

发布于 2011-06-10 01:10:35

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

代码语言:javascript
复制
var rootConn = new System.Data.SqlClient.SqlConnection(your_connection_string_minus_your_ef_junk);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6296444

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档