使用Entity Framework DB2数据库更新/删除记录时出错

pdkcd3nj  于 2023-03-23  发布在  DB2
关注(0)|答案(1)|浏览(237)

你好,我有一个名为Employee的Db2表,所有4列上都有一个主键

| EmployeeID | EmployeeName         |Email                      |     EmpVersion |
| --------   | --------------        | --------                 | -------------- |
|1          |Christoforo Rowcliffe  |crowcliffe0@dion.ne.jp     |0|
|1          |Christoforo Rowcliffe  |crowcliffe0@dion.ne.jp     |1|
|2          |Justus Cockin          |jcockin1@virginia.edu      |1|
|3          |Kellia Shaw            |kshaw2@chron.com           |0|
|3          |Kellia Shaw            |kshaw2@chron.com           |1|
|4          |Dalt Fletcher          |dfletcher3@typepad.com     |1|
|4          |Dalt Fletcher          |dfletcher3@typepad.com     |0|
|5          |Sonnie Stryde          |sstryde4@theatlantic.com   |0|
|6          |Jane Dow               |janedoe@theatlantic.com    |5|

我正在尝试过滤/更新记录,以便如果有任何员工的EmpVersion为0,则我需要检查是否有任何其他员工具有相同的ID,名称,电子邮件和EmpVersion为1,然后删除EmpVersion为0的记录,如果没有找到具有相同ID,名称和电子邮件的员工,则将当前员工记录EmpVersion更新为1
我希望输出类似于

| EmployeeID | EmployeeName         |Email                      |     EmpVersion |
| --------   | --------------        | --------                 | -------------- |
|1          |Christoforo Rowcliffe  |crowcliffe0@dion.ne.jp     |1|
|2          |Justus Cockin          |jcockin1@virginia.edu      |1|
|3          |Kellia Shaw            |kshaw2@chron.com           |1|
|4          |Dalt Fletcher          |dfletcher3@typepad.com     |1|
|5          |Sonnie Stryde          |sstryde4@theatlantic.com   |1|
|6          |Jane Dow               |janedoe@theatlantic.com    |5|

这是我的Linq查询

var employees = _dbContext.Employees.ToList();
        var EnployeesWithVersion0 = employees.Where(a => a.EmpVersion == 0).ToList();
        foreach (var emp in EnployeesWithVersion0)
        {
            try
            {
                if (Employees.Where(x => x.EmployeeID == emp.EmployeeID && 
                                        x.EmployeeName == emp.EmployeeName  &&
                                        x.Email == emp.Email && x.EmpVersion == 1).Any())
                {
                    _dbContext.Entry(emp).State = EntityState.Deleted;
                    _dbContext.SaveChanges();
    
                }
                else
                {
                    try
                    {
                        var innerEmployee = Employees.Where(Employees.Where(x => 
                                           x.EmployeeID == emp.EmployeeID && 
                                           x.EmployeeName == emp.EmployeeName  &&
                                           x.Email == emp.Email && x.EmpVersion ==0).FirstOrDefault();
    
                        _dbContext.EmpVersion = 1;
                        _dbContext.Employees.Update(innerEmployee);
                        _dbContext.SaveChanges();
    
    
                    }
                    catch (Exception ex)
                    {
                        //Log Error
    
                    }
                }
    
            }
            catch (Exception ex)
            {
              //Log Error
            }
         return employees;
       }

但是我得到了错误。更新条目时出错。有关详细信息,请查看内部异常。错误[23505] [IBM][DB2/NT 64] SQL 0803 N INSERT语句、UPDATE语句或DELETE语句导致的外键更新中的一个或多个值无效,因为主键唯一约束或唯一索引标识为1约束表dbo。雇员具有重复的索引键值。”有人对此有意见吗?

k4ymrczo

k4ymrczo1#

如果where没有严格要求使用一些应用程序逻辑,请尝试这种方法。

CREATE TABLE T (EmployeeID, EmployeeName, Email, EmpVersion) AS
(
VALUES
  (1, 'Christoforo Rowcliffe', 'crowcliffe0@dion.ne.jp',    0)
, (1, 'Christoforo Rowcliffe', 'crowcliffe0@dion.ne.jp',    1)
, (2, 'Justus Cockin', 'jcockin1@virginia.edu',             1)
, (3, 'Kellia Shaw', 'kshaw2@chron.com',                    0)
, (3, 'Kellia Shaw', 'kshaw2@chron.com',                    1)
, (4, 'Dalt Fletcher', 'dfletcher3@typepad.com',            1)
, (4, 'Dalt Fletcher', 'dfletcher3@typepad.com',            0)
, (5, 'Sonnie Stryde', 'sstryde4@theatlantic.com',          0)
, (6, 'Jane Dow', 'janedoe@theatlantic.com',                5)
) WITH DATA
;

DELETE FROM
(
SELECT 
  ROW_NUMBER () OVER 
  (
    PARTITION BY EMPLOYEEID, EMPLOYEENAME, EMAIL 
    ORDER BY EMPVERSION DESC
  ) AS RN_
FROM T
)
WHERE RN_ > 1
;

UPDATE T SET EMPVERSION = 1 WHERE EMPVERSION = 0;

SELECT * FROM T;

| 雇员|员工姓名|电子邮件|EMPVERSION|
| - ------|- ------|- ------|- ------|
| 1个|克里斯托福罗·罗克利夫|crowcliffe0@dion.ne.jp|1个|
| 第二章|贾斯特斯·科金|jcockin1@virginia.edu|1个|
| 三个|凯莉亚·肖|kshaw2@chron.com|1个|
| 四个|德尔塔·弗莱彻|dfletcher3@typepad.com|1个|
| 五个|桑尼·斯特雷德|sstryde4@theatlantic.com|1个|
| 六个|简·道|janedoe@theatlantic.com|五个|
fiddle

相关问题