Eri*_*ric 4 entity-framework ef-code-first azure-sql-database
我意识到这个类似的问题已被问过几次,我在这些问题中尝试过这些建议而没有成功.
我正在使用实体框架(4.3)并针对SQL Azure运行(在联合数据库上).我希望能够记录实体框架生成的SQL.
我已经使用了实体分析器框架,虽然这在开发过程中很有用,但我不确定它在生产过程中是否有用.
我无法使用SQL事件探查器,因为这是一个SQL Azure数据库.
我已尝试使用EFTracingProvider并按照此处的步骤操作.不幸的是,当我尝试执行我的第一个命令(使用适当的联合)时,我得到一个异常,表明"不支持指定的方法".
生成错误的代码如下:
public MyContext(int tenantId)
: base(CreateTracingConnection("TheDb"), true)
{
const string FederationCmdText =
"USE FEDERATION TenantFederation(CustomerId = {0}) WITH RESET, FILTERING=ON";
((IObjectContextAdapter)this).ObjectContext.EnableTracing();
((IObjectContextAdapter)this).ObjectContext.Connection.Open();
// This is the line which throws the exception
this.Database.ExecuteSqlCommand(string.Format(FederationCmdText, tenantId));
}
Run Code Online (Sandbox Code Playgroud)
这是例外:
Specified method is not supported.
at EFProviderWrapperToolkit.DbConnectionWrapper.CreateDbCommand()
at System.Data.Common.DbConnection.CreateCommand()
...
Run Code Online (Sandbox Code Playgroud)
所以这是我的问题:
谢谢你的帮助,埃里克
我遇到的问题是因为,我相信,EFTracingProvider不支持直接执行SQL.为了解决这个问题,下面是一个解决方案(我在此处的问答中找到)
创建以下类:
public class DbTracingConnection : EFTracingConnection
{
protected override DbCommand CreateDbCommand()
{
return this.WrappedConnection.CreateCommand();
}
}
Run Code Online (Sandbox Code Playgroud)
创建连接时,请使用上面的类而不是EFTracingConnection.