并行运行多个的EntityFramework数据库查询 [英] Running several EntityFramework database queries in parallel

查看:155
本文介绍了并行运行多个的EntityFramework数据库查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图并行运行,但林不知道3分贝查询,我做正确的。

我做了3个功能,每个进行查询到数据库。

 私有静态异步任务<串GT;是getAccount codeAsync(字符串的DeviceID)
    {
        长deviceIdLong = long.Parse(DEVICEID);
        使用(VAR DB =新NetworksEntities())
        {
            返回db.Devices.Where(X => x.DeviceId == deviceIdLong)。选择(X => x.Account code).FirstOrDefault();
        }
    }    私有静态异步任务<串GT; getDeviceTypeAsync(字符串的DeviceID)
    {
        长deviceIdLong = long.Parse(DEVICEID);
        使用(VAR DB =新NetworksEntities())
        {
            返回db.Devices.Where(X => x.DeviceId == deviceIdLong)。选择(X => x.DeviceType)。.FirstOrDefault()的devicetype;
        }
    }    私有静态异步任务<串GT; getUserNameAsync(字符串userid)
    {
        INT userIdInt;
        Int32.TryParse(用户ID,出userIdInt);
        使用(VAR DB =新NetworksEntities())
        {
            返回db.Users.Where(X => x.UserId == userIdInt)。选择(X => x.Email).FirstOrDefault();
        }
    }

然后我在我的code运行三项任务:

  VAR TaskAccount code =等待是getAccount codeAsync(DEVICEID);
    VAR TaskDeviceType =等待getDeviceTypeAsync(DEVICEID);
    VAR TaskUsername =等待getUserNameAsync(用户ID);
    等待Task.WhenAll();
    //使用来自我的3个任务的结果使一个新的插入到数据库。

就是我实际上做并行运行我的3个的数据库查询?

编辑:

 私有静态异步任务<串GT;是getAccount codeAsync(字符串的DeviceID)
    {
        长deviceIdLong = long.Parse(DEVICEID);
        使用(VAR DB =新NetworksEntities())
        {
            返回db.Devices.Where(X => x.DeviceId == deviceIdLong)。选择(X => x.Account code).FirstOrDefaultAsync();
        }
    }


解决方案

您将不得不改变code的最后一部分,使其在并行运行:

  VAR taskAccount code =是getAccount codeAsync(DEVICEID);
VAR taskDeviceType = getDeviceTypeAsync(DEVICEID);
VAR taskUsername = getUserNameAsync(用户ID);
等待Task.WhenAll(taskAccount code,taskDeviceType,taskUsername);
VAR帐户code = taskAccount code.Result;
VAR的devicetype = taskDeviceType.Result;
VAR用户名= taskUsername.Result;

请注意,只有一个的await 。在原来的code你等待每个任务一个接一个地使他们的顺序,而不是并行运行。在

此外,是getAccount codeAsync 的方法等等都算不上异步方法(你应该得到一个编译器警告这个)。然而,实体框架6对异步操作的支持,并使用您应该替换 FirstOrDefault <$c$c>FirstOrDefaultAsync.对于每个并行操作,则必须使用一个单独的环境,这是你在做什么。

I am trying to run 3 db queries in parallel but im not sure that I am doing it correctly.

I have made 3 functions which each make a query to the db.

    private static async Task<string> getAccountCodeAsync(string deviceId)
    {
        long deviceIdLong = long.Parse(deviceId);
        using (var db = new NetworksEntities())
        {
            return db.Devices.Where(x => x.DeviceId == deviceIdLong).Select(x => x.AccountCode).FirstOrDefault();
        }
    }

    private static async Task<string> getDeviceTypeAsync(string deviceId)
    {
        long deviceIdLong = long.Parse(deviceId);
        using (var db = new NetworksEntities())
        {
            return db.Devices.Where(x => x.DeviceId == deviceIdLong).Select(x => x.DeviceType).FirstOrDefault().DeviceType;
        }
    }

    private static async Task<string> getUserNameAsync(string userId)
    {
        int userIdInt;
        Int32.TryParse(userId, out userIdInt);
        using (var db = new NetworksEntities())
        {
            return db.Users.Where(x => x.UserId == userIdInt).Select(x => x.Email).FirstOrDefault();
        }
    }   

I then in my code run the three tasks:

    var TaskAccountCode = await getAccountCodeAsync(deviceId);
    var TaskDeviceType = await getDeviceTypeAsync(deviceId);
    var TaskUsername = await getUserNameAsync(userId);
    await Task.WhenAll();   
    //use the results from my 3 tasks to make a new insert into the db.

Is what I am doing actually running my 3 db queries in parallel?

EDIT:

    private static async Task<string> getAccountCodeAsync(string deviceId)
    {
        long deviceIdLong = long.Parse(deviceId);
        using (var db = new NetworksEntities())
        {               
            return db.Devices.Where(x => x.DeviceId == deviceIdLong).Select(x => x.AccountCode).FirstOrDefaultAsync();
        }
    }

解决方案

You will have to change the last part of the code to make it run in parallel:

var taskAccountCode = getAccountCodeAsync(deviceId);
var taskDeviceType = getDeviceTypeAsync(deviceId);
var taskUsername = getUserNameAsync(userId);
await Task.WhenAll(taskAccountCode, taskDeviceType, taskUsername);
var accountCode = taskAccountCode.Result;
var deviceType = taskDeviceType.Result;
var username  = taskUsername.Result;

Notice that there is only one await. In your original code you await every task one after the other making them run in sequence instead of in parallel.

Also, the methods getAccountCodeAsync etc. are not really async methods (you should get a compiler warning about this). However, Entity Framework 6 has support for async operations and to use that you should replace FirstOrDefault with FirstOrDefaultAsync. For each parallel operation you will have to use a separate context, and that is exactly what you are doing.

这篇关于并行运行多个的EntityFramework数据库查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆