I get error when trying to insert entity, (whole graph to be correct) using Entity framework when Id is has some set value, for example 6. It has it because it was stored in another database, and I can manually set it for each element in a list, and for each element in sublist etc etc. But I have to do this for each element, and for each query. I would like to have unique global way of setting Id field to zero because when I try to insert value I get error:
Cannot insert explicit value for identity column in table 'XXX' when IDENTITY_INSERT is set to OFF.
I want my database to set Ids, and make entity framework to ignore if Id is set to anything besides 0
What I tried was to ovveride OnSaveChanges and that seems to late because when Add or AddRange method is used object was started to being tracked and I cannot set Id to multiple objects in a list.
Idto 0 in the business logic before adding it to any Entity Framework collection/tracking? It sounds like you're getting the object from another system which already has an identifier and need to strip out that identifier, which I would interpret as the business logic of the mapping between the two systems.