0

探查器安装在我的 MVC 3 应用程序中并且它可以工作,但我遇到困难的是正确设置 EF 4 db 部分的正确方法。

从分析器的主页

使用工厂返回您的连接:

public static DbConnection GetOpenConnection()
{
    var cnn = CreateRealConnection(); 

    // wrap the connection with a profiling connection that tracks timings 
    return MvcMiniProfiler.Data.ProfiledDbConnection.Get(cnn, MiniProfiler.Current);
}

实体框架

public static MyModel Get()
{
    var conn = ProfiledDbConnection.Get(GetConnection());
    return ObjectContextUtils.CreateObjectContext<MyModel>(conn);
}

好的,所以我想澄清一下对 MyModel Get 方法的 GetConnection() 的调用是否应该读取 GetOpenConnection()?

那么如果它是一个错字,那么 CreateRealConnection 会是什么样子呢?我通过提供者模型使用 ODP.NET,并且我的模型库没有对 Oracle.DataAccess.Client 的引用,如果可以的话,我更愿意保持这种方式。

此外,所有这些代码将驻留在我的存储库中的什么位置?

public IQueryable<PRODUCTHEADER> Products
{
    get{ return ctx.PRODUCTHEADERs.AsQueryable(); }
}

谢谢你,斯蒂芬

4

2 回答 2

1

我能够从这篇带有 EF“模型优先”edmx 模型的 MiniProfiler 帖子中获取片段, 并首先将数据库的工作示例拼接在一起。现在授予此代码可能需要从控制器中拉出并退出,但它的工作方式与宣传的一样。

作为旁注,我最初的问题询问文档中是否有错字,我不相信,有两种不同的用法,它们并不像我想的那样相互依赖。

mvc-mini-profiler FTW!

问候,斯蒂芬

    public ActionResult Index()
    {
        // http://code.google.com/p/mvc-mini-profiler/
        // https://stackoverflow.com/questions/6802855/miniprofiler-with-ef-model-first-edmx-model

        var profiler = MiniProfiler.Current;

        var pConn = GetConnection();
        var context = pConn.CreateObjectContext<ChinookEntities>();

        using (profiler.Step("Doing complex stuff"))
        {
            using (profiler.Step("Step A"))
            {
                Thread.Sleep(100);
            }
            using (profiler.Step("Step B"))
            {
                Thread.Sleep(250);
            }
            using (profiler.Step("Step C"))
            {
                var result = context.Albums.AsQueryable()
                    .OrderBy(a => a.ArtistId).First();

                return View(result);
            }
        }
    }

    private static DbConnection GetConnection()
    {
        // A SqlConnection, SqliteConnection ... or whatever
        var connectionString = ConfigurationManager.ConnectionStrings["ChinookEntities"].ConnectionString;
        var ecsb = new EntityConnectionStringBuilder(connectionString);
        var sqlConn = new SqlConnection(ecsb.ProviderConnectionString);
        // wrap the connection with a profiling connection that tracks timings 
        return ProfiledDbConnection.Get(sqlConn, MiniProfiler.Current);
    }
于 2011-07-27T04:46:25.400 回答
-1

我要说的是迷你分析器只有 4.0,但它最近更新了。这可能会有所帮助(尽管您不是首先编写代码)

使用实体框架代码优先的 mvc-mini-profiler 数据库分析

于 2011-07-25T12:05:54.473 回答