Error when tracking entity
I have some code that’s throwing the following exception:
This is the offending code:
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
Entry, Attach, Update, etc. are all traps
Why are you even explicitly looking in the change tracker?
I’m still trying to ascertain that. I didn’t write this code.
That entity is a shadow property
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.
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:
DbContext setup:
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
?