making database call asynchronous in EF

2019-09-04 19:02发布

问题:

I am using EF 5.0 and have a question on making the Database calls asynchronous.

To begin with below is the autogenerated method call in contextModel>.context.cs

public virtual ObjectResult<InstructorsComplex> GetInstructors(string nm, string cd, string grp_cd)
    {
        var nmParameter = nm != null ?
            new ObjectParameter("nm", nm) :
            new ObjectParameter("nm", typeof(string));

        var cdParameter = cd != null ?
            new ObjectParameter("cd", cd) :
            new ObjectParameter("cd", typeof(string));

        var grp_cdParameter = grp_cd != null ?
            new ObjectParameter("grp_cd", grp_cd) :
            new ObjectParameter("grp_cd", typeof(string));

        return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<InstructorsComplex>("GetInstructors", nmParameter, cdParameter, grp_cdParameter);
    }

I am calling the method as below to achieve asynchrony since above call takes about 1 second to execute:

public async Task<IList<Instructor>> GetInstructors(DatabaseRequest input)
    {           
        //fetch from database ASYNChronously
        var daoOutput = await Task.Run( () => _DAO.GetInstructors(input));

        var retVal = daoOutput.ToList<Instructor>();

        return retVal;
    }

But would it give any kind of performance boost just by running the process using Task.Run() ?

In .Net framework we have xxxAsync() methods. But I don't find any such methods in EF 5.0.

If above code is not efficient, how can I write a asynchronous method for expensive database calls using EF 5.0 ?

Any help is appreciated.