20 lines
1.3 KiB
Plaintext
20 lines
1.3 KiB
Plaintext
== Why is this an issue?
|
|
|
|
Using temporal types as the primary key of a table is risky.
|
|
When these types are used as primary keys, it usually means that each new key is created with the use of `.Now` or `.UtcNow` properties from `DateTime` and `DateTimeOffset` classes.
|
|
In those cases, duplicate keys exceptions may occur in many ways:
|
|
|
|
* when entries are added consecutively by a machine with low-enough system clock resolution;
|
|
* when two different threads are inserting entries in close enough sequence for both to have the same time;
|
|
* when changes such as daylight saving time (DST) transitions occur, where values can be repeated the following hour (only for `DateTime` type);
|
|
|
|
The rule raises an issue if:
|
|
|
|
* Entity Framework, or Entity Framework Core dependencies are found;
|
|
* a class contains a property either named `Id`, `<type name>Id` or decorated by the `[Key]` or `[PrimaryKey]` attribute.
|
|
* the key property is of one of the following types:
|
|
** https://learn.microsoft.com/en-us/dotnet/api/system.datetime[System.DateTime]
|
|
** https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset[System.DateTimeOffset]
|
|
** https://learn.microsoft.com/en-us/dotnet/api/system.timespan[System.TimeSpan]
|
|
** https://learn.microsoft.com/en-us/dotnet/api/system.dateonly[System.DateOnly]
|
|
** https://learn.microsoft.com/en-us/dotnet/api/system.timeonly[System.TimeOnly] |