我正在使用EF 5.0并且有一个关于使数据库调用异步的问题。
首先,下面是 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);
}
我调用下面的方法来实现异步,因为上面的调用大约需要1秒才能执行:
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;
}
但它是否会通过使用Task.Run()运行进程来提升任何性能?
在.Net框架中,我们有xxxAsync()方法。但我在EF 5.0中找不到任何此类方法。
如果以上代码效率不高,如何使用EF 5.0为昂贵的数据库调用编写异步方法?
感谢任何帮助。