Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reliability on getting generated ID of inserted items using C# 9 immutable records as models #30837

Closed
Peluko opened this issue May 6, 2023 · 2 comments

Comments

@Peluko
Copy link

Peluko commented May 6, 2023

Hello. I'm testing the use of C# 9 records as models for EF and I've confirmed that, albeit being immutable, the record object passed to .Add() gets its autogenerated ID field updated after .SaveChanges().

I think that this behaviour is intentional (and very convenient, of course), but as it clashes directly with the immutability of records, I want to ask if I can rely on this behaviour for future developments. I'm unable to find anything about it on the available documentation.

Regards.

@roji
Copy link
Member

roji commented May 8, 2023

Duplicate of #11457

@roji roji marked this as a duplicate of #11457 May 8, 2023
@roji
Copy link
Member

roji commented May 8, 2023

EF doesn't currently have a full story for truly immutable CLR types; such a full story would mean eventually refraining from updating the immutable entity with the database-generated ID. Instead, a new instance would be created and returned with the new ID. However, if the current behavior is what you want, that should be fine - we'll very likely always allow you to e.g. have a private writable field, where EF would inject the ID. This way, although the type technically isn't fully immutable, it doesn't publicly expose APIs for mutation.

@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale May 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants