并行循环中的LINQ To SQL:如何防止重复插入?

时间:2018-12-13 14:46:26

标签: c# concurrency parallel-processing linq-to-sql parallel-foreach

在尝试并行化昂贵的API集成时遇到了麻烦。

该集成并行查询API,并填充ConcurrentBag集合。完成一些处理,然后将其传递到Parallel.ForEach(),在其中使用LINQ To Sql将其与数据库接口。

有:

  1. 一个与课程并行运行的外部循环

  2. 通过学科的内在循环

  3. 在其中,另一个遍历Lessons的循环。

问题,我遇到的问题是:由于任何一门课程可能都属于多门课程,因此并行循环浏览课程意味着有时一门课程会插入多次。 >

当前代码如下:

({externalCourseList是类型ConcurrentBag<ExternalCourse>的集合。)

Parallel.ForEach(externalCourseList, externalCourse =>
{
    using ( var context = new DataClassesDataContext() )
    {
        var dbCourse = context.Courses.Single( x => x.externalCourseId == externalCourse.courseCode.ToString() );

        dbCourse.ShortDesc = externalCourse.Summary;
        //dbCourse.LongDesc = externalCourse.MoreInfo;
        //(etc)

        foreach (var externalDiscipline in externalCourse.Disciplines)
        {
            var dbDiscipline = context.Disciplines.Where(
                x => x.ExternalDisciplineID == externalDiscipline.DisciplineCode.ToString() ).SingleOrDefault();

            if (dbDiscipline == null)
                dbDiscipline = new Linq2SQLEntities.Discipline();

            dbDiscipline.Title = externalDiscipline.Name;
            //(etc)
            dbDiscipline.ExternalDisciplineID = externalDiscipline.DisciplineCode.ToString();

            if (!dbDiscipline.IsLoaded)
                context.Disciplines.InsertOnSubmit(dbDiscipline);

            // relational table used as one-to-many relationship for legacy reasons
            var courseDiscipline = dbDiscipline.Course_Disciplines.SingleOrDefault(x => x.CourseID == dbCourse.CourseID);

            if (courseDiscipline == null)
            {
                courseDiscipline = new Course_Discipline
                {
                    Course = dbCourse,
                    Discipline = dbDiscipline
                };

                context.Course_Disciplines.InsertOnSubmit(courseDiscipline);
            }

            foreach (var externalLesson in externalDiscipline.Lessons)
            {
                /// The next statement throws an exception
                var dbLesson = context.Lessons.Where(
                    x => x.externalLessonID == externalLesson.LessonCode).SingleOrDefault();

                if (dbLesson == null)
                    dbLesson = new Linq2SQLEntities.Lesson();

                dbLesson.Title = externalLesson.Title;
                //(etc)
                dbLesson.externalLessonID = externalLesson.LessonCode;

                if (!dbLesson.IsLoaded)
                    context.Lessons.InsertOnSubmit(dbLesson);

                var disciplineLesson = dbLesson.Discipline_Lessons.SingleOrDefault(
                    x => x.DisciplineID == dbDiscipline.DisciplineID && x.LessonID == dbLesson.LessonID);

                if (disciplineLesson == null)
                {
                    disciplineLesson = new Discipline_Lesson
                    {
                        Discipline = dbDiscipline,
                        Lesson = dbLesson
                    };

                    context.Discipline_Lessons.InsertOnSubmit(disciplineLesson);
                }
            }

        }

        context.SubmitChanges();
    }
}
);

({IsLoadedhere所述实现。)

///之前的行上引发异常,因为同一课常常被插入多次,并且在.SingleOrDefault()上调用context.Lessons.Where(x => x.externalLessonID == externalLesson.LessonCode)失败。

解决这个问题的最佳方法是什么?

谢谢。

0 个答案:

没有答案