Entity FrameWork对有外键关联的数据表的添加操作
前天做了一个MVC Entity FrameWork项目,遇到有外键关联的数据编辑问题。当你编辑的时候,按照正常的逻辑,把每个字段的数据都对号入座了,然后点击保存按钮,本以为会顺理成章的编辑数据,但是EF却与众不同,它就是不如你所愿,当你查看数据列表的时候,你会发现列表中莫名其妙的又多了一条数据。很是蛋疼啊。
实体类

public class DeviceViewControl { public int ID { get; set; } //控件ID public DeviceView DeviceView { get; set; } //所在视图ID public DeviceSensorPoint DeviceSensorPoint { get; set; } //对应感控点ID public string ViewControlClassName { get; set; } //控件类名称 public string ViewControlPosition { get; set; } //控件位置 public string Remark { get; set; } //备注 }

1 public class DeviceView 2 { 3 public Device deviceID { get; set; } //所属设备ID 4 public int ID { get; set; } //视图ID 5 public string Code { get; set; } //视图编码 6 public string viewName { get; set; } //视图类名称 7 }

public class DeviceSensorPoint { public int ID { get; set; } public Device deviceID { get; set; } //设备ID public string EnglishName { get; set; } //感控点名称(英文) public string OldEnglishName { get; set; } //原有感控点名称(英文) public string ChineseName { get; set; } //中文感控点名称 public string ValueType { get; set; } //数据类型(Analog/Switch) public Nullable<bool> Readable { get; set; } //是否可读 public Nullable<bool> Writeable { get; set; } //是否可写 public Nullable<decimal> RecommandValue { get; set; } //推荐值 public Nullable<decimal> WriteValue { get; set; } //写入值 public Nullable<bool> IsWriten { get; set; } //是否写入 public Nullable<int> PLCReadChannelNo { get; set; } //读通道号 public Nullable<int> PLCWriteChannelNo { get; set; } //写通道号 public string SwitchNameOfTrue { get; set; } //开关量True代表的含义 public string SwitchNameOfFalse { get; set; } //开关量False代表的含义 public Nullable<decimal> MaxValue { get; set; } //量程上限 public Nullable<decimal> MinValue { get; set; } //量程下限 public bool IsAlarmNeeded { get; set; } //是否开启报警 public string Unit { get; set; } //计量单位 public Nullable<decimal> AlarmUpperValue { get; set; } //报警上限 public Nullable<decimal> AlarmLowerValue { get; set; } //报警下限 public string AlarmUpperInfo { get; set; } //报警上限提示信息 public string AlarmLowerInfo { get; set; } //报警下限提示信息 public string Remark { get; set; } //备注 public Nullable<decimal> CurrentValue { get; set; } }
实现方法:
原来实现的是直接把ID(外键关联)赋予外键,但是发现这样是不行的。会出现开篇时候所说的效果。就想了如下解决办法。

1 [HttpPost] 2 public ActionResult Edit(string deviceViewCode, DAL.DeviceViewControl deviceViewControl,int Id) 3 { 4 if (!ModelState.IsValid) 5 { 6 if (string.IsNullOrEmpty(deviceViewControl.ViewControlClassName)) 7 { 8 ModelState.AddModelError("ViewControlClassName", "请输入控件类名"); 9 } 10 } 11 12 //获取视图 13 var deviceView = this.db.DeviceViews.Where(p => p.Code == deviceViewCode).FirstOrDefault(); 14 15 //获取感控点 16 var deviceSeneorPoint = db.DeviceSensorPoints.Where(p => p.ID == deviceViewControl.DeviceSensorPoint.ID).FirstOrDefault(); 17 18 //获取控件 19 var deviceControl = db.DeviceViewControls.Where(p => p.ID == Id).FirstOrDefault(); 20 deviceControl.Remark = deviceViewControl.Remark; 21 deviceControl.ViewControlClassName = deviceViewControl.ViewControlClassName; 22 deviceControl.DeviceSensorPoint = deviceSeneorPoint; 23 this.db.SaveChanges(); 24 25 return RedirectToAction("List", new { deviceViewCode = deviceViewCode }); 26 }
解决办法:就是先获取外键表的实体,将实体数据赋予要编辑表的外键。这样的话,实现主外键关联。真正实现编辑,而不是编辑变添加。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?