私は次の機能を提供EF 4.1から単一のコールバックを取得するために書くことができ、最小限のコードは何ですか:どのようにしてEntity Frameworkを正しくプロファイルできますか?
私たちがあると思われる厄介なハックを使用する瞬間OnSQLExecuted(DbCommand cmd, DateTime start, double durationMS, string stacktrace)
私はこのコールバックをアプリに最小限の影響で達成する方法について興味があります。
我々はhacking aroundによってMini Profilerにこれを配線することができます - intially我々は、しかし、デフォルトの工場でいじくるDatabase.DefaultConnectionFactory
を変更するには、あなたが同時に起こって2つのプロファイリングの工場を持っていないことを意味します。そこで、より積極的なルートを進めました。
一般的に使用される技術はかなり簡単です、あなたが実装:DbProviderFactory
、IDbConnectionFactory
、DbProviderServices
、DbConnection
、DbCommand
とDbDataReader
を、彼らはコールとプロファイルを横取りするような方法で。
これまでのところ、簡単に...あなたはこれを配線しようとすると、しかし、それは散らかっ取得します。
FileLoadException: The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)
:それはEFの最新バージョンにいくつかの反射ハックと完全に爆弾を必要と
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);
}
プロファイル作成のファクトリとファミリを堅牢で洗練された方法で配線するにはどうすればよいですか?
コールバックを取得する他の方法はありますか?
質問はEFv4.1に言及しています。トレースラッパーがDbContext APIで動作するかどうかはわかりません。私は長い間それを試してみたいが、私はまだそれをチェックしていない。 –