1、在进行数据库表更新时出现以下错误提示
Database operation expected to affect 1 row(s) but actually affected 0 row(s).
存储区更新、插入或删除语句影响到了意外的行数(0)。实体在加载后可能被修改或删除。
贴出来的出错代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| TempVisionFormula = new Entities.VisionFormula() { DateTime = exists.Content[0].DateTime, Guid = exists.Content[0].Guid, Name = exists.Content[0].Name, Description = richTextBox1.Text, Width = width, Height = height, HoleCount = holecount, XOffset = 0d, UpdateDateTime = DateTime.Now, };
var result = sql.Update(TempVisionFormula);
|
数据库的更新方法如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| protected override Result Update(VisionFormula data) { try { interlocked.Enter(); if (data.IsNullOrEmpty()) return Result.Error(); using var context = new HiContext(); context.Formulas.Attach(data); context.Entry(data).Property(l => l.Guid).IsModified = false; context.Entry(data).Property(l => l.DateTime).IsModified = false; context.Entry(data).Property(l => l.Name).IsModified = true; context.Entry(data).Property(l => l.UpdateDateTime).IsModified = true; context.Entry(data).Property(l => l.Description).IsModified = true; context.Entry(data).Property(l => l.XOffset).IsModified = true; context.Entry(data).Property(l => l.Width).IsModified = true; context.Entry(data).Property(l => l.Height).IsModified = true; context.Entry(data).Property(l => l.Count).IsModified = true; context.SaveChanges(); return Result.Successed(); } catch (Exception ex) { ex.Error(StackTrace.LineNumber()); return Result.Error(ex); } finally { interlocked.Exit(); } }
|
错误分析:
由于更新时,没有将表的Id填写上去
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| TempVisionFormula = new Entities.VisionFormula() { FormulaId = exists.Content[0].FormulaId, DateTime = exists.Content[0].DateTime, Guid = exists.Content[0].Guid, Name = exists.Content[0].Name, Description = richTextBox1.Text, Width = width, Height = height, HoleCount = holecount, XOffset = 0d, UpdateDateTime = DateTime.Now, };
var result = sql.Update(TempVisionFormula);
|