一个实体对象不能由多个 IEntityChangeTracker 实例引用

错误:一个实体对象不能由多个 IEntityChangeTracker 实例引用

原因:

之前的代码:db.T_DeviceVarDataCurrent.Add(current); 

 SaveChanges执行后current对象中的一些值(或引用)会发生改变,导致catch 中obj发生改变。 

修改:try中list不参与数据库操作,或者catch中重新赋值给新对象current2。

如下:

        /// <summary>
        ///  最新数据
        /// </summary>
        /// <param name="str"></param>
        /// <param name="minuteInterval"></param>
        public void AddDeviceVarDataCurrent(string str)
        {
            var waterSensor = JsonConvert.DeserializeObject<WaterSensorRealTimeModel>(str);
            if (waterSensor.CP != null && waterSensor.CP.data != null && waterSensor.CP.data.Count() > 0)
            {
              
                //如果不报错,一下插入多条。
                using (var db = new FarmSensorDataWaterEntities())
                {
                    var tran = db.Database.BeginTransaction();//开启事务
                    var list = new List<T_DeviceVarDataCurrent>() { };
                    var nowTime = DateTime.Now;
                    try
                    {
                        var varList = db.T_Var.ToList();
                        var deviceList= db.T_Device.ToList();
                        var varTypeList = db.T_VarType.ToList();
                        var deviceVarList = db.T_DeviceVar.ToList();
                        foreach (var item in waterSensor.CP.data)
                        {
                            var dateTime = GetDateTime(item.SampleTime);
                            //如果是正常值
                            if (item.Flag == "N" && dateTime != null)
                            {
                                var varId = varList.Where(d => d.Tag == item.code).Select(d => d.VarID).SingleOrDefault();
                                var deviceId = deviceList.Where(d => d.Tag == waterSensor.MN&& d.ProviderID==1).Select(d => d.DeviceID).SingleOrDefault();//中科山东东营地理研究院
                                var typeList = varTypeList.Where(d => d.TypeID ==1).ToList();// 实时值
                                if (varId != 0 && deviceId != 0)
                                {
                                    foreach (var type in typeList)
                                    {
                                        var deviceVarID = deviceVarList.Where(d => d.VarID == varId && d.DeviceID == deviceId && d.TypeID == type.TypeID).Select(d => d.DeviceVarID).SingleOrDefault();
                                        if (deviceVarID != 0)
                                        {
                                            //存储list
                                            var current = new T_DeviceVarDataCurrent
                                            {
                                                DeviceVarID = deviceVarID,
                                                Value = item.Rtd,
                                                VarDate = (DateTime)dateTime,
                                                UpdateTime = nowTime,
                                            };
                                            list.Add(current);

                                            var currentInfo = db.T_DeviceVarDataCurrent.Where(d => d.DeviceVarID == deviceVarID).SingleOrDefault();
                                            if (currentInfo != null)                                         
                                            {   //修改
                                                //currentInfo.Value = item.Rtd;
                                                //currentInfo.VarDate = (DateTime)dateTime;
                                                //currentInfo.UpdateTime = nowTime; 
                                                currentInfo.Value = current.Value;
                                                currentInfo.VarDate = current.VarDate;
                                                currentInfo.UpdateTime = current.UpdateTime;
                                            }
                                            else
                                            {  //添加
                                                var current2 = new T_DeviceVarDataCurrent
                                                {
                                                    DeviceVarID = deviceVarID,
                                                    Value = item.Rtd,
                                                    VarDate = (DateTime)dateTime,
                                                    UpdateTime = nowTime,
                                                };
                                                db.T_DeviceVarDataCurrent.Add(current2);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        if (list.Count > 0)
                        {
                            db.SaveChanges();
                             //必须调用Commit(),不然数据不会保存
                            tran.Commit();
  
                        }

                    }
                    catch (Exception ex)
                    {
                        tran.Rollback(); //出错就回滚
                        while (ex.InnerException != null)
                        {
                            ex = ex.InnerException;
                        }
                        LogManage.Error(ex);
                        //若重复错误,则逐条遍历。
                        if (ex.Message.Contains("IX_"))
                        {
                            foreach (var obj in list)
                            {
                                try
                                {
                                    using (var db2 = new FarmSensorDataWaterEntities())
                                    {
                                        var currentInfo = db2.T_DeviceVarDataCurrent.Where(d => d.DeviceVarID == obj.DeviceVarID).SingleOrDefault();
                                
                                        if (currentInfo != null)
                                        {
                                            currentInfo.Value = obj.Value;
                                            currentInfo.VarDate = obj.VarDate;
                                            currentInfo.UpdateTime = nowTime;
                                        }
                                        else
                                        {
                                            var current = new T_DeviceVarDataCurrent
                                            {
                                                DeviceVarID = obj.DeviceVarID,
                                                Value = obj.Value,
                                                VarDate = obj.VarDate,
                                                UpdateTime = obj.UpdateTime,
                                            };
                                            db2.T_DeviceVarDataCurrent.Add(current);
                                        }
                                        db2.SaveChanges();

                                    }
                                }
                                catch (Exception ex2)
                                {
                                    //LogManage.Error(ex2);
                                }
                            }
                       }
                    }
                }
            }
        }

  

 

posted @ 2023-03-07 15:26  hao_1234_1234  阅读(20)  评论(0编辑  收藏  举报