I can\'t find way to add a unique constraint to my field with using attribute:
public class User
{
[Required]
public int Id { get; set; }
[Requi
None of these methods worked for me in .NET Core 2.2 but I was able to adapt some code I had for defining a different primary key to work for this purpose.
In the instance below I want to ensure the OutletRef field is unique:
public class ApplicationDbContext : IdentityDbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity()
.HasIndex(o => new { o.OutletRef });
}
}
This adds the required unique index in the database. What it doesn't do though is provide the ability to specify a custom error message.