在升级到EF6.1后,我们的目标是充分利用异步/等待选项与数据集进行交互。在从以前的Linq2Sql移植中,有许多.ToList(),.FirstOrDefault()和.Count()。我知道我们可以搜索并修复这些问题,但如果我们能够在编译时防止这些函数被允许进入构建,那就更好了。是否有创造性的想法来实现这一点?即使只是编译器警告也可以(例如使用Obsolete属性)。
接着上一篇文章...我没有找到一个可以在编译时检测到这个问题的解决方案,但是我能够在DataContext中通过代码实现:
public EfMyCustomContext(string connctionString)
: base(string.Format(CONNECTION_STRING, connctionString))
{
#if DEBUG
this.Database.Log = LogDataBaseCall;
#endif
}
#if DEBUG
private void LogDataBaseCall(string s)
{
if (s.Contains("Executing "))
{
if (!s.Contains("asynchronously"))
{
// This code was not executed asynchronously
// Please look at the stack trace, and identify what needs
// to be loaded. Note, an entity.SomeOtherEntityOrCollection can be loaded
// with the eConnect API call entity.SomeOtherEntityOrCollectionLoadAsync() before using the
// object that is going to hit the sub object. This is the most common mistake
// and this breakpoint will help you identify all synchronous code.
// eConnect does not want any synchronous code in the code base.
System.Diagnostics.Debugger.Break();
}
}
}
#endif