ef core 3.1 删除跟踪
ef core 3.1 delete tracking
更新数据库中的对象时出现以下未处理异常。
InvalidOperationException:无法跟踪实体类型 'DataloggerBrand' 的实例,因为已跟踪具有键值“{Id: 3}”的另一个实例。附加现有实体时,确保只附加一个具有给定键值的实体实例。
我正在使用 .net core 3.1 和 ef core 3.1(这不能改变这个学校项目的分配)
我的控制器中有以下代码:
if (ModelState.IsValid)
{
List<InfluxTechnologyDatalogger> influxTechnologyDataloggers = new List<InfluxTechnologyDatalogger>();
influxTechnologyDataloggers = GetActiveInfluxLoggers();
DataloggerBrand dataloggerBrand = _context.DataloggerBrands.AsNoTracking().FirstOrDefault(x => x.Id == 3);
foreach (var influxTechnologyDatalogger in influxTechnologyDataloggers)
{
InfluxTechnologyDatalogger selectedInfluxDatalogger = new InfluxTechnologyDatalogger();
selectedInfluxDatalogger = _context.InfluxTechnologyDataloggers.AsNoTracking()
.Include(x => x.DataloggerBrand)
.Include(x => x.DataloggerState)
.FirstOrDefault(x => x.SerialNumber == influxTechnologyDatalogger.SerialNumber);
if (!_context.InfluxTechnologyDataloggers.Any(x => x.SerialNumber == influxTechnologyDatalogger.SerialNumber))
{
DataloggerState newDataloggerState = new DataloggerState();
newDataloggerState = _context.DataloggerStates.AsNoTracking().FirstOrDefault(x => x.Name == "Choose State");
_context.Add(new InfluxTechnologyDatalogger()
{
DataloggerBrand = dataloggerBrand,
DataloggerState = newDataloggerState,
SerialNumber = influxTechnologyDatalogger.SerialNumber,
VHMeasureNumber = influxTechnologyDatalogger.VHMeasureNumber,
LastContact = influxTechnologyDatalogger.LastContact,
FreeSpaceOnSDCard = influxTechnologyDatalogger.FreeSpaceOnSDCard,
});
}
else
{
InfluxTechnologyDatalogger editedInfluxTechnologyDatalogger = new InfluxTechnologyDatalogger()
{
Id = selectedInfluxDatalogger.Id,
DataloggerBrand = selectedInfluxDatalogger.DataloggerBrand,
DataloggerState = selectedInfluxDatalogger.DataloggerState,
SerialNumber = selectedInfluxDatalogger.SerialNumber,
VHMeasureNumber = selectedInfluxDatalogger.VHMeasureNumber,
LastContact = influxTechnologyDatalogger.LastContact,
FreeSpaceOnSDCard = influxTechnologyDatalogger.FreeSpaceOnSDCard
};
_context.Update(editedInfluxTechnologyDatalogger);
}
_context.SaveChanges();
}
}
我的问题是如何解决这个问题? .AsNoTracking() 没有帮助。
可以分离上下文条目。
我也清理了代码。
""
如果(ModelState.IsValid)
{
列出 influxTechnologyDataloggers = new List();
influxTechnologyDataloggers = GetActiveInfluxLoggers();
DataloggerBrand 数据记录器品牌 = _context.DataloggerBrands.Find(3);
foreach (var influxTechnologyDatalogger in influxTechnologyDataloggers)
{
InfluxTechnologyDatalogger selectedInfluxDatalogger = new InfluxTechnologyDatalogger();
selectedInfluxDatalogger = _context.InfluxTechnologyDataloggers.AsNoTracking()
.FirstOrDefault(x => x.SerialNumber == influxTechnologyDatalogger.SerialNumber);
Datalogger selectedDatalogger = new Datalogger();
selectedDatalogger = _context.Dataloggers.AsNoTracking().Include(x => x.DataloggerState)
.FirstOrDefault(x => x.SerialNumber == influxTechnologyDatalogger.SerialNumber);
DataloggerState dataloggerState =
(selectedDatalogger == null || selectedDatalogger.DataloggerState == null) ?
_context.DataloggerStates.Find(5) : selectedDatalogger.DataloggerState;
int selectedId = (selectedInfluxDatalogger == null) ? 0 : selectedInfluxDatalogger.Id;
InfluxTechnologyDatalogger newInfluxTechnologyDatalogger = new InfluxTechnologyDatalogger()
{
Id = selectedId,
DataloggerBrand = dataloggerBrand,
DataloggerState = dataloggerState,
SerialNumber = influxTechnologyDatalogger.SerialNumber,
VHMeasureNumber = influxTechnologyDatalogger.VHMeasureNumber,
LastContact = influxTechnologyDatalogger.LastContact,
FreeSpaceOnSDCard = influxTechnologyDatalogger.FreeSpaceOnSDCard,
LastGPS = influxTechnologyDatalogger.LastGPS,
LastGPSLatitude = (influxTechnologyDatalogger.LastGPS == "") ? 0 : Conversion.GetLatitude(influxTechnologyDatalogger.LastGPS),
LastGPSLongitude = (influxTechnologyDatalogger.LastGPS == "") ? 0 : Conversion.GetLongitude(influxTechnologyDatalogger.LastGPS),
StreamlogDataloggerId = influxTechnologyDatalogger.StreamlogDataloggerId,
IsLogging = influxTechnologyDatalogger.IsLogging,
IsStreaming = influxTechnologyDatalogger.IsStreaming,
LastVRevisionID = influxTechnologyDatalogger.LastVRevisionID,
Diagnostic = influxTechnologyDatalogger.Diagnostic
};
_context.Update(newInfluxTechnologyDatalogger);
if (selectedDatalogger != null)
{
_context.Entry(selectedDatalogger.DataloggerState).State = EntityState.Detached;
}
_context.SaveChanges();
newInfluxTechnologyDatalogger = null;
dataloggerState = null;
}
} ""
更新数据库中的对象时出现以下未处理异常。
InvalidOperationException:无法跟踪实体类型 'DataloggerBrand' 的实例,因为已跟踪具有键值“{Id: 3}”的另一个实例。附加现有实体时,确保只附加一个具有给定键值的实体实例。
我正在使用 .net core 3.1 和 ef core 3.1(这不能改变这个学校项目的分配)
我的控制器中有以下代码:
if (ModelState.IsValid)
{
List<InfluxTechnologyDatalogger> influxTechnologyDataloggers = new List<InfluxTechnologyDatalogger>();
influxTechnologyDataloggers = GetActiveInfluxLoggers();
DataloggerBrand dataloggerBrand = _context.DataloggerBrands.AsNoTracking().FirstOrDefault(x => x.Id == 3);
foreach (var influxTechnologyDatalogger in influxTechnologyDataloggers)
{
InfluxTechnologyDatalogger selectedInfluxDatalogger = new InfluxTechnologyDatalogger();
selectedInfluxDatalogger = _context.InfluxTechnologyDataloggers.AsNoTracking()
.Include(x => x.DataloggerBrand)
.Include(x => x.DataloggerState)
.FirstOrDefault(x => x.SerialNumber == influxTechnologyDatalogger.SerialNumber);
if (!_context.InfluxTechnologyDataloggers.Any(x => x.SerialNumber == influxTechnologyDatalogger.SerialNumber))
{
DataloggerState newDataloggerState = new DataloggerState();
newDataloggerState = _context.DataloggerStates.AsNoTracking().FirstOrDefault(x => x.Name == "Choose State");
_context.Add(new InfluxTechnologyDatalogger()
{
DataloggerBrand = dataloggerBrand,
DataloggerState = newDataloggerState,
SerialNumber = influxTechnologyDatalogger.SerialNumber,
VHMeasureNumber = influxTechnologyDatalogger.VHMeasureNumber,
LastContact = influxTechnologyDatalogger.LastContact,
FreeSpaceOnSDCard = influxTechnologyDatalogger.FreeSpaceOnSDCard,
});
}
else
{
InfluxTechnologyDatalogger editedInfluxTechnologyDatalogger = new InfluxTechnologyDatalogger()
{
Id = selectedInfluxDatalogger.Id,
DataloggerBrand = selectedInfluxDatalogger.DataloggerBrand,
DataloggerState = selectedInfluxDatalogger.DataloggerState,
SerialNumber = selectedInfluxDatalogger.SerialNumber,
VHMeasureNumber = selectedInfluxDatalogger.VHMeasureNumber,
LastContact = influxTechnologyDatalogger.LastContact,
FreeSpaceOnSDCard = influxTechnologyDatalogger.FreeSpaceOnSDCard
};
_context.Update(editedInfluxTechnologyDatalogger);
}
_context.SaveChanges();
}
}
我的问题是如何解决这个问题? .AsNoTracking() 没有帮助。
可以分离上下文条目。 我也清理了代码。
"" 如果(ModelState.IsValid) { 列出 influxTechnologyDataloggers = new List(); influxTechnologyDataloggers = GetActiveInfluxLoggers(); DataloggerBrand 数据记录器品牌 = _context.DataloggerBrands.Find(3);
foreach (var influxTechnologyDatalogger in influxTechnologyDataloggers)
{
InfluxTechnologyDatalogger selectedInfluxDatalogger = new InfluxTechnologyDatalogger();
selectedInfluxDatalogger = _context.InfluxTechnologyDataloggers.AsNoTracking()
.FirstOrDefault(x => x.SerialNumber == influxTechnologyDatalogger.SerialNumber);
Datalogger selectedDatalogger = new Datalogger();
selectedDatalogger = _context.Dataloggers.AsNoTracking().Include(x => x.DataloggerState)
.FirstOrDefault(x => x.SerialNumber == influxTechnologyDatalogger.SerialNumber);
DataloggerState dataloggerState =
(selectedDatalogger == null || selectedDatalogger.DataloggerState == null) ?
_context.DataloggerStates.Find(5) : selectedDatalogger.DataloggerState;
int selectedId = (selectedInfluxDatalogger == null) ? 0 : selectedInfluxDatalogger.Id;
InfluxTechnologyDatalogger newInfluxTechnologyDatalogger = new InfluxTechnologyDatalogger()
{
Id = selectedId,
DataloggerBrand = dataloggerBrand,
DataloggerState = dataloggerState,
SerialNumber = influxTechnologyDatalogger.SerialNumber,
VHMeasureNumber = influxTechnologyDatalogger.VHMeasureNumber,
LastContact = influxTechnologyDatalogger.LastContact,
FreeSpaceOnSDCard = influxTechnologyDatalogger.FreeSpaceOnSDCard,
LastGPS = influxTechnologyDatalogger.LastGPS,
LastGPSLatitude = (influxTechnologyDatalogger.LastGPS == "") ? 0 : Conversion.GetLatitude(influxTechnologyDatalogger.LastGPS),
LastGPSLongitude = (influxTechnologyDatalogger.LastGPS == "") ? 0 : Conversion.GetLongitude(influxTechnologyDatalogger.LastGPS),
StreamlogDataloggerId = influxTechnologyDatalogger.StreamlogDataloggerId,
IsLogging = influxTechnologyDatalogger.IsLogging,
IsStreaming = influxTechnologyDatalogger.IsStreaming,
LastVRevisionID = influxTechnologyDatalogger.LastVRevisionID,
Diagnostic = influxTechnologyDatalogger.Diagnostic
};
_context.Update(newInfluxTechnologyDatalogger);
if (selectedDatalogger != null)
{
_context.Entry(selectedDatalogger.DataloggerState).State = EntityState.Detached;
}
_context.SaveChanges();
newInfluxTechnologyDatalogger = null;
dataloggerState = null;
}
} ""