0

可能重复:
无法使用已在使用的键添加实体 (LINQ)

我在这个问题上搜索了很多,发现很多人都面临同样的问题。但可以通过它。发布了许多解决方案,但没有收获。

我编写了一个LINQ查询,它检索大约 38 条记录,并使用foreach循环根据代码将这些记录插入到其他表中。

var Results =  (from p in (from d in compact.CURRENT_STATUS
                                                   where QtrEndDate >= d.ValidStartDate && QtrEndDate <= d.ValidDueDate && d.LocationCode == EmployeeLocation && d.DeptCode == selecteddept
                                            select new
                                            {
                                                loccode = d.LocationCode,
                                                taskcode = d.TaskCode,
                                                deptcode = d.DeptCode,
                                                flag = d.Flag,
                                                startdate = d.ValidStartDate,
                                                duedate = d.ValidDueDate,
                                                remarks = d.Remarks,
                                                content = d.UploadDocContent,
                                                title = d.UploadDocTitle,
                                                type = d.UploadDocType
                                            }).Union(from i in compact.HISTORY_STATUS
                                                     where QtrEndDate >= i.ValidStartDate && QtrEndDate <= i.ValidEndDate && i.LocationCode == EmployeeLocation && i.DeptCode ==  selecteddept
                                                     select new
                                                     {
                                                loccode = i.LocationCode,
                                                taskcode = i.TaskCode,
                                                deptcode = i.DeptCode,
                                                flag = i.Flag,
                                                startdate = i.ValidStartDate,
                                                duedate = i.ValidEndDate.Date,
                                                remarks = i.Remarks,
                                                content = i.UploadDocContent,
                                                title = i.UploadDocTitle,
                                                type = i.UploadDocType
                                                     }) select p);





                        foreach (var item in Results)
                        {
                            REPORT_TABLE RT = new REPORT_TABLE();
                            {

                                RT.LocationCode = item.loccode;
                                RT.TaskCode = item.taskcode;
                                RT.DeptCode = item.deptcode;
                                RT.Flag = item.flag;
                                RT.ValidStartDate = item.startdate;
                                RT.ValidDueDate = item.duedate;
                                RT.Remarks = item.remarks;
                                RT.UploadDocContent = item.content;
                                RT.UploadDocTitle = item.title;
                                RT.UploadDocType = item.type;
                                RT.SubmittedBy = (Session["username"]).ToString();
                                RT.SubmittedByIP = (Session["IP"]).ToString();
                                RT.SubmittedOn = System.DateTime.Now;
                                RT.ReportID = RHqueryFind.ReportID;
                                RT.AsOnDate = QtrEndDate;
                            }
                            compact.REPORT_TABLEs.InsertOnSubmit(RT);  
                            compact.SubmitChanges();
                        };

但我确实收到错误Cannot add an entity with a key that has been in use in the last line ie SubmitChanges

在其中一个解决方案中,我发现要添加一行代码,RefreshMode但这无法解决我的问题。

4

1 回答 1

1

我认为您必须删除列的主键。创建新列即SerialNo并将其设置为具有Identity Specification的主键

我遇到了同样的问题,即无法使用已在使用的键添加实体,但是当我删除了插入值的列的主键并使用主键和身份规范创建新列时。问题解决了

于 2013-01-30T04:43:20.143 回答