dotnet / EntityFramework.Docs

Documentation for Entity Framework Core and Entity Framework 6
https://docs.microsoft.com/ef/
Creative Commons Attribution 4.0 International
1.59k stars 1.95k forks source link

Document the use of record types as entities #4438

Open cremor opened 11 months ago

cremor commented 11 months ago

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 commented 11 months ago

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 (https://github.com/dotnet/efcore/issues/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.