Skip to main content
added 2 characters in body
Source Link
Sampath
  • 66.2k
  • 70
  • 327
  • 461

On EF core you cannot create Indexes using data annotations.But you can do it using the Fluent API.

Like this inside your {Db}Context.cs{Db}Context.cs:

protected override void OnModelCreating(ModelBuilder builder) { builder.Entity<User>() .HasIndex(u => u.Email) .IsUnique(); } 

...or if you're using the overload with the buildAction:

protected override void OnModelCreating(ModelBuilder builder) { builder.Entity<User>(entity => { entity.HasIndex(e => e.Email).IsUnique(); }); } 

You can read more about it here : Indexes

On EF core you cannot create Indexes using data annotations.But you can do it using the Fluent API.

Like this inside your {Db}Context.cs:

protected override void OnModelCreating(ModelBuilder builder) { builder.Entity<User>() .HasIndex(u => u.Email) .IsUnique(); } 

...or if you're using the overload with the buildAction:

protected override void OnModelCreating(ModelBuilder builder) { builder.Entity<User>(entity => { entity.HasIndex(e => e.Email).IsUnique(); }); } 

You can read more about it here : Indexes

On EF core you cannot create Indexes using data annotations.But you can do it using the Fluent API.

Like this inside your {Db}Context.cs:

protected override void OnModelCreating(ModelBuilder builder) { builder.Entity<User>() .HasIndex(u => u.Email) .IsUnique(); } 

...or if you're using the overload with the buildAction:

protected override void OnModelCreating(ModelBuilder builder) { builder.Entity<User>(entity => { entity.HasIndex(e => e.Email).IsUnique(); }); } 

You can read more about it here : Indexes

code aligned
Source Link

On EF core you cannot create Indexes using data annotations.But you can do it using the Fluent API.

Like this inside your {Db}Context.cs:

 protected override void OnModelCreating(ModelBuilder builder)  {   builder.Entity<User>()   .HasIndex(u => u.Email)   .IsUnique();  } 

...or if you're using the overload with the buildAction:

 protected override void OnModelCreating(ModelBuilder builder)  {   builder.Entity<User>(entity => {   entity.HasIndex(e => e.Email).IsUnique();   });  } 

You can read more about it here : Indexes

On EF core you cannot create Indexes using data annotations.But you can do it using the Fluent API.

Like this inside your {Db}Context.cs:

 protected override void OnModelCreating(ModelBuilder builder)  {   builder.Entity<User>()   .HasIndex(u => u.Email)   .IsUnique();  } 

...or if you're using the overload with the buildAction:

 protected override void OnModelCreating(ModelBuilder builder)  {   builder.Entity<User>(entity => {   entity.HasIndex(e => e.Email).IsUnique();   });  } 

You can read more about it here : Indexes

On EF core you cannot create Indexes using data annotations.But you can do it using the Fluent API.

Like this inside your {Db}Context.cs:

protected override void OnModelCreating(ModelBuilder builder) { builder.Entity<User>() .HasIndex(u => u.Email) .IsUnique(); } 

...or if you're using the overload with the buildAction:

protected override void OnModelCreating(ModelBuilder builder) { builder.Entity<User>(entity => { entity.HasIndex(e => e.Email).IsUnique(); }); } 

You can read more about it here : Indexes

Added an example for configuing the index inside an EntityTypeBuilder action.
Source Link

On EF core you cannot create Indexes using data annotations.But you can do it using the Fluent API.

Like this inside your {Db}Context.cs:

 protected override void OnModelCreating(ModelBuilder builder) { builder.Entity<User>() .HasIndex(u => u.Email) .IsUnique(); } 

...or if you're using the overload with the buildAction:

 protected override void OnModelCreating(ModelBuilder builder) { builder.Entity<User>(entity => { entity.HasIndex(e => e.Email).IsUnique(); }); } 

You can read more about it here : Indexes

On EF core you cannot create Indexes using data annotations.But you can do it using the Fluent API.

Like this inside your {Db}Context.cs:

 protected override void OnModelCreating(ModelBuilder builder) { builder.Entity<User>() .HasIndex(u => u.Email) .IsUnique(); } 

You can read more about it here : Indexes

On EF core you cannot create Indexes using data annotations.But you can do it using the Fluent API.

Like this inside your {Db}Context.cs:

 protected override void OnModelCreating(ModelBuilder builder) { builder.Entity<User>() .HasIndex(u => u.Email) .IsUnique(); } 

...or if you're using the overload with the buildAction:

 protected override void OnModelCreating(ModelBuilder builder) { builder.Entity<User>(entity => { entity.HasIndex(e => e.Email).IsUnique(); }); } 

You can read more about it here : Indexes

changed variable name to match the one .net core generates DbContexts. And specified which file the code is to be put (which is clear from the external link)
Source Link
ono2012
  • 5.3k
  • 2
  • 35
  • 43
Loading
added 15 characters in body
Source Link
Sampath
  • 66.2k
  • 70
  • 327
  • 461
Loading
Source Link
Sampath
  • 66.2k
  • 70
  • 327
  • 461
Loading