0

How can I apply not null and unique key constraint on a property from asp.net MVC model,

Below is my code:

public class Role
{
    public int id { get; set; }

    [Required]
    [Index("RoelsIndex",IsUnique=true)]        
    public string Roles { get; set; }
}
madhu
  • 33
  • 2
  • 9

1 Answers1

0

Not sure what's your problem but Required and Index work fine when I try it.

public class Product
{
    public int Id { get; set; }
    [Required]
    [Index(IsUnique = true)]
    [MaxLength(400)]
    public string Name { get; set; }
}
public class AppContext : DbContext
{
    public DbSet<Product> Products { get; set; }
}

Result

Yuliam Chandra
  • 14,494
  • 12
  • 52
  • 67