Postgres database contains product table with natural primary key:
create table product
(
productcode char(10) primary key,
price numeric(12,2),
... a lot of other columns
);
and other similar tables with natural primary keys.
ASP.NET 5 MVC application is used to update it using EF Core with Npgsql data provider.
If product code is also changed, EF Core throws error
The property 'Product.Productcode' is part of a key and so cannot be modified or marked as modified. To change the principal of an existing entity with an identifying foreign key, first delete the dependent and invoke 'SaveChanges', and then associate the dependent with the new principal.
Product code is used as foreign key in other tables (with ON UPDATE CASCADE clause) so it cannot deleted.
Database structure change is not an option.
How to allow update of the primary key column also?
Some ideas:
Block this check in EF Core, e.q setting old value to same as new value forcibly before update.
Set primary key to some other value before saving changes.
Force EF Core to create update statement and execute it manually
Use some EF Core extension or other framework.
Change Npgsql EF core provider to allow this.
Which is best way to implement this without changing database structure?
Exception is thrown in line
private static void ThrowIfKeyChanged(InternalEntityEntry entry, IProperty property)
{
if (property.IsKey()
&& property.GetAfterSaveBehavior() == PropertySaveBehavior.Throw)
{
throw new InvalidOperationException(CoreStrings.KeyReadOnly(property.Name, entry.EntityType.DisplayName()));
}
}
Is it OK to comment this out and compile new EF Core dll.