Sam*_*ron 21 .net c# entity-framework entity-framework-4
为了从EF 4.1获得单个回调,我可以编写的最小代码量是多少,它提供了以下内容:
OnSQLExecuted(DbCommand cmd, DateTime start, double durationMS, string stacktrace) 目前我们使用了一个似乎在泄露性能的令人讨厌的黑客,我很好奇我们如何能够以最小的影响对应用程序实现此回调.
我们能够通过黑客入侵Mini Minir来解决这个问题 - Database.DefaultConnectionFactory但是我们改变了默认工厂的默认情况意味着你不能同时拥有两个分析工厂.所以我们采取了更激进的路线.
常用的技术是相当直接的,你实现:DbProviderFactory,IDbConnectionFactory,DbProviderServices,DbConnection,DbCommand并DbDataReader以这样的方式,他们拦截电话和个人资料.
到目前为止,很容易......但是当你试图连接它时它会变得混乱:
try
{
// ensure all the factories are loaded
DbProviderFactories.GetFactory("...");
}
catch (ArgumentException)
{
}
Type type = typeof(DbProviderFactories);
DataTable table;
// SUPER UGLY - Can this be done in another way?
object setOrTable = (type.GetField("_configTable", BindingFlags.NonPublic | BindingFlags.Static) ??
type.GetField("_providerTable", BindingFlags.NonPublic | BindingFlags.Static)).GetValue(null);
if (setOrTable is DataSet)
{
table = ((DataSet)setOrTable).Tables["DbProviderFactories"];
}
table = (DataTable)setOrTable;
foreach (DataRow row in table.Rows.Cast<DataRow>().ToList())
{
DbProviderFactory factory;
try
{
factory = DbProviderFactories.GetFactory(row);
}
catch (Exception)
{
continue;
}
var profType = typeof(MvcMiniProfiler.Data.EFProfiledDbProviderFactory<>).MakeGenericType(factory.GetType());
DataRow profiled = table.NewRow();
profiled["Name"] = row["Name"];
profiled["Description"] = row["Description"];
profiled["InvariantName"] = row["InvariantName"];
profiled["AssemblyQualifiedName"] = profType.AssemblyQualifiedName;
table.Rows.Remove(row);
table.Rows.Add(profiled);
}
Run Code Online (Sandbox Code Playgroud)
在最新版本的EF上需要一些反射黑客和完全炸弹:
FileLoadException: The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)
如何以健壮和优雅的方式连接我的剖析工厂和家庭?
有没有其他方法来获得我的回调?