2

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 2

3

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();
}
Sign up to request clarification or add additional context in comments.

2 Comments

API has changed a little, now its .HasIndex(o => o. EmailAddress).IsUnique(true);.
do you know any idea on how to transform these by roslyn api
0

According to documentation:

Indexes can not be created using data annotations.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.