Entity Framework MVVM Update Primary Key

Quentin Samuelson 1 Reputation point

I am using MVVM and Entity Framework to do a CRUD application. I am using INotifyPropertyChanged in a ViewModelBase class.
My data is present in a DataGrid. If I update the fields in the datagrid and use SaveChnages, it works for ALL fields apart from the primary key update.

Is there a way to determine the changes to the primary key field BEFORE and AFTER Update and then I can use a FromSqlRaw to produce and update command if the primary key has changed.

Would this be the correct way to update primary keys?

The table is a reference table with allowed values for other tables which are cascade updated so I cannot delete and insert the reference data values as it would then destroy the data in the other tables.


Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
512 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Duane Arnold 3,216 Reputation points

    No relational database is going to allow the update of a table record's primary-key. Once the primary-key has been assigned to a database table record, that's it, it is static and unchangeable.

    1 person found this answer helpful.
    0 comments No comments