Entity Framework 6 - Providing asynchronous requests, compilation time prevents synchronous calls

With the switch to EF6.1, our goal is to use the exclusivity of the Async / Await options that speak to our datasets. When migrating from our previous Linq2Sql, there are many .ToList (),. FirstOrDefault (), and .Count (). I know that we can search and fix everything, but it would be nice if at the time of compilation we could disable these functions even being allowed in the assembly. Does anyone have a creative idea on how to do this? Even if they were compiler warnings that could be selected (for example, using the Obsolete attribute).

+4
source share
2 answers

After that ... I never found a solution that could detect this at compile time, but I was able to do this in code in a 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 

Hope this helps someone else and still enjoy it if there is some option during compilation.

0
source

You can use the .NET Compiler Platform to write Diagnostics and Code Fix , which will look for these patterns and provide warnings / errors.

You can even implement syntax conversion to automatically modify these constructs - although the effort can be more expensive than just doing it manually.

0
source

All Articles