0

I have an entity (Entity1) which has a property of another entity (Entity2). The database is set up correctly and when I insert a new row for Entity1, the foreign key value is correct and I can insert infinite number of rows for Entity1 and they all reference Entity2 correctly.

However, when I make a change to Entity1, call Attach() and then try to save those changes, I'm getting a SQL exception that says it can't insert a new row for Entity2 because IDENTITY_INSERT is not on.

I obviously don't want to do anything with Entity2 - definitely not insert a new row, I just want to update values in the Entity1 table.

I am using an adapter pattern to adapt everything from my business models to my data entities and everything there looks fine.

I'm happy to supply code if you think that would help, but right now I'm just looking for some high level ideas of where to start chasing this one down.

TIA

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Blair Holmes
  • 1,521
  • 2
  • 22
  • 35
  • Some code samples would be really helpful - there's any number of things this could be. It sounds most to me like you're adding a version of Entity2 that is not being tracked by the saving context, but it's very difficult to help with why without code unfortunately – Nick Murphy Sep 27 '19 at 15:54
  • I think you might be on to something. I'll play around with this idea and if I can't sort it out, I'll post some code as a follow-up. – Blair Holmes Sep 27 '19 at 15:58
  • @NickMurphy Is there a way in VS debugger to see if entities are already attached and being tracked? – Blair Holmes Sep 27 '19 at 16:22
  • https://stackoverflow.com/a/12573886/8563429 will work in the watch window. Alternatively in you can expand your context object, expand the Entity2 set, and evaluate Local by allowing all threads to run. Anything in there is being tracked – Nick Murphy Sep 27 '19 at 16:40
  • @NickMurphy well I had a few issues in play here but I got it all sorted out now. Thanks so much for putting me on the right path - I had a disconnected entity at first (you were right) and then my adpater was creating a new instance of Entity2 and so I was getting "another entity with same key...." problems. Thanks again. – Blair Holmes Sep 27 '19 at 17:58

0 Answers0