简体   繁体   中英

How do I define an index for ASP.NET Core using EF?

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?

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();
}

According to documentation :

Indexes can not be created using data annotations.

But, you can use the Fluent API for this purpose.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM