EF核心 我有实体
public class LogisticsZone : BaseEntityDeleted<int>
{ ...
public virtual List<LogisticsZoneShippingPoint> LogisticsZoneShippingPoints { get; set; }
}
public class LogisticsZoneShippingPoint
{
public int ShippingPointId { get; set; }
public int LogisticsZoneId { get; set; }
}
我通过id获取LogisticsZone实体:
var entity = await this.repository.ListAsync(new IdSpecification<LogisticsZone>(id)).Include(x => x.LogisticsZoneShippingPoints).AsNoTracking().FirstOrDefaultAsync();
将其发送给客户端,并在来自客户端时对其进行编辑
public virtual async Task UpdateAsync(T entity, CancellationToken token = default(CancellationToken))
{
this.Context.Set<T>().Update(entity);
await this.Context.SaveChangesAsync(token);
}
在我将LogisticsZone字段添加到LogisticsZoneShippingPoint之前,它工作得非常好:
public class LogisticsZoneShippingPoint
{
public int ShippingPointId { get; set; }
public int LogisticsZoneId { get; set; }
public virtual LogisticsZone LogisticsZone { get; set; }
}
现在,当我通过id获得的LogisticsZone时,它会加载LogisticsZoneShippingPoint的列表(与以前一样),但还会将每个LogisticsZoneShippingPoint设置为LogisticsZone 当我尝试编辑此实体时,我得到错误消息:
无法跟踪实体类型'LogisticsZone'的实例,因为 具有相同的{'Id'}键值的另一个实例已经在 跟踪。连接现有实体时,请确保只有一个实体 具有给定键值的实例被附加。考虑使用 'DbContextOptionsBuilder.EnableSensitiveDataLogging'以查看 冲突的键值。