I'm intrested in what is the real difference between this
e.HasIndex(c => new { c.UserId, c.ApplicationId, c.Value }).IsUnique();
and this
e.HasAlternateKey(c => new { c.UserId, c.ApplicationId, c.Value });
in EF core fluent api configuration.
The use-case here is that we have a PK called Id
then we have a secondary(alternate key) which is a composite key which is built from the following attributes: UserID
ApplicationId
and Value
.
The only thing i found about my question is the following quote from here: https://www.learnentityframeworkcore.com/configuration/fluent-api/hasindex-method
"While similar in most practical aspects, this is not the same as creating a unique constraint on the column, which can be achieved by creating an alternate key on the property."
I dont really get the concept of this difference and i also don't know the difference between a unique constraint and a unique index.
If somone could explain the definitions(and the difference between them) to me i would be really grateful.
In EF, alternate keys are read-only and provide additional semantics over unique indexes because they can be used as the target of a foreign key. Alternate keys are typically introduced for you when needed and you do not need to manually configure them.
The Entity Framework Core Fluent API HasAlternateKey method enables you to create an alternate key by placing a unique constraint (and therefore a unique index) on a property or properties other than those that form the primary key. This is typically done to help ensure the uniqueness of data.
Entity Framework Core supports composite keys - primary key values generated from two or more fields in the database. Composite keys are not covered by conventions or data annotation attributes. The only way to configure composite keys is to use the HasKey method.
Principal key: The properties that uniquely identify the principal entity. This may be the primary key or an alternate key. Foreign key: The properties in the dependent entity that are used to store the principal key values for the related entity.
The Entity Framework Core Fluent API HasIndex method is used to create a database index on the column mapped to the specified entity property. By default, indexes are created for foreign keys and alternate keys. You may wish to create indexes on other properties to speed up data retrieval:
In EF, alternate keys are read-only and provide additional semantics over unique indexes because they can be used as the target of a foreign key. In other words - both create a unique index, but the alternate key has some additional properties (e.g. can be used as the target of a foreign key)
Alternate keys can be used at principal side of a FK relationships. Also in EF Core once created they cannot be modified (similar to primary keys). EF Core documentation: Alternate Keys
If you just want to enforce uniqueness on a column, define a unique index rather than an alternate key (see Indexes). In EF, alternate keys are read-only and provide additional semantics over unique indexes because they can be used as the target of a foreign key.
From the official documentation:
If you just want to enforce uniqueness on a column, define a unique index rather than an alternate key (see Indexes). In EF, alternate keys are read-only and provide additional semantics over unique indexes because they can be used as the target of a foreign key.
In other words - both create a unique index, but the alternate key has some additional properties (e.g. can be used as the target of a foreign key)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With