making database call asynchronous in EF

末鹿安然 提交于 2019-12-13 06:13:24

问题


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.

来源:https://stackoverflow.com/questions/18046204/making-database-call-asynchronous-in-ef

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!