代码之家  ›  专栏  ›  技术社区  ›  Thomas.Benz

在处理实体框架时使用C#Parallel.ForEach替换掉大部分循环

  •  0
  • Thomas.Benz  · 技术社区  · 8 年前

    我有一个控制台应用程序。应用程序要求我分析存储在Windows文件夹中的文本文件中的数据,将提取的数据插入或更新到SQL Server表中,并将摘要信息记录到外部公共日志文件中。我使用的是实体框架v6.2和 DbContext 类来操作数据。每个人拥有不同的文件夹,每个文件夹存储多个数据文本文件。

    目前,应用程序使用 同步的 使用方法 foreach 循环。请参阅下面的方法1。

    但是,我担心如果使用同步编程,那么当我将应用程序发布到生产服务器时,可能会导致性能问题,例如运行速度减慢。所以,我计划用C替换我的同步代码# Parallel.ForEach 接近。

    我的问题是: 使用C语言更好更安全吗# 并行循环 语句而不是使用当前的同步代码?

    谢谢你的帮助。

    我正在做什么和计划做什么的细节。

    方法1 :对于使用C#regular的最外层循环是同步的 foreach公司

    public void Process(List<Person> people)
    {
        // Note: each person has multiple folders
        // Each folder has multiple text files that are parsed in insert/update to SQL server database tables
    
        try
        {
            // Notes: 
            // _people variable is of type List<Person>
            // MyDbContext is of type C# Entity Framework DbContext
    
            // Note: one DbContext-typed context is used for all loops
    
            using (var context = new MyDbContext())
            {
                // Note: at present, I am using  C# regular foreach lopp to iterate all persons in People collection variable 
    
                foreach (var person in people) // Note: this is the outmost loop and it makes synchronous execution
                {
                    // Note: FolderSerivce is an helper class that uses C# DbContext-typed MyDbContext as above and person object to read folder rows in database for each person.
    
                    var folders = new FolderService(context, person);
                    var personName = person.Name;
    
    
                    foreach (var folder in folders)
                    {
                        var myParserAndInsert = new ParserAndInsert();
                        // Note: Dowork() method is used to parse all files of a folder and insert/update data to databse using EF "context" (DbContext typed" and to log summary to an external common log file).
                        myParserAndInsert.Dowork(context, folder); 
                    }
                }
            }
           ...
        }
        catch (Exception e)
        {
           ....
        }
    }
    

    方法2 :我计划使用 并行循环 替换最外层的循环,即遍历 List<Person> -输入方法1中的people变量。

    public void Process(List<Person> people)
    {
        // Note: each person has multiple folders
        // Each folder has multiple text files that are parsed in insert/update to SQL server database tables
    
        try
        {
            // Notes: 
            // _people variable is of type List<Person>
            // MyDbContext is of type C# Entity Framework DbContext
    
            Parallel.ForEach(people, person =>
            {
                // Note: context is re-created for each person
                using (var context = new MyDbontext())
                {
                    var folders = new FolderService(context, person);                            
    
                    foreach (var folder in folders)
                    {
                        var myParserAndInsert = new ParserAndInsert();
    
                        // Note: Dowork() method is used to parse all files of a folder and insert/update data to databse using EF "context" (DbContext typed" and to log summary to an external common log file).
    
                        myParserAndInsert.Dowork(context, folder); 
                    }
                }
            });
        }
        catch (Exception e)
        {
           ....
        }
    }
    
    0 回复  |  直到 6 年前