Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the real difference between Alternate Key and HasIndex with uniqueness in EF core?

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.

like image 337
Menyus Avatar asked Feb 04 '20 12:02

Menyus


People also ask

What is alternate key EF core?

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.

Is unique entity framework core?

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.

What is composite key in Entity Framework?

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.

What is principal key in EF core?

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.

What is hasindex in Entity Framework Core fluent API?

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:

What is the difference between an alternate key and a unique?

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)

What are alternate keys in EF Core?

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

How do I enforce uniqueness on a column in EF?

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.


1 Answers

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)

like image 160
Pavlo Kyrylenko Avatar answered Oct 22 '22 08:10

Pavlo Kyrylenko