Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

c# - Efficient and fast way to process database records in thread or parallel

I'm currently searching for an efficient and fast way to process some records from a SQL Server database table in parallel/thread. Currently, I'm creating a console app that process the following:

  • Update some records in the database (roughly thousands)
  • Generate an excel file based on the updated records in the database
  • Send an email and attached the generated excel file

Below is my trim code:

public static void Main(string[] args)
{
    // Users here might be thousands of records
    var users = UnitOfWork.UserRepository.GetAll().Where(p => p.Status == "Active").ToList();

    Parallel.ForEach(requestsToUpdate, (user) =>
    {
        UpdateRecordsBaseOnProductId(user.Id);
    });
}

private void UpdateRecordsBaseOnProductId(int userId)
{
    // User requests also might be thousands of records
    var userRequests = UnitOfWork.UserRequestRepository
                                    .GetAll()
                                    .Where(userRequest => userRequest.Id == userId);

    foreach (var item in userRequests)
    {
        item.Status = "Denied";
        item.DateUpdated = DateTime.Now;

        UnitOfWork.UserRequestRepository.Update(item);
        UnitOfWork.Save();
    }

    // Generate excel file
    // Send email with excel attachedment
}

Basically, what I want is to process all the Users that I retrieved from the database in parallel obviously to finish the processing in a short period of time.

So far in my existing code using Parallel.Foreach, I'm receiving the following errors in random.

  • A second operation started on this context before a previous asynchronous operation completed. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context. Any instance members are not guaranteed to be thread safe

  • requires an open and available connection. The connection's current state is connecting

  • Object reference not set to an instance of an object.

These errors occur in the UpdateRecordsBaseOnProductId method. Where I'm retrieving the user history.

Any recommendations and suggestions will highly appreciated.

Additionally, I'm using a Repository Pattern and Entity Framework 6 to query, update, and save records in the database.

Thank you!upda


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...