We applied .netcore 3.1 + EF core 3.1 to an old DB, we cannot change it or change its keys.
We a list where users can delete from it. In this list, we display the number of each row which is (1,2,3,4).
The row field is a composite key beside the primary key. (Cannot Change this)
If the user deletes the row 3, I need to reorder the remaining ones like (1,2,3).
The way I did it was to pull all items and re-increment each entity, but I got the below exception.
How can I accomplish this?
Stack trace Exception:
Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=527962 for information on understanding and handling optimistic concurrency exceptions.
EF Core version: 3.1
Target framework: (.NET Core 3.1)
IDE: (Visual Studio 2019 16.3)
@cristureansamuel there isn't enough information on exactly what you're doing, can you please post a short, runnable code sample?
Hi @roji, please see below a sample project
https://we.tl/t-gkXxfGqQLX
@roji why did you close it ?
Did you manage to place a breakpoint on the exception?
I only added the label after you closed the issue, assuming that you found an issue in your code.
When deleting a row in your application, I get the following error, not the one you posted above:
System.InvalidOperationException: The property 'ListRow' on entity type 'UpdateList' 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' then associate the dependent with the new principal.
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetPropertyModified(IProperty property, Boolean changeState, Boolean isModified, Boolean isConceptualNull, Boolean acceptChanges)
Either way, you are trying to modify ListRow, which is defined in your model as an alternate key - this is probably not what you want. Try removing that and your program should work.
Alternate keys are only when you need have a related entity, but using with the foreign key targeting a property other than the primary key (see the docs). As you don't have any related entities, that doesn't really make sense here. If you just want to enforce uniqueness on ListRow, define a unique index instead.
@roji
_Either way, you are trying to modify ListRow, which is defined in your model as an alternate key - this is probably not what you want. Try removing that and your program should work._
This is what I want, to modify those keys and arrange them as they are displayed to the user.
Cannot change anything as the program runs on an old DB and every change is going to break other systems.
Duplicate of #4073
@cristureansamuel can you confirm whether you have an actual related entity, with a foreign key constraint to ListRow (and not to Id)? If not, you should not be defining ListRow as an alternate key.
If so, then as @smitpatel posted above this is currently unsupported. Understand that if you change an alternate key, that means that all related entities must be changed as well to point to the new value - is this what you're looking for in your application?
_If so, then as @smitpatel posted above this is currently unsupported. Understand that if you change an alternate key, that means that all related entities must be changed as well to point to the new value - is this what you're looking for in your application?_
@roji yes :)
That won't be taken care of by EF Core, until #4073 is implemented. If you really can't change the database schema (which seems to be quite a bad fit for what you're trying to do), you can probably take care of this via raw SQL, i.e. manually send UPDATE queries to modify the IDs.