dotnet / EntityFramework.Docs

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

Clarify the difference in EF between an alternate key and a unique index #1000

Open callmeblessed opened 6 years ago

callmeblessed commented 6 years ago

I followed this but I cannot update the License Plate because it is a key. How to make it unique but we still able to correct the License Plate later ?


Document Details

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

ajcvickers commented 6 years ago

@callmeblessed Try using HasIndex instead and making the index unique. Alternate keys have additional semantics in EF Core such as not being able to update the property.

VictorioBerra commented 5 years ago

I am encountering this issue as well. I can confirm HasIndex() combined with IsUnique solved my issue. IMO the docs are misleading. When you google "Unique Constraints" you see that doc first thing. It sounds like you get some unique protection on a column in the database but you dont realize EF wont be able to update it.

douglasg14b commented 5 years ago

Agreed with @VictorioBerra The docs are pretty misleading on this one.

davisnw commented 4 years ago

Also, there appears to be no way in EF Core to add a unique constraint without having it also be an alternate key. Sometimes we don't want the extra "alternate key" enforcement that EF adds to the application logic (namely, not being able to change any part of the alternate key), but the dba prefers to have it listed as a unique constraint instead of an index (less likely to accidentally drop an important unique enforcement when evaluating excessive indexes).

AndriySvyryd commented 4 years ago

Related: https://github.com/aspnet/EntityFramework.Docs/issues/1561