Entityframework.extensions 在批量删除时出现错误“序列包含多个元素”

本文关键字:包含多 元素 错误 extensions 删除 Entityframework | 更新日期: 2023-09-27 18:30:30

我正在我的项目中使用 EntityFramework.Extended 库,其中包含一个代码优先实体模型。当我执行此 linq 语句并从库中执行批量删除时,我收到错误消息"序列包含多个元素":

var subjLocal = (from subjectLocal in customerContext.RosterSummaryData_Subject_Local
                         where ((subjectLocal.fkRosterSetID == 0) &&
                                (statsInfo.TestInstanceIDsList.Contains(subjectLocal.fkTestInstanceID)) &&
                                (subjectLocal.fkTestTypeID == statsInfo.TestTypeID) &&
                                (statsInfo.SchoolYearIDsList.Contains(subjectLocal.fkSchoolYearID)) &&
                                (subjectLocal.fkRosterTypeID == 1) &&
                                (subjectLocal.fkSchoolID == 0) &&
                                (subjectLocal.fkDepartmentID == 1) &&
                                (subjectLocal.fkCourseID == 1) &&
                                (subjectLocal.fkPeriodID == 1) &&
                                (statsInfo.DemoCatIDsList.Contains(subjectLocal.fkDemoCommonCategoryID)) &&
                                (statsInfo.DemoCodeIDsList.Contains(subjectLocal.fkDemoCommonCodeID)) &&
                                (statsInfo.TestSubjectIDsList.Contains(subjectLocal.fkTest_SubjectID)))
                         select subjectLocal.pkSummarySubjectLocalID).ToArray();
        if (subjLocal.Length > 0)
        {
            customerContext.RosterSummaryData_Subject_Local.Where(s => subjLocal.Contains(s.pkSummarySubjectLocalID)).Delete();
            customerContext.RosterSummaryData_Subject_Local_Bands.Where(s => subjLocal.Contains(s.fkSummarySubjectLocalID)).Delete();
            customerContext.RosterSummaryData_Subject_Local_Averages.Where(s => subjLocal.Contains(s.fkSummarySubjectLocalID)).Delete();
            customerContext.SaveChanges();
        }

错误发生在:

customerContext.RosterSummaryData_Subject_Local.Where(s => subjLocal.Contains(s.pkSummarySubjectLocalID)).Delete();

customerContext是我继承DBContext的基本实体,RosterSummaryData_Subject_Local/_Bands/_Averages是一个子表。我正在使用引用导入库,它注意到 Delete() 方法,因此它可以正常工作:

using EntityFramework.Extensions;

批量删除的目的不是为了确保您可以一次删除多个项目,并且序列不应该包含多个元素吗?我在这里缺少什么吗?

Entityframework.extensions 在批量删除时出现错误“序列包含多个元素”

你可以在这里找到类似的帖子EntityFramework.Extensions 6.1 Batch Delete抛出"序列包含多个元素"。

根据@deherech,这似乎是扩展框架中的一个错误