I'm using EF.Core in an ASP.NET core project. I have a model class, but can't find any documentation about what attributes to set on a property or the class to create an index or a unique index. Is this possible?
2 Answers
This is currently not supported in EF core (at least not yet). I believe this is in the pipeline to be a standard annotation instead of a separate downloadable package. You can use the fluent API instead:
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<User>()
.HasIndex(u => u.EmailAddress).Unique();
}
2 Comments
Martin Braun
API has changed a little, now its
.HasIndex(o => o. EmailAddress).IsUnique(true);.Karthic G
do you know any idea on how to transform these by roslyn api
According to documentation:
Indexes can not be created using data annotations.
But, you can use the Fluent API for this purpose.