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

Document the use of record types as entities #4438

Open
cremor opened this issue Aug 1, 2023 · 3 comments
Open

Document the use of record types as entities #4438

cremor opened this issue Aug 1, 2023 · 3 comments

Comments

@cremor
Copy link

cremor commented Aug 1, 2023

The page https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/types/records mentions this:

When to use records

Value equality

Not all data models work well with value equality. For example, Entity Framework Core depends on reference equality to ensure that it uses only one instance of an entity type for what is conceptually one entity. For this reason, record types aren't appropriate for use as entity types in Entity Framework Core.

There is no mention of this on the page Entity Types - EF Core or any other page in the EF Core documentation that I could find.

Please expand the EF core documentation to clearify the use of record types.

Please also include an explanation for why the default value equality of record types is actually a problem for EF Core. (If EF Core depends on reference equality (like documented above), then why doesn't EF Core just always use reference equality and ignore the equality implementation of the entity type?


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

@roji
Copy link
Member

roji commented Aug 1, 2023

For one thing, many people come to records in search of a quick-and-easy immutable type (and indeed, minimal record syntax with a primary constructor does produce an immutable type). Immutable types don't work well with EF regardless of records (dotnet/efcore#11457).

For another, records are very much about defining the equailty behavior of the type to be value equality. EF can indeed ignore that and use reference equality, but that's once again not what many users expect - after all, they've chosen to user records for a reason ("I've chosen value semantics, why does EF override that?").

All that isn't to say that it's impossible to use records with EF, just that it's usually not what users want. I agree the documentation on this can be improved though.

@PingvinB

This comment was marked as off-topic.

@cremor
Copy link
Author

cremor commented Sep 5, 2024

@PingvinB Please note that I created this issue explicitly for the Entity Types - EF Core page and also mentioned "entity" in the issue title. Also, EF Core 8 was not released when I created this issue, so "Complex Types" did not exist yet.

The section you linked explicitly mentions using C# record types as a "Complex type", not as an entity!

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

4 participants