C
C#2mo ago
Mango

Error when tracking entity

I have some code that’s throwing the following exception:
The instance of entity type 'Setting' cannot be tracked because another instance with the key value '{Id: 286}' is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached.
The instance of entity type 'Setting' cannot be tracked because another instance with the key value '{Id: 286}' is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached.
This is the offending code:
var trackedSetting = context.ChangeTracker.Entries<Setting>().FirstOrDefault(e => e.SettingId == setting.SettingId);
if (trackedSetting != null)
context.Entry(trackedSetting.Entity).State = EntityState.Unchanged;
var trackedSetting = context.ChangeTracker.Entries<Setting>().FirstOrDefault(e => e.SettingId == setting.SettingId);
if (trackedSetting != null)
context.Entry(trackedSetting.Entity).State = EntityState.Unchanged;
1) does the call to context.Entry() attach the entity 2) if yes, is there a way to attach only if not already attached?
4 Replies
jcotton42
jcotton422mo ago
Entry, Attach, Update, etc. are all traps Why are you even explicitly looking in the change tracker?
Mango
MangoOP2mo ago
I’m still trying to ascertain that. I didn’t write this code. That entity is a shadow property
jcotton42
jcotton422mo ago
It seems like they wanted to use DbSet.Find? Which looks in the change tracker by PK, reaching out to the database if no match was found.
Mango
MangoOP2mo ago
There’s no PK property One sec, I can’t type this on mobile I have to go to my non work pc This is why asking for help anywhere outside teams sucks I can’t use Discord on work pc this is what the whole entity looks like:
public class Thing
{
private Setting _currentSetting = null!;

public Setting CurrentSetting { get { return _currentSetting; } set { _currentSetting = value; } }
// code ommitted for brevity
}
public class Thing
{
private Setting _currentSetting = null!;

public Setting CurrentSetting { get { return _currentSetting; } set { _currentSetting = value; } }
// code ommitted for brevity
}
DbContext setup:
public class ApplicationDbContext : DbContext
{
protected override OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Setting>(entity =>
{
entity.HasOne(e => e.CurrentSetting).WithMany().HasForeignKey("CurrentSetting");
}
}
}
public class ApplicationDbContext : DbContext
{
protected override OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Setting>(entity =>
{
entity.HasOne(e => e.CurrentSetting).WithMany().HasForeignKey("CurrentSetting");
}
}
}
and then in a save method, that original code is called according to the dev that wrote that, we are trying to insert a new Thing entity but without insert a new Setting entity and he said the default behavior is that it was trying to insert a new Setting entity each time more context from the dev: he chose not to setup a foreign key property on Thing to Setting cause he didnt want an ICollection<Thing> inverse property on Setting im wondering if we can have a public int SettingId { get; set; } and public Setting CurrentSetting { get; set; } property on Thing with no inverse property on Setting?

Did you find this page helpful?