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(主键表)
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     }
实体类2(外键表)
  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; }
    }
实体类3(外键表)

实现方法:

原来实现的是直接把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         }
实现编辑

解决办法:就是先获取外键表的实体,将实体数据赋予要编辑表的外键。这样的话,实现主外键关联。真正实现编辑,而不是编辑变添加。

原文地址:https://www.cnblogs.com/AlphaThink-AT003/p/3327831.html