我一直在玩MVC-Mini-Profiler,发现它非常有用。但是,在我追踪的所有页面上,我都会收到重复查询的报告,如下所示。
但是,我已经在SQL Server Profiler中跟踪了查询,毫无疑问它只会在数据库中命中一次。
我在这里错过了一个概念,还是我错误地设置了它?对于有类似问题的人,我一直在搜索高低,没有运气,所以我怀疑是否有错误。
http://localhost:27941/clubs
T+175.2 ms
Reader
13.6 ms
utePageHierarchy Execute System.Collections.Generic.IEnumerable<T>.GetEnumerator GetResults Execute ExecuteStoreCommands
SELECT
[Extent1].[TeamId] AS [TeamId],
[Extent1].[Title] AS [Title],
[Extent1].[TitleShort] AS [TitleShort],
[Extent1].[LogoImageId] AS [LogoImageId],
[Extent1].[Slug] AS [Slug],
(SELECT
COUNT(1) AS [A1]
FROM [dbo].[Athletes] AS [Extent2]
WHERE [Extent1].[TeamId] = [Extent2].[TeamId]) AS [C1]
FROM [dbo].[Teams] AS [Extent1]
WHERE 352 = [Extent1].[CountryId]
http://localhost:27941/clubs
T+175.4 ms
DUPLICATE Reader
13.4 ms
utePageHierarchy Execute System.Collections.Generic.IEnumerable<T>.GetEnumerator GetResults Execute ExecuteStoreCommands
SELECT
[Extent1].[TeamId] AS [TeamId],
[Extent1].[Title] AS [Title],
[Extent1].[TitleShort] AS [TitleShort],
[Extent1].[LogoImageId] AS [LogoImageId],
[Extent1].[Slug] AS [Slug],
(SELECT
COUNT(1) AS [A1]
FROM [dbo].[Athletes] AS [Extent2]
WHERE [Extent1].[TeamId] = [Extent2].[TeamId]) AS [C1]
FROM [dbo].[Teams] AS [Extent1]
WHERE 352 = [Extent1].[CountryId
我使用EF4并实现了这样的上下文:
public class BaseController : Controller
{
public ResultsDBEntities _db;
public BaseController()
{
var rootconn = ProfiledDbConnection.Get(GetStoreConnection(ConfigurationManager.ConnectionStrings["ResultsDBEntities"].ConnectionString));
var conn = ProfiledDbConnection.Get(rootconn);
_db = ObjectContextUtils.CreateObjectContext<ResultsDBEntities>(conn);
}
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;
}
}
}
答案 0 :(得分:5)
我向Mini Profiler团队报告了这个问题(http://code.google.com/p/mvc-mini-profiler/issues/detail?id=62&can=1),他们今天发布了补丁这似乎解决了这个问题。
我想这将包含在下一个版本中。希望有所帮助:)