0

I have a table with a composite primary key made up of 3 fields. It just so happens, that one of these fields can change, so what happens is that when I want to update a record in the database and I change field3 (part of the primary key), the entity framework can't find the record to update because I have changed one of the primary key fields. Is there something I can do to solve this with the following limitations?

  1. I can't change the primary fields.
  2. I can't add an identity field and make it primary.
  3. In a nutshell, I can't modify the schema.

This is not good database design and it was designed a while ago for individual hardware systems, so that is why I can't change the schema, because these individual units expect the schema in a certain way, otherwise, I would have designed it differently.

I understand primary keys should be immutable, but is there some sort of trick or hack I can use to update an existing record with a modified primary key without changing the schema?

Xaisoft
  • 45,655
  • 87
  • 279
  • 432
  • 1
    i would say that http://stackoverflow.com/questions/3838414/can-we-update-primary-key-values-of-a-table should answer your question – b_meyer Jun 05 '13 at 14:40
  • Can you fetch the object data, delete the object in the database, and insert a new one with the "updated" primary keys and the data from the previously fetched object? – Aether McLoud Jun 05 '13 at 15:29
  • @AetherMcLoud - So what you are saying is to get the original record, delete it and then insert it, so perform an update, but do it like an insert. – Xaisoft Jun 05 '13 at 15:32
  • Yes that's the only way I can think of to "change" a primary key with entityframework. Of course that won't work if foreign keys point to that object but seeing as you want to change a primary key I suppose no foreign keys use that primaries. – Aether McLoud Jun 05 '13 at 15:36

0 Answers0