【发布时间】:2014-08-01 08:13:53
【问题描述】:
我知道我可以为所有查询设置 DbContext 的 CommandTimeout,如下所示:
public class YourContext : DbContext
{
public YourContext() : base("YourConnectionString")
{
// Get the ObjectContext related to this DbContext
var objectContext = (this as IObjectContextAdapter).ObjectContext;
// Sets the command timeout for all the commands
// to 2 min instead of the default 30 sec
objectContext.CommandTimeout = 120;
}
}
但是,我想保留默认的 30 秒,除了一个方法需要更长的时间。
我应该如何为这个单一查询更改它?
我确实尝试过使用:
public void doSomething(){
// The using had another reason, but in this case it also
// automatically disposes of the DbContext
using(IMyDbContext = delegateDbContext()){
((IObjectContextAdapter)usingDb).ObjectContext.CommandTimeout = 120;
... // myQuery
}
}
一切正常,直到我使用 Mock-DbContext 运行我的 UnitTest(是的,我确实将我的委托设置为这个 Mock-DbContext)。它给了我一个InvalidCastException:
System.InvalidCastException: Unable to cast object of type
'Castle.Proxies.FakeMyDbContextProxy' to type
'System.Data.Entity.Infrastructure.IObjectContextAdapter'.
【问题讨论】:
标签: c# asp.net-mvc entity-framework unit-testing mocking