我通过 nuget 在我的项目中安装了 MiniProfiler 和 MiniProfiler.EF。
在使用 MiniProfiler 之前,我会在模型存储库中使用它打开一个连接:
public class NotificationRepository
{
private CBNotificationModel.CB_NotificationEntities db;
public NotificationRepository()
{
db = new CB_NotificationEntities();
}
public NotificationContact GetNotificationContacts()
{
return db.NotificationContacts.ToList();
}
}
要使用我创建的迷你分析器:
public static class ConnectionHelper
{
public static CB_NotificationEntities GetEntityConnection()
{
var conn = new StackExchange.Profiling.Data.EFProfiledDbConnection(GetConnection(), MiniProfiler.Current);
return ObjectContextUtils.CreateObjectContext<CB_NotificationEntities>(conn); // resides in the MiniProfiler.EF nuget pack
}
public static EntityConnection GetConnection()
{
return new EntityConnection(ConfigurationManager.ConnectionStrings["CB_NotificationEntities"].ConnectionString);
}
}
模型存储库现在使用
db = ConnectionHelper.GetEntityConnection();
然而这给出了错误:
mscorlib.dll 中发生“System.StackOverflowException”类型的未处理异常
我是不是少了一步?我尝试在 Application_start() 中添加 MiniProfilerEF.Initialize() 和 MiniProfilerEF.Initialize_EF42() 但这只会改变给出的错误。
似乎没有太多关于设置实体框架项目以使用 miniprofiler 的信息,除非它是 codefirst 的。
我可以通过将 ConnectionHelper 类更改为以下内容来实现此功能:
public static class ConnectionHelper
{
public static CB_NotificationEntities GetEntityConnection()
{
var connectionString = ConfigurationManager.ConnectionStrings["CB_NotificationEntities"].ConnectionString;
var ecsb = new EntityConnectionStringBuilder(connectionString);
var sqlConn = new SqlConnection(ecsb.ProviderConnectionString);
var pConn = new StackExchange.Profiling.Data.EFProfiledDbConnection(sqlConn, MiniProfiler.Current);
var context = ObjectContextUtils.CreateObjectContext<CB_NotificationEntities>(pConn);
return context;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)