Does .NET 4.5's async feature work with MySql and other databases too?

后端 未结 3 408
日久生厌
日久生厌 2020-12-21 00:33

I understand that .NET 4.5 comes with a bunch of features to make asynchronous database operations easier to implement. MSDN says that if the connection string is not set to

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-21 01:00

    I was into the same problem today and I made a Console Application to test whether it's working or not. Seems to me it's not working for my MySQL connector 6.9.4.

    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace MySQLTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                Service service = new Service();
    
                var task1 = service.GetCountries("1");
                var task2 = service.GetCountries("2");
                var task3 = service.GetCountries("3");
    
                Console.WriteLine("bö");
                Console.ReadLine();
            }
        }
    
        public class Service
        {
           public async Task> GetCountries(string param)
           {
                Console.WriteLine(String.Format("{0} started.", param));
                using (worldEntities context = new worldEntities())
                {
                    Console.WriteLine(String.Format("{0} awaiting.", param));
                    List countries = await context.country.ToListAsync();
    
                    Console.WriteLine(String.Format("{0} done.", param));
                    return new List();
                }
            }
        }
    }
    

    This one outputs,

    output1

    And When I change List countries = await context.country.ToListAsync();

    to await Task.Delay(5000);

    it outpus :

    output2

    after 5 seconds.

    So I say it's not supported yet.

提交回复
热议问题