Quantcast
Channel: entityframework Discussions Rss Feed
Viewing all articles
Browse latest Browse all 1793

New Post: ToListAsync doesn't return with await

$
0
0

Hi everybody,

I have created a very simple test method to play with async methods of EF6 but this just doesn't work with await. I cannot get the exception if there is ever one here (in all honesty, I didn't try that hard to find it). Here is the code:

publicclass AccommProperty {

    publicint AccommPropertyID { get; set; }
    publicstring PropertyName { get; set; }
}

publicclass MyContext : DbContext {

    public MyContext() : base("Hotels") {
    }

    public IDbSet<AccommProperty> AccommProperties { get; set; }
}

class Program {

    staticvoid Main(string[] args) {

        GetHotelsAsync().ContinueWith(task => {

            var hotels = task.Result;
            foreach (var hotel in hotels) {
                Console.WriteLine(hotel.PropertyName);
            }
            Console.ReadLine();
        });
    }

    static async Task<IEnumerable<AccommProperty>> GetHotelsAsync() {

        using (MyContext ctx = new MyContext()) {

            var hotels = await ctx.AccommProperties.ToListAsync();
            return hotels;
        }
    }
}

The AccommProperties table is not empty, there are 247 records inside it.

Also, the following code works perfectly fine:

publicclass AccommProperty {

    publicint AccommPropertyID { get; set; }
    publicstring PropertyName { get; set; }
}

publicclass MyContext : DbContext {

    public MyContext() : base("Hotels") {
    }

    public IDbSet<AccommProperty> AccommProperties { get; set; }
}

class Program {

    staticvoid Main(string[] args) {

        MyContext ctx = new MyContext();
        ctx.AccommProperties.ToListAsync().ContinueWith(task => {

            var hotels = task.Result;
            foreach (var hotel in hotels) {
                Console.WriteLine(hotel.PropertyName);
            }
        });
        Console.ReadLine();
    }
}

Not sure what I am missing here or there is anything wrong with the EF here.


Viewing all articles
Browse latest Browse all 1793

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>